Results 1 to 8 of 8

Thread: QSortFilterProxyModel nothing changes

  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 08:49. Reason: change all my anwser.

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

    Default Re: QSortFilterProxyModel nothing changes

    Thank you!

    Thank you!


    Added after 5 minutes:


    A subquestion of my question:
    When i get selected row from the model i use it on the following way:
    Qt Code:
    1. QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
    2.  
    3.  
    4.  
    5. int selected_row;
    6. int i=0;
    7. selected_row=selectedList.at(i).row();
    8.  
    9. mod->setQuery("SELECT * FROM clients");
    10.  
    11. QString ClientName = mod->data(mod->index(selected_row, 0)).toString();
    12. QString ClientCity = mod->data(mod->index(selected_row, 1)).toString();
    13. QString ClientAddress = mod->data(mod->index(selected_row, 2)).toString();
    14. QString ClientMol = mod->data(mod->index(selected_row, 3)).toString();
    15. QString ClientEik = mod->data(mod->index(selected_row, 4)).toString();
    16. QString ClientVat = mod->data(mod->index(selected_row, 5)).toString();
    17.  
    18. //QMessageBox::information(this,"", ClientMol);
    19.  
    20. emit selectedName(ClientName);
    21. emit selectedCity(ClientCity);
    22. emit selectedAddress(ClientAddress);
    23. emit selectedMol(ClientMol);
    24. emit selectedEik(ClientEik);
    25. emit selectedVat(ClientVat);
    26. //emit selectedRaw(QString::number(ClientId));
    27. this->close();
    To copy to clipboard, switch view to plain text mode 

    The problem is that when searching the order of appearance is changing and the selected row i get already doesn't much the row selected we see.
    Last edited by unix7777; 18th August 2012 at 10:26.

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

    Default Re: QSortFilterProxyModel nothing changes

    Does nobody have an idea why the model doesn't correspond to the view after searching with QSortFilterProxyModel?

Similar Threads

  1. Qsortfilterproxymodel
    By migel in forum Newbie
    Replies: 1
    Last Post: 3rd October 2011, 17:40
  2. QSortfilterProxyModel and Treeview
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 27th March 2009, 13:36
  3. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 11:28
  4. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 12: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.