Results 1 to 8 of 8

Thread: Problem with QTableView and QSortFilterProxyModel

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

    Default Problem with QTableView and QSortFilterProxyModel

    Hi,

    i have a model an tableview.
    I set the model to the table view on the following way:
    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. proxyModel->setFilterKeyColumn(-1);
    7.  
    8. connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
    9. proxyModel, SLOT(setFilterFixedString(QString)));
    10. connect(proxyModel, SIGNAL(hasSelection()),
    11. ui->lineEdit_search_clients, SLOT(setText()));
    12.  
    13.  
    14. model->setQuery("SELECT ROWID, ClientName, ClientCity, ClientEik FROM clients");
    15. model->setHeaderData(0, Qt::Horizontal, QObject::tr("RowID"));
    16. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    17. model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
    18. model->setHeaderData(3, Qt::Horizontal, QObject::tr("ID"));
    19.  
    20.  
    21. ui->tableView_clients->setModel(proxyModel);
    22. //ui->tableView_clients->setSortingEnabled(true);
    23. ui->tableView_clients->setColumnWidth(0,30);
    24. ui->tableView_clients->setColumnWidth(1,170);
    25. ui->tableView_clients->setColumnWidth(2,100);
    26. ui->tableView_clients->setColumnWidth(3,70);
    27. ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    28. ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
    29. ui->tableView_clients->show();
    To copy to clipboard, switch view to plain text mode 

    When i search using QSortFilterProxyModel the content in the TableView changes it's position and doesn't map the model.
    Than when i try to select some row , what i see doesn't respond of what i select.

    This is the selection:

    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 

    Please HELP!!!

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTableView and QSortFilterProxyModel

    To get the correct items from the model you will have to use proxyModel->mapToSource() else you will get the row numers that the proxy uses

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

    Default Re: Problem with QTableView and QSortFilterProxyModel

    I don't know in which of the code i should use it?The proxyModel is declared in updateCleintTable()
    and i want to get the row value in on_pushButton_select_clicked()
    Last edited by unix7777; 21st August 2012 at 19:58.

  4. #4
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTableView and QSortFilterProxyModel

    can you attach the full code then it is easyer to explain

    form the previous piece of code you should change your selecion code
    Qt Code:
    1. selected_row=proxyModel->mapToSource(selectedList.at(i)).row();
    To copy to clipboard, switch view to plain text mode 
    change proxyModel to the name of your proxy model

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

    Default Re: Problem with QTableView and QSortFilterProxyModel

    Thank you for answer so quick.
    The proxyModel is declared in another place.

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

    And another one:

    Qt Code:
    1. void Clients::on_pushButton_select_clicked()
    2. {
    3.  
    4. QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
    5.  
    6.  
    7. int selected_row;
    8. int i=0;
    9. selected_row=selectedList.at(i).row();
    10.  
    11. mod->setQuery("SELECT * FROM clients");
    12.  
    13. QString ClientName = mod->data(mod->index(selected_row, 0)).toString();
    14. QString ClientCity = mod->data(mod->index(selected_row, 1)).toString();
    15. QString ClientAddress = mod->data(mod->index(selected_row, 2)).toString();
    16. QString ClientMol = mod->data(mod->index(selected_row, 3)).toString();
    17. QString ClientEik = mod->data(mod->index(selected_row, 4)).toString();
    18. QString ClientVat = mod->data(mod->index(selected_row, 5)).toString();
    19.  
    20. //QMessageBox::information(this,"", ClientMol);
    21.  
    22. emit selectedName(ClientName);
    23. emit selectedCity(ClientCity);
    24. emit selectedAddress(ClientAddress);
    25. emit selectedMol(ClientMol);
    26. emit selectedEik(ClientEik);
    27. emit selectedVat(ClientVat);
    28. //emit selectedRaw(QString::number(ClientId));
    29.  
    30. this->close();
    31.  
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTableView and QSortFilterProxyModel

    Then ask the view for the proxymodel
    ui->tableView_clients->model(); //this will return the proxy model that you set in your view

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

    Default Re: Problem with QTableView and QSortFilterProxyModel

    Thank you for the answer, but is seems that ui->tableView_clients->model() doesn't return the proxyModel because there is no function mapToSource()


    Added after 24 minutes:


    I just have write this
    Qt Code:
    1. mod->setSourceModel(ui->tableView_clients->model());
    To copy to clipboard, switch view to plain text mode 
    and it works, i just have to change the indexes of the cells.

    THANK YOU VERY MUCH FOR YOUR HELP!!!
    Last edited by unix7777; 23rd August 2012 at 19:06.

  8. #8
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTableView and QSortFilterProxyModel

    mod->setSourceModel(ui->tableView_clients->model()); this is not the right way to do it

    ui->tableView_clients->model() returns a QAbstractModel you have to cast it into a QProxyModel like this:
    Qt Code:
    1. QProxyModel * tmpProxy;
    2. if(tmpProxy = qobject_cast<QProxyModel *>(ui->tableView_clients->model()))
    3. {
    4. // here you can use the tmpProxy to mapToSource
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Moving Items in QTableView with QSortFilterProxyModel
    By Rudolf Rendier in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2012, 15:07
  2. Replies: 6
    Last Post: 22nd November 2011, 03:53
  3. Replies: 1
    Last Post: 18th November 2009, 23:21
  4. Problem QSortFilterProxyModel
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2009, 18:51
  5. QSortFilterProxyModel problem
    By blukske in forum Qt Programming
    Replies: 8
    Last Post: 22nd June 2006, 08:26

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.