[SOLVED] In Qt I want to make my model generation in a separate class for a QTableView



  • As a C/C# programmer I'm new to Qt and have little experience in C++.

    What I want to do is 'making' the model of a QTableView in the class containing my data. In C# I could return a DataSet from a static method in a class containing all my stuff concerning this data, and bind this dataset to my table or list.

    public class Books
    {
       //properties
       ...
    
       //construtors etc...
       ...
    
       static DataSet BookData()
       {
           // fill my dataset
           return myDataSet;
       }
    } 
    

    In the main program I then bind my DataSet with the control I wanted to use

    Is there a way to do so in Qt / C++ doing the same so that I can write something like:

    QSqlTableModel* Books::BookData()
    {
        // Create an QSqlTableModel
        // Fill it with my data
        return model; // or whatever is possible
    }
    

    in the main program:

    ...
        ui->tvBooks->setModel(BookData());
    ...
    

    And this with correct garbage cleaning or is this wishful thinking...

    tnx


  • Qt Champions 2016

    @ON7AMI---Jean-Paul-Mertens said:

    Hi and welcome

    Its a good idea to read about the view /model idea in Qt
    http://doc.qt.io/qt-4.8/qsqlquerymodel.html

    I do not know C# but I assume a DataSet is a table/selection from the DB.

    you could do

    class Books
    {
         QSqlQueryModel * GetBookData()
       {
       QSqlQueryModel *model = new QSqlQueryModel; // makes a new each time called!
        model->setQuery("SELECT name, pagecount FROM books");
        model->setHeaderData(0, Qt::Horizontal, tr("Name"));
        model->setHeaderData(1, Qt::Horizontal, tr("Pages"));
        return model;
       }
    }; 
    
        QTableView *view = new QTableView;
        view->setModel(model);
        view->show();
    

    If you sort of want to wrap it all into one handler object/class.

    And this with correct garbage cleaning or is this wishful thinking...

    Well many classes in Qt will take ownership of objects given to them.
    But in this case view->setModel(model); does not delete the old model as it could be shared between views so its
    up to you in this case.



  • @mrjj said:

    I did:

    class Books
    {
       static QSqlQueryModel * GetBookData()
       {
       QSqlQueryModel *model = new QSqlQueryModel; // makes a new each time called!
        model->setQuery("SELECT name, pagecount FROM books");
        ...
        return model;
       }
    }; 
    
        QTableView *view = new QTableView;
        view->setModel(Books::GetBookData());
        view->show();
    

    and that worked fine. Just to figure out how I can delete the models.


  • Qt Champions 2016

    hi

    Well you do not need to new the model each time. You can just change the
    selection of data it makes available.

    you could have one in the class (something) like

    class Books {
    	QSqlQueryModel model;
    
    	 QSqlQueryModel * GetBookData()
    	{
    		model.setQuery ( "SELECT name, pagecount FROM books" );
    		return  &model;
    	}
    
    	void ChangeBooksData ()
    	{
    		model.setQuery ( "SELECT SOMETHING ELSE FROM books" );
    	}
    };
    

    But then you do need an instance of Books and the model will have same life span as Books instance.

    Books mybook; // will be deleted when runs out of scope. Say like after main returns.
    QTableView *view = new QTableView;
    view->setModel( mybook.GetBookData() );
    view->show();
    
    mybook.ChangeBooksData ();// make the view show something else
    

    Sorry if I don't understand what you are trying to archive.



  • @mrjj

    OK but I use the class to encapsulate all data and logics on that data. The collection of all data to fill drop-down boxes, lists reports etc... are all in the class together with the hiding of the record data by get and put protection. So if I have to create an instance every time, I'll spend a whole needless bunch of heap for every list. If I use static functions it's more memory friendly :-)

    I have studied the model/view stuff of Qt almost the same thing was used by MS in the MFC time a 20 years ago, they also had a Data/View structure, but then we programmed still in C. So my ignorance is situated there that I have little C++ experience, just played a little with it in the time of Turbo Borland C++ a long time ago.

    Professionally I'm a MS programmer but for my son I'm developing a whole suite on Linux and Mono did not give me what I was used to so I switched to Qt and C++. This way the old man has to study again. But it keeps the mind young...

    Thanks a lot
    Jean Paul



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.