Results 1 to 8 of 8

Thread: TableView does not show (up) with QSqlTableModel

  1. #1
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Red face TableView does not show (up) with QSqlTableModel

    Hi,

    I created a Qt window form (GUI) and put an empty tableView on the form in Qt Creator. When I started the GUI, the GUI::Initialization() function was called. But nothing happened with the tableView -- no header, no title, no row, no column. It was still empty. Why it did not show up? There was no any error message. What's wrong with my code? Can anybody help me? My code is as the following.

    Also how to save the QSqlTableModel contents to a file and load it afterwards from the file?

    Thank you in advance.

    /Fulin



    void GUI::Initialization()
    {
    QSqlTableModel model;
    initializeModel(&model);
    tableView_init(&model);
    }
    void GUI::initializeModel(QSqlTableModel *model)
    {
    model->setTable("person");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit) ;
    model->select();
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    }

    void GUI::tableView_init(QSqlTableModel *model)
    {
    ui->tableView->setModel(model);
    ui->tableView->show();
    }

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TableView does not show (up) with QSqlTableModel

    it looks like you started off with Qt's tablemodel example but then changed it and left out the connection to the sqlite db. If there is no data supplied, the table will be completely empty and the header won't be shown.

    oh, and btw use code tags next time.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TableView does not show (up) with QSqlTableModel

    Even if there was a database available: you initialise a QSQlQueryModel allocated in a local scope that is destroyed at the end of the
    GUI::Initialization() method.

    BTW: Are you the same person as sujan.dasmahapatra from https://www.qtcentre.org/showthread.php?t=42722 or is it just a coincidence?
    Last edited by ChrisW67; 27th June 2011 at 23:04. Reason: Fixed fat-handed typing

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TableView does not show (up) with QSqlTableModel

    Quote Originally Posted by ChrisW67 View Post
    BTW: Are you the same person as sujan.dasmahapatra from https://www.qtcentre.org/showthread.php?t=42722 or is it just a coincidence?
    perhaps they are in the same class working on the same assignment

  5. #5
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Default Re: TableView does not show (up) with QSqlTableModel

    Thanks for the hints. I'm just a QT newbie and want to learn and use a simple predefined model/view for my 2D parameter data online tuning. My objectives: data can be saved to file and load from a file; data can be viewed and edited from the tableview. Next step is to put data in sql database.

    I tried the QStandardItemModel also as the following, still nothing is seen from the tableview --empty. It seems that the qt model/view is really not so easy for a qt-newbie.

    Any suggestions?

    Qt Code:
    1. QStandardItemModel model( 5, 2 );
    2. for( int r=0; r<5; r++ )
    3. for( int c=0; c<2; c++)
    4. {
    5. QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );
    6.  
    7. if( c == 0 )
    8. for( int i=0; i<3; i++ )
    9. item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) );
    10.  
    11. model.setItem(r, c, item);
    12. }
    13. ui->tableView->setModel(&model);
    14. ui->tableView->show();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TableView does not show (up) with QSqlTableModel

    What is the point of bringing up more questions?
    have you fixed your initial problem?
    I *can* reproduce the empty header, empty table by commenting out the call to 'createConnection'.

    Here is a suggestions... try the tablemodel example as is and see if that works for you.

  7. The following user says thank you to schnitzel for this useful post:

    fulin (30th June 2011)

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TableView does not show (up) with QSqlTableModel

    Your code with minimal support to make it self-contained:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QStandardItemModel model( 5, 2 );
    9. for( int r=0; r<5; r++ )
    10. for( int c=0; c<2; c++)
    11. {
    12. QString("Row:%0, Column:%1").arg(r).arg(c) );
    13.  
    14. if( c == 0 )
    15. for( int i=0; i<3; i++ )
    16. item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) );
    17.  
    18. model.setItem(r, c, item);
    19. }
    20.  
    21. QTableView view;
    22. view.setModel(&model);
    23. view.show();
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    The result:
    example.png
    Clearly it can work (although your attempt to build a tree is wasted on a table view).

    You need to ask yourself why it is not working in your case. Consider the lifetime of the model (and ignore Qt... this is pure C++ understanding).

  9. The following user says thank you to ChrisW67 for this useful post:

    fulin (30th June 2011)

  10. #8
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Default Re: TableView does not show (up) with QSqlTableModel

    Thank you, ChrisW67 and Schnitzel .

    You are right. Now I got the QStandardItemModel work. Great ! I'll try to add QSqlQueryModel and to make QSqlTableModel work.

Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 20:06
  2. Replies: 3
    Last Post: 12th July 2010, 13:12
  3. tableview
    By GuL in forum Newbie
    Replies: 1
    Last Post: 26th August 2008, 17:18
  4. help in tableview
    By bala in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 15:46
  5. TreeView, TableView
    By rbrand in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2006, 08:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.