Results 1 to 6 of 6

Thread: Merge 2 QSortFilterProxyModel

  1. #1

    Default Merge 2 QSortFilterProxyModel

    Hi,

    i have a QSqlQueryModel with about 5000 records with 4 columns.

    I want to filter the contents of column 1 of the source model respect a fixedstring specified in a QLineEdit.

    Qt Code:
    1. QString searchText = ui->searchBar->text().trimmed();
    2. MySortFilterProxyModel *proxy = new MySortFilterProxyModel(0);
    3. proxy->setSourceModel(model);
    4. proxy->setFilterKeyColumn(1);
    5. proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    6. proxy->setFilterFixedString(searchText);
    To copy to clipboard, switch view to plain text mode 

    The task that i can't figure out is:
    I want to sort results respect two categories:
    - Results that start with searchText
    - All other results

    Is it possible to merge two QSortFilterProxyModel?

    Please help. Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Merge 2 QSortFilterProxyModel

    While you can stack proxy models, why not implement your proxy's lessThan() method so that it takes the search criteria into account?

    Cheers,
    _

  3. #3

    Default Re: Merge 2 QSortFilterProxyModel

    I tried implementing my lessThan() function and it works.

    The problem is the slowness of the function.

    My situation is (supposed i want to search 'E' caseinsensitive):

    row - string

    0001 - row1E
    0002 - row2 E row2
    0003 - Erow3
    0004 - row4E
    0005 - E row5

    and i want to sort in:

    row - string

    0003 - Erow3
    0005 - E row5
    0001 - row1E
    0002 - row2 E row2
    0004 - row4E

    I just need to put on the top the row that start with E without change any other order;

    Is there a way to speed up this process?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Merge 2 QSortFilterProxyModel

    You could use two proxies, one only including the rows with E and one excluding the rows with E and then creating a new model that just operates on the two proxies.

    Alternatively write a model that uses QSqlQuery for the database access and keeps its own mapping of model rows to DB rows.
    It could the just move the matching rows to the front of its internal list.

    Cheers,
    _

  5. #5

    Default Re: Merge 2 QSortFilterProxyModel

    Can you explain me something more about creating a new model that just operates on the two proxies.

    i have done what you say with
    Qt Code:
    1. MySortFilterProxyModel *proxy = new MySortFilterProxyModel(0);
    2. proxy->setSourceModel(model);
    3. proxy->setFilterKeyColumn(1);
    4. proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    5. proxy->setFilterFixedString(searchText);
    6.  
    7. MySortFilterProxyModel *proxy1 = new MySortFilterProxyModel(1);
    8. proxy1->setSourceModel(model);
    9. proxy1->setFilterKeyColumn(1);
    10. proxy1->setFilterCaseSensitivity(Qt::CaseInsensitive);
    11. proxy1->setFilterFixedString(searchText);
    To copy to clipboard, switch view to plain text mode 

    but i can't figure out how to "append" proxy1 to proxy...

    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Merge 2 QSortFilterProxyModel

    You create a new model by deriving from QAbstractTableModel and then implementing rowCount(), columnCount() and data().

    columnCount() is the columnCount() of either source model.
    rowCount() is obviously the sum of the source models.
    data() then needs to check which row it gets, create an index for the appropriate sub model with the same column and call data with the new index and the same role.

    Cheers,
    _

Similar Threads

  1. How to merge two QPixmaps in to one.
    By live_07 in forum Qt Programming
    Replies: 2
    Last Post: 23rd September 2009, 09:19
  2. How to merge qpixmaps in qt4.5
    By newqtuser in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2009, 15:59
  3. How to merge two QPixmaps in to one.
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 17th August 2008, 14:59
  4. how to merge QPixmaps
    By tommy in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2008, 13:02

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
  •  
Qt is a trademark of The Qt Company.