Results 1 to 12 of 12

Thread: QTableView sorting problem

  1. #1
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView sorting problem

    Hello!

    I'm new to Qt and I'm using Qt eclipse integration with version 4.3.4 in Windows XP.
    I've created a QTableView in the Eclipse's designer. The tableview is filled with a QSqlTableModel from a SQLite database and I set the sortingEnabled property to true.
    When running the program and click in a column header all the table goes empty (the data and the headers)...

    This is the code I have:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("stock.sqlite");
    3. if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    4.  
    5. model = new QSqlTableModel(this);
    6. model->setTable("productes");
    7. model->select();
    8. ui.tableView->setSortingEnabled(true);
    9. ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    10. ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    11. ui.tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? I tried a lot of things ( changing the propertys in the designer or after the setModel and I got the same result).

    Thanks in advance.

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

    Default Re: QTableView sorting problem

    Could you provide a minimal compilable example reproducing the problem?

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QTableView sorting problem

    My experience with this comination is : allways use
    Qt Code:
    1. while (myQueryModel->canFetchMore()){
    2. myQueryModel->fetchMore();
    3. }
    To copy to clipboard, switch view to plain text mode 

    If this solves your Problem: I dont know

    btw: sqlite + QSqlTabeleModel is a pain in the ass
    I use QSqlQueryModel. But then sorting does not work

  4. #4
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting problem

    Thank you Janus, but this do not sove my problem...

    A compilable code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDatabase>
    3. #include <QSqlQuery>
    4. #include <QString>
    5. #include <QSqlTableModel>
    6. #include <QTableView>
    7. #include <QtGui>
    8. #include <QtSql>
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. //QWidget *w = new QWidget;
    15.  
    16. QTableView * tableView = new QTableView();
    17.  
    18. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    19. db.setDatabaseName("stock.sqlite");
    20. if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    21.  
    22. model = new QSqlTableModel(tableView);
    23. model->setTable("productes");
    24. model->select();
    25. tableView->setSortingEnabled(true);
    26. tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    27. tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    28. tableView->setModel(model);
    29.  
    30. db.close();
    31. tableView->show();
    32. //w->show();
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    this can be tried with the database attached (it is very small), i put the .txt extension to upload it, but it is in sqlite3 format.

    I was trying (for one week) to use a mysql database, but I can't compile the driver, I know that are lots of threads and lots of tutorials, but I always get a compile error, is there a really working way to compile/load the mysql driver?
    Attached Files Attached Files

  5. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView sorting problem

    I had a similar problem that went me crazy, but it was with QTableWidget, maybe it could give you some clou.
    My problem was solved by disabling the sorting before table filling and reenabling it after fill was complete, so something like:
    Qt Code:
    1. void myClass::populateTable()
    2. {
    3. table->setSortingEnabled(false);
    4. // table fill operations.... [...]
    5. table->setSortingEnabled(true);
    6. }
    To copy to clipboard, switch view to plain text mode 
    I hope this can somehow help.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #6
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QTableView sorting problem

    i modified it a little bit. But at least it works like this ...

    Qt Code:
    1. tablesort::tablesort(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QTableView * tableView = new QTableView(this);
    7.  
    8. connect(tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),this, SLOT(err()));
    9.  
    10.  
    11. db = QSqlDatabase::addDatabase("QSQLITE");
    12.  
    13. db.setDatabaseName("test");
    14.  
    15. if (!db.open()) QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    16.  
    17. model = new QSqlTableModel(this);
    18. model->setTable("tbl1");
    19. model->select();
    20.  
    21. tableView->setSortingEnabled(true);
    22. tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    23. tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    24. tableView->setModel(model);
    25. // db.close();
    26. }
    27.  
    28. void tablesort::err()
    29. {
    30.  
    31. QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

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

    noktus (22nd April 2008)

  8. #7
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting problem

    Thank you janus! it works now.

    The error was that i was showing te widget from a Dialog, and then i get a "Driver not loaded" error when clicking in the header.

  9. #8
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting problem

    It only works if only the widget is shown, if I do an exec of a QDialog before or after the widget is shown, when I click in the header driver not loaded error happens.
    I don't know if it is a problem of the database, in the installation of QT or in my application.
    I prefer to use another database, better mysql, but I'm not able to compile the drivers...

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

    Default Re: QTableView sorting problem

    Wrap the model into QSortFilterProxyModel and forget about all the problems.

  11. The following user says thank you to wysota for this useful post:

    noktus (23rd April 2008)

  12. #10
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QTableView sorting problem

    Quote Originally Posted by noktus View Post
    It only works if only the widget is shown, if I do an exec of a QDialog before or after the widget is shown, when I click in the header driver not loaded error happens.
    Could you post the complete code?

  13. #11
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView sorting problem

    Finally the problem was solved using the QSortFilterProxyModel class that wysota recommends.

    Thanks for your concern janus

    The code that I was using to try was:
    Qt Code:
    1. myDialog dialog;
    2. dialog.exec();
    3.  
    4. formUser frm1;
    5. frm1.show();
    To copy to clipboard, switch view to plain text mode 

    And the formUser only has the table.

  14. #12
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QTableView sorting problem

    @noktus
    Dont mind. it is also concern for my code

    @wysota
    QSortFilterProxyModel helped me as well. Now sorting even works with a queryModel ( I can not use QSqlTableModel because it locks the sqlite database). AND sorting with the QSqlTableModel did change the rowhight (table->resizeRowsToContents() did not work). So thx. This solves two problems I had.

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. Problem with QTableView
    By BoneCollector in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 12:30
  3. Slow problem QTableView
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 26th February 2008, 12:19
  4. problem in printing QTableView
    By miguel_mark in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 08:19
  5. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 16:47

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.