Results 1 to 8 of 8

Thread: QSortFilterProxyModel nothing changes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSortFilterProxyModel nothing changes

    Hi i'm trying to filter the content displayed in the TableView getting the string from lineEdit but nothing happens when i write in the lineEdit.
    HELP

    Qt Code:
    1. QString searchValue = ui->lineEdit_search_clients->text();
    2.  
    3.  
    4. proxyModel->setSourceModel(model);
    5. proxyModel->setFilterRegExp(QRegExp(searchValue, Qt::CaseInsensitive, QRegExp::FixedString));
    6.  
    7. model->setQuery("SELECT ROWID, ClientName, ClientCity, ClientEik FROM clients");
    8. model->setHeaderData(0, Qt::Horizontal, QObject::tr("RowID"));
    9. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    10. model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
    11. model->setHeaderData(3, Qt::Horizontal, QObject::tr("ID"));
    12.  
    13.  
    14. ui->tableView_clients->setModel(proxyModel);
    15. ui->tableView_clients->setSortingEnabled(true);
    16. ui->tableView_clients->setColumnWidth(0,30);
    17. ui->tableView_clients->setColumnWidth(1,170);
    18. ui->tableView_clients->setColumnWidth(2,100);
    19. ui->tableView_clients->setColumnWidth(3,70);
    20. ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    21. ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
    22. ui->tableView_clients->show();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel nothing changes

    If you write in lineEdit after this code has been run then it will not change anything. You have to call setFilterRegExp() again (or, better yet, setFilterFixedString() in your case) after the lineEdit's text has been modified. Also remember that QSortFilterProxyModel filters column 0 of the model by default, which may or may not be what you want.

    By the way your code leaks memory in two places.

  3. #3
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSortFilterProxyModel nothing changes

    Thanks, now the code looks like this:

    Qt Code:
    1. QString searchValue = ui->lineEdit_search_clients->text();
    2. QSqlQueryModel *model=new QSqlQueryModel(this);
    3.  
    4. proxyModel->setSourceModel(model);
    5. proxyModel->setFilterRegExp(QRegExp(searchValue, Qt::CaseInsensitive, QRegExp::FixedString));
    6.  
    7. connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
    8. proxyModel, SLOT(setFilterFixedString(QString)));
    9.  
    10. model->setQuery("SELECT ROWID, ClientName, ClientCity, ClientEik FROM clients");
    11. model->setHeaderData(0, Qt::Horizontal, QObject::tr("RowID"));
    12. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    13. model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
    14. model->setHeaderData(3, Qt::Horizontal, QObject::tr("ID"));
    15.  
    16.  
    17. ui->tableView_clients->setModel(proxyModel);
    18. ui->tableView_clients->setSortingEnabled(true);
    19. ui->tableView_clients->setColumnWidth(0,30);
    20. ui->tableView_clients->setColumnWidth(1,170);
    21. ui->tableView_clients->setColumnWidth(2,100);
    22. ui->tableView_clients->setColumnWidth(3,70);
    23. ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    24. ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
    25. ui->tableView_clients->show();
    To copy to clipboard, switch view to plain text mode 

    Remain the problem with filtering not only first column but everywhere.Any suggestions?

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel nothing changes

    QSortFilterProxyModel has a method to select which column to filter: either a specific one or all of them.

  5. #5
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSortFilterProxyModel nothing changes

    sorry , but i can't figure out how to filter ALL without subclassing!

  6. #6
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel nothing changes

    add the code
    Qt Code:
    1. proxymodel->setFilterKeyColumn ( -1 )
    To copy to clipboard, switch view to plain text mode 

    the -1 indicate read the value in all the column.
    Last edited by ecanela; 18th August 2012 at 07:49. Reason: change all my anwser.

Similar Threads

  1. Qsortfilterproxymodel
    By migel in forum Newbie
    Replies: 1
    Last Post: 3rd October 2011, 16:40
  2. QSortfilterProxyModel and Treeview
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 27th March 2009, 12:36
  3. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 10:28
  4. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 11:53

Tags for this Thread

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.