Project

General

Profile

TableView and QueryModel

Added by Samuele Pederzini 3 months ago

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

hello.C: In constructor HelloApplication::HelloApplication(const Wt::WEnvironment&):
hello.C:163: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

#include <Wt/Dbo/Dbo.h>
#include <string>



namespace dbo = Wt::Dbo;

class Utente
{ 
public:
  Utente();
    //int id;
    std::string nome;
    std::string cognome;
    //std::string telefono;

    template<class Action>
    void persist(Action& a)
    {
        //dbo::field(a, id,     "id");
        dbo::field(a, nome, "nome");
        dbo::field(a, cognome,     "cognome");
        //dbo::field(a, telefono,     "telefono");
    }
   ~Utente();
};

implementation code:

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) );

Replies (2)

RE: TableView and QueryModel - Added by Bruce Toll 3 months ago

Hi,

I believe that modelDataTable has the wrong type. The setModel() call is expecting a shared_ptr. See: https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WTableView.html#ae658504e7ff363609462390dc387ebb0.

I haven't tested it, but you might try something like:

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

In addition, you do not need the std::move() in the setModel() call.

RE: TableView and QueryModel - Added by Samuele Pederzini 3 months ago

I’ve tested and I don’t have any error message, but unfortunately the app crashes in this line

tableView->setModel(modelDataTable); 

Error:

192.168.30.43 - - [2020-Dec-29 14:02:40.414] "GET /?wtd=Jv77jLqKGLKjXEqV&request=style&page=1 HTTP/1.1" 200 108
[2020-Dec-29 14:02:40.414] 11910 - [info] "WebRequest: took 34.995 ms"
Segmentation fault (core dumped)

    (1-2/2)