c++mysqlwebwt

Wt++ TableView and QueryModel


I have a problem with tableView and queryModel this is my error:

    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>&’

This is a 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();
};

this code is in inside in application 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) );

Solution

  • Wt::WTableView::setModel expects a shared pointer, i.e. std::shared_ptr<Wt::WAbstractItemModel>, not a raw pointer to your item model.

    So, you should replace

    auto *modelDataTable = new Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>>();
    

    by

    auto modelDataTable = std::make_shared<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Utente>> >();
    

    Note that this is exactly what your error message is telling you:

    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>&

    (Try to reread the error message, so you understand it the next time)