Wt++ TableView 和 QueryModel
Wt++ TableView and QueryModel
我对 tableView 和 queryModel 有疑问
这是我的错误:
lo.C: In constructor ‘HelloApplication::HelloApplication(const Wt::WEnvironment&)’:
hello.C:162:48: error: no matching function for call to ‘Wt::WTableView::setModel(std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type)’
tableView->setModel( std::move(modelDataTable) );
^
In file included from hello.C:30:0:
/usr/local/include/Wt/WTableView.h:94:16: note: candidate: virtual void Wt::WTableView::setModel(const std::shared_ptr<Wt::WAbstractItemModel>&)
virtual void setModel(const std::shared_ptr<WAbstractItemModel>& model)
^
/usr/local/include/Wt/WTableView.h:94:16: note: no known conversion for argument 1 from ‘std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type {aka Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*}’ to ‘const std::shared_ptr<Wt::WAbstractItemModel>&’
这是一个 Utente class:
class Utente
{
public:
Utente();
std::string nome;
std::string cognome;
template<class Action>
void persist(Action& a)
{
dbo::field(a, nome, "nome");
dbo::field(a, cognome, "cognome");
}
~Utente();
};
此代码在应用程序内部 class :
auto *modelDataTable = new Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>>();
modelDataTable->setQuery(session.query<Wt::Dbo::ptr<Utente>>("select utente from utente Utente"));
modelDataTable->addAllFieldsAsColumns();
auto tableView = root()->addWidget(std::make_unique<Wt::WTableView>() );
tableView->setModel( std::move(modelDataTable) );
Wt::WTableView::setModel
需要一个共享指针,即 std::shared_ptr<Wt::WAbstractItemModel>
,而不是指向您的项目模型的原始指针。
所以,你应该更换
auto *modelDataTable = new Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>>();
来自
auto modelDataTable = std::make_shared<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>> >();
请注意,这正是您的错误消息告诉您的内容:
no known conversion for argument 1 from std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type
{aka __Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*
} to const std::shared_ptr<Wt::WAbstractItemModel>&
(尝试重新阅读错误信息,以便下次理解)
我对 tableView 和 queryModel 有疑问 这是我的错误:
lo.C: In constructor ‘HelloApplication::HelloApplication(const Wt::WEnvironment&)’:
hello.C:162:48: error: no matching function for call to ‘Wt::WTableView::setModel(std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type)’
tableView->setModel( std::move(modelDataTable) );
^
In file included from hello.C:30:0:
/usr/local/include/Wt/WTableView.h:94:16: note: candidate: virtual void Wt::WTableView::setModel(const std::shared_ptr<Wt::WAbstractItemModel>&)
virtual void setModel(const std::shared_ptr<WAbstractItemModel>& model)
^
/usr/local/include/Wt/WTableView.h:94:16: note: no known conversion for argument 1 from ‘std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type {aka Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*}’ to ‘const std::shared_ptr<Wt::WAbstractItemModel>&’
这是一个 Utente class:
class Utente
{
public:
Utente();
std::string nome;
std::string cognome;
template<class Action>
void persist(Action& a)
{
dbo::field(a, nome, "nome");
dbo::field(a, cognome, "cognome");
}
~Utente();
};
此代码在应用程序内部 class :
auto *modelDataTable = new Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>>();
modelDataTable->setQuery(session.query<Wt::Dbo::ptr<Utente>>("select utente from utente Utente"));
modelDataTable->addAllFieldsAsColumns();
auto tableView = root()->addWidget(std::make_unique<Wt::WTableView>() );
tableView->setModel( std::move(modelDataTable) );
Wt::WTableView::setModel
需要一个共享指针,即 std::shared_ptr<Wt::WAbstractItemModel>
,而不是指向您的项目模型的原始指针。
所以,你应该更换
auto *modelDataTable = new Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>>();
来自
auto modelDataTable = std::make_shared<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>> >();
请注意,这正是您的错误消息告诉您的内容:
no known conversion for argument 1 from
std::remove_reference<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*&>::type
{aka __Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente> >*
} toconst std::shared_ptr<Wt::WAbstractItemModel>&
(尝试重新阅读错误信息,以便下次理解)