Results 1 to 6 of 6

Thread: Using QSortFilterProxyModel with QTableWidgets

  1. #1
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using QSortFilterProxyModel with QTableWidgets

    Hello,

    I'm creating a database program, which contains one QTableWidget "tableSongs" that displays the whole data of a database.

    It has 6 columns: 4 of them contain numbers, one contains text string and the last one contains sets of chars like "1A", "1B", "2A", "32G" or "111E" - it consists of a number (1-3 digits) and one upcase letter.

    What I want to do is to add sorting and filtering functionality - columns with numbers should be sorted using numbers' value, column with strings - alphabetically and the last one like that: "1A" < "1B" but "9Z" < "19Z".

    I thought that QSortFilterProxyModel would be a good class to use here.
    I created second QTableWidget "tableSource" but didn't placed in on the window. I'm inserting rows and adding items to tableSource and I want QSortFilterProxyModel to show them in tableSongs after doing the sorting and filtering. But I have problems with doing so. Here is my code:

    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(tableSource.model());
    tableSongs->setModel(proxyModel);
    But it doesn't work. How should I correct it?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using QSortFilterProxyModel with QTableWidgets

    You do not need to create a second view.

    This is the basic schematic:
    Qt Code:
    1. +--------------+ +-----------------------+ +----------+
    2. | Source model +---+ Proxy model (sorting) +---+ The view |
    3. +--------------+ +-----------------------+ +----------+
    To copy to clipboard, switch view to plain text mode 

    However, I think you'll need to create your own sorting routine as the items you want to sort are strings, and they sort differently than integers.

  3. #3
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QSortFilterProxyModel with QTableWidgets

    But is there as convenient solution basing on a model as using QTableWidget which has all of these "insert/removeRow", "clearContents" slots?

    The sorting will be the next problem, but at the moment my problem is the communicate, which I receive after trying to compile:
    Qt Code:
    1. proxyModel = new QSortFilterProxyModel(this);
    2. proxyModel->setSourceModel(tableSource.model());
    3. ui.tableSongs->setModel(proxyModel);
    To copy to clipboard, switch view to plain text mode 

    The communicate is as follows:
    ...src/gui/itemviews/qtablewidget.h:338: error: 'virtual void QTableWidget::setModel(QAbstractItemModel*)' is private
    To be accurate, my "tableSongs" is not pure QTableWidget - I have promoted it to "upgTableWidget" - QTableWidget subclass. Can it be the reason of the error?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using QSortFilterProxyModel with QTableWidgets

    Is there any reason why you won't use QTableView with a QStandardItemModel or any other model?

  5. #5
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QSortFilterProxyModel with QTableWidgets

    Instead of "tableSongs"?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using QSortFilterProxyModel with QTableWidgets

    Quote Originally Posted by kremuwa View Post
    Instead of "tableSongs"?
    yes, because then you wouldn't have the problem with a private model handling. And beside model/view is the higher approach then using *Widget.

Similar Threads

  1. Replies: 2
    Last Post: 12th May 2010, 14:27
  2. Qsortfilterproxymodel in a Qthread?
    By calmspeaker in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2008, 17:16
  3. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 10:28
  4. QTableWidgets problems with columns
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 08:47
  5. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 11:53

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.