Results 1 to 12 of 12

Thread: Difficulties with QTableView and setModel

  1. #1
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Difficulties with QTableView and setModel

    The Qt4.4 Doc, in the SQL section, has the following example for connecting an opened SQL form to a QTableView widget:

    Qt Code:
    1. QTableView *view = new QTableView;
    2. view->setModel(model);
    3. view->show();
    To copy to clipboard, switch view to plain text mode 
    However, when I try something similar:

    Qt Code:
    1.  
    2. model.setTable("employee");
    3. model.setFilter("salary > 50000");
    4. model.setSort(2, Qt::DescendingOrder);
    5. model.select();
    6.  
    7. for (int i = 0; i < model.rowCount(); ++i) {
    8. QString name = model.record(i).value("name").toString();
    9. int salary = model.record(i).value("salary").toInt();
    10. qDebug() << name << salary;
    11. }
    12.  
    13. model.insertRows(row, 1);
    14. model.setData(model.index(row, 0), 1013);
    15. model.setData(model.index(row, 1), "Peter Gordon");
    16. model.setData(model.index(row, 2), 68500);
    17. model.submitAll();
    18.  
    19. ui.cueTable->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    I get

    "src/cues.cpp:38: error: no matching function for call to ‘QTableView::setModel(QSqlTableModel&)â€⠄¢
    /usr/include/qt4/QtGui/qtableview.h:73: note: candidates are: virtual void QTableView::setModel(QAbstractItemModel*)
    make: *** [build/cues.o] Error 1"

    I cant tell why.. As far as I understood, QSqlTableModel Was a QAbstractItemModel. I'm trying to put the model into a QTableView, just like in the example, but it won't accept it.

    Why is this?

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Difficulties with QTableView and setModel

    You have to pass a pointer to the model, not a reference to it. And don't create the model on the stack - it will go out of scope immediately.

  3. #3
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difficulties with QTableView and setModel

    Try this:

    ui.cueTable->setModel(&model);

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Difficulties with QTableView and setModel

    Quote Originally Posted by cydside View Post
    Try this:

    ui.cueTable->setModel(&model);
    This won't work because of the reasons I mentioned in my post.

  5. #5
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difficulties with QTableView and setModel

    Sorry, I've posted my hint without refresh the page with your post.

  6. #6
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    "ui.cueTable->setModel(*model);"

    Doesn't work for me either. What syntax do you suggest? I'm not very comfortable with pointers yet.

    Thank you for the help thus far

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    There are 2 errors in your code (both of which Wysota mentioned).
    i) you must pass a POINTER in the call to setModel
    ii) you must ensure that that pointer actually lives long enough

    The easiest way to to do that, is:
    Qt Code:
    1. QSqlTableModel *model = new QSqlTableModel(ui.cueTable);
    2.  
    3. << replace your calls to "model." with "model->" >>
    4.  
    5. setModel(model);
    To copy to clipboard, switch view to plain text mode 


    If the method you posted should not go out of scope (e.g. if it is followed by a QApplication::exec() or so...); then you could leave your code as is and just write
    Qt Code:
    1. setModel(&model);
    To copy to clipboard, switch view to plain text mode 
    otherwise you will leave your view with a dangling pointer and the code will crash in all probability.

    HTH
    Christoph

  8. #8
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    Thanks for your response, caduel, I think I understand it better now.

    I've moved everything back into my ui class, just to make it simpler for now. I put a

    QSqlTableModel *model;

    in the public declaration for that class. In the implementation, I have:

    Qt Code:
    1. QSqlDatabase cuedb = QSqlDatabase::addDatabase("QSQLITE");
    2. cuedb.setHostName("westdalelighting");
    3. cuedb.setDatabaseName("Cues");
    4. cuedb.setUserName("Westdale");
    5. cuedb.setPassword("westdale");
    6. bool ok = cuedb.open();
    7.  
    8. int row = 1;
    9.  
    10. model->setTable("employee");
    11. model->setFilter("salary > 50000");
    12. model->setSort(2, Qt::DescendingOrder);
    13. model->select();
    14.  
    15. for (int i = 0; i < model->rowCount(); ++i) {
    16. QString name = model->record(i).value("name").toString();
    17. int salary = model->record(i).value("salary").toInt();
    18. qDebug() << name << salary;
    19. }
    20.  
    21. model->insertRows(row, 1);
    22. model->setData(model->index(row, 0), 1013);
    23. model->setData(model->index(row, 1), "Peter Gordon");
    24. model->setData(model->index(row, 2), 68500);
    25. model->submitAll();
    26.  
    27. cueTable->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    Which compiles successfully, but fails to put any items into my cueTable widget. The cueTable widget was constructed by QDesigner-- am I perhaps missing something there?

  9. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    Where do you actually create that model?

    A line of
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    just declares a pointer. When your class is instantiated, all you have is an (uninitialized, i.e. random!) pointer to a model.

    You need to have a real model, i.e. a line like
    Qt Code:
    1. model = new QSqlTableModel( <add Qt-parent here> );
    To copy to clipboard, switch view to plain text mode 
    somewhere.


    (Side note: On the other hand, if you are willing to move the model (pointer) into your class, then you could of course put the real model there. Then you could revert to your original code in most places.)

  10. #10
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    I want the model to be accessible to other functions within that class, so I'll need to declare *model in the class definition, no?

    Now I have "QSqlTableModel *model;" in the public declaration of the class, and

    Qt Code:
    1. void MainWindowImpl::cueNewShowClick(){
    2.  
    3. -> model = new QSqlTableModel( cueTable );
    4.  
    5. QSqlDatabase cuedb = QSqlDatabase::addDatabase("QSQLITE");
    6. cuedb.setHostName("westdalelighting");
    7. cuedb.setDatabaseName("Cues");
    8. cuedb.setUserName("Westdale");
    9. cuedb.setPassword("westdale");
    10. bool ok = cuedb.open();
    11.  
    12. int row = 1;
    13.  
    14. model->setTable("employee");
    15. model->setFilter("salary > 50000");
    16. model->setSort(2, Qt::DescendingOrder);
    17. model->select();
    18.  
    19. for (int i = 0; i < model->rowCount(); ++i) {
    20. QString name = model->record(i).value("name").toString();
    21. int salary = model->record(i).value("salary").toInt();
    22. qDebug() << name << salary;
    23. }
    24.  
    25. model->insertRows(row, 1);
    26. model->setData(model->index(row, 0), 1013);
    27. model->setData(model->index(row, 1), "Peter Gordon");
    28. model->setData(model->index(row, 2), 68500);
    29. model->submitAll();
    30.  
    31. cueTable->setModel(model);
    32. }
    To copy to clipboard, switch view to plain text mode 

    Still though, nothing appears in the QTableView. This event is executed when a "populate table" button is pressed.

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Difficulties with QTableView and setModel

    Does such table exist?
    J-P Nurmi

  12. #12
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulties with QTableView and setModel

    try to print the output of your database calls
    i.e. do something like
    Qt Code:
    1. bool ok = cuedb.open();
    2. qDebug() << "open: ok="<<ok;
    To copy to clipboard, switch view to plain text mode 
    Do the same for all calls that might fail.

Tags for this Thread

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.