Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] In Qt I want to make my model generation in a separate class for a QTableView

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

Scheduled Pinned Locked Moved General and Desktop
qt5qtableviewqsqltablemodelc++
5 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ON7AMI - Jean Paul Mertens
    wrote on 25 Jul 2015, 13:49 last edited by ON7AMI - Jean Paul Mertens
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Jul 2015, 14:29 last edited by
      #2

      @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.

      O 1 Reply Last reply 25 Jul 2015, 15:17
      1
      • M mrjj
        25 Jul 2015, 14:29

        @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.

        O Offline
        O Offline
        ON7AMI - Jean Paul Mertens
        wrote on 25 Jul 2015, 15:17 last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 Jul 2015, 16:08 last edited by mrjj
          #4

          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.

          O 1 Reply Last reply 26 Jul 2015, 06:47
          1
          • M mrjj
            25 Jul 2015, 16:08

            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.

            O Offline
            O Offline
            ON7AMI - Jean Paul Mertens
            wrote on 26 Jul 2015, 06:47 last edited by
            #5

            @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

            1 Reply Last reply
            0

            3/5

            25 Jul 2015, 15:17

            • Login

            • Login or register to search.
            3 out of 5
            • First post
              3/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved