Results 1 to 9 of 9

Thread: Sorting columns of the QTableView

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Sorting columns of the QTableView

    Hi all. I need your help please...
    I have a QTableView and use the QSqlQueryModel and the QItemSelectionModel to handle the data. Now I want to enable sorting by clicking on the header. I can enable sorting at the TableView, but the sort method desn't work.

    Does anybody knows how I can sort my QTableView with less effort. I don't want to use the QTableWidget. It would be great, if that works.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting columns of the QTableView

    By default model doesn't know how to sort data in effective way, you have to implement it by overriding QAbstractItemModel::sort.

    Edit: sorry, there is easy solution. Just replace QSqlQueryModel with QSqlTableModel, there is sort method already implemented. You have to just enable sorting in table QTableView::sortingEnabled.
    Last edited by MarekR22; 29th April 2011 at 09:06.

  3. #3
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting columns of the QTableView

    Ok, thank u for your help, but I want to use the setQuery method. And if I use QSqlTableModel this Method isn't available(protected). And the documentation say, that setQuery shouldn't be used.

    Do you know a solution?

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting columns of the QTableView

    Which setQuery do you use? Note that void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase()) is still public, only void QSqlQueryModel::setQuery(const QSqlQuery &query) is protected.

  5. #5
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting columns of the QTableView

    Unfortunately QSqlTableModel has only one setQuery() method and it is this one: QSqlQueryModel::setQuery(const QSqlQuery &query).

    What can I do?

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting columns of the QTableView

    Nope! You are wrong!
    QSqlTableModel inharits form QSqlQueryModel. One version of setQuery is repated in QSqlTableModel to make it protected for some reason. Second version is still public and you can and should use it.

  7. #7
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting columns of the QTableView

    I tried to make that and know that there are two versions, but the compiler doesn't offer the possibility to use it

  8. #8
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting columns of the QTableView

    Does anybody know why???

  9. #9
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sorting columns of the QTableView

    This has been asked a lot and it seems there is no easy or direct answer. HERE

    It seems implementing your own model is the best bet. I prefer sorting via proxy (QSortFilterProxyModel) but DB access speed can make large models sort slowly.

    spawn9997

Similar Threads

  1. QTableview Sorting and hiding
    By hmarani in forum Newbie
    Replies: 8
    Last Post: 28th February 2011, 15:05
  2. QTableView sorting
    By realdarkman71 in forum Newbie
    Replies: 12
    Last Post: 1st December 2010, 22:45
  3. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  4. QTableView sorting
    By Bojan in forum Newbie
    Replies: 2
    Last Post: 28th September 2006, 08:11
  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.