Results 1 to 8 of 8

Thread: Sorting QTableView

  1. #1
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Sorting QTableView

    I am trying to extend the QTableView class to create a table that sorts by column when the column header is clicked. So far the only way I can see to do this is by connecting the QTableView's horizontalHeader's sectionClicked() signal to the QTableView's sortByColumn() slot, but nothing seems to happen when I click the column header other than the sortIndicator changing. How can I get the column to sort, how can I specify how the column should be sorted (ascending vs. descending)?

    Here is the constructor for my implementation of QTableView. This is where I am connecting the signal to the slot. I am new to QT so it is quite possible I am doing this completely wrong. Any advice is appreciated.

    Qt Code:
    1. MyTableView::MyTableView(QWidget *parent) : QTableView(parent)
    2. {
    3. this->setDragEnabled(false);
    4. this->setAlternatingRowColors(true);
    5. this->setAcceptDrops(false);
    6.  
    7. QHeaderView* rowHeader = this->verticalHeader();
    8. rowHeader->hide();
    9.  
    10. QHeaderView* columnHeader = this->horizontalHeader();
    11. columnHeader->setClickable(true);
    12. columnHeader->setSortIndicatorShown(true);
    13. columnHeader->setSortIndicator(0, Qt::AscendingOrder);
    14.  
    15. QObject::connect(columnHeader, SIGNAL(sectionClicked(int)), this, SLOT(sortByColumn(int)));
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting QTableView

    The problem might be in model:
    void QAbstractItemModel::sort ( int column, Qt::SortOrder order = Qt::AscendingOrder ) [virtual]
    Sorts the model by column in the given order.
    The base class implementation does nothing.
    Also read this.

  3. #3
    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: Sorting QTableView

    You should use a proxy model, like so:

    Qt Code:
    1. QTableView *view = new QTableView(...);
    2. md->setSourceModel(model);
    3. view->setModel(md);
    4. md->sort(0);
    5. view->header()->setSortIndicator(0, Qt::AscendingOrder);
    6. view->header()->setSortIndicatorShown(true);
    7. view->header()->setClickable(true);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting QTableView

    Yes - I suppose it would help a great deal if I actually implemented the sorting function.

    Thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting QTableView

    Quote Originally Posted by wysota
    You should use a proxy model, like so:

    Qt Code:
    1. QTableView *view = new QTableView(...);
    2. md->setSourceModel(model);
    3. view->setModel(md);
    4. md->sort(0);
    5. view->header()->setSortIndicator(0, Qt::AscendingOrder);
    6. view->header()->setSortIndicatorShown(true);
    7. view->header()->setClickable(true);
    To copy to clipboard, switch view to plain text mode 
    Is your example complete or have you left out important details?:
    from the Assistant:
    When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data().

  6. #6
    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: Sorting QTableView

    It's complete (not counting the dots as constructor parameters). Take a closer look at the code -- I create a QStandardItemModel object and not a QAbstractItemModel one.

  7. #7
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting QTableView

    Quote Originally Posted by wysota
    It's complete (not counting the dots as constructor parameters). Take a closer look at the code -- I create a QStandardItemModel object and not a QAbstractItemModel one.
    I thought I did:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    Isn't QStandardItemModel a subclass of QAbstractItemModel?

    And, what is *model a subclass of?
    QAbstractItemModel *model
    or
    *model = new QStandardItemModel?

    When you mix classes like that which one is the "winner"?
    Does it matter as long as the one on the right is a subclass of the one on the left?

  8. #8
    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: Sorting QTableView

    Qt Code:
    1. class A{};
    2. class B : public class A{};
    3. classA * obj = new classB();
    4. // is equal to:
    5. class B *tmp = new classB();
    6. classA *obj = (obj*)tmp;
    To copy to clipboard, switch view to plain text mode 

    As long as class B inherits class A, you make make such a cast right? And all virtual functions from "obj" will be taken from classB, while all non virtual ones that were defined in classA will be taken from classA.

    Because I don't need any features of standard item model while using the proxy, I can cast the pointer to its base class which is needed by the proxy. This was my way of showing that it works with any item model inherited from QAbstractItemModel (I couldn't instantiate a QAbstractItemModel object, as it is pure abstract, like you noted).

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. Refresh QTableView after sorting
    By araglin in forum Newbie
    Replies: 4
    Last Post: 18th December 2008, 22:13
  3. QTableView sorting when using a model
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 13:13
  4. QTableView sorting problem
    By noktus in forum Newbie
    Replies: 11
    Last Post: 23rd April 2008, 10:20
  5. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 13:22

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.