Results 1 to 7 of 7

Thread: Select specific item in QListView

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Select specific item in QListView

    I have a QListView which contents I said with a Model. When resetting the model I need to reselect the previous item but I'm not sure how to search for it and than select it.
    This is how I set the model:

    Qt Code:
    1. void IMClientGui::setActiveUsers(QSqlQueryModel *sqm) {
    2. QString currentUser = this->getSelectedHost();
    3. ui->clientsOnline->setModel(sqm);
    4. //here the old selection should be reset if this entry is still in the list
    5. }
    6.  
    7. QString IMClientGui::getSelectedHost() {
    8. auto itemselectmodel = ui->clientsOnline->selectionModel();
    9.  
    10. if (itemselectmodel->selectedIndexes().size() > 0) {
    11. QModelIndex index = itemselectmodel->selectedIndexes().at(0);
    12. return index.data().toString();
    13. } else
    14. return "";
    15. }
    To copy to clipboard, switch view to plain text mode 

    I cannot work with the indexes here (e.g. if item with index 3 was selected reset to index 3) since a previous item might have been removed and therefore the index could be less or more if an item was added

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

    Default Re: Select specific item in QListView

    See QAbstractItemModel::match() for finding a model index with certain data.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Select specific item in QListView

    I'm not sure how to use this method, what exactly am i supposed to put as QModelIndex and role? And how do I define the QVariant, in my case the displayed values are all QStrings?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Select specific item in QListView

    Qt Code:
    1. QModelIndexList indexes = model->match(
    2. model->index(0,0), // first row, first column
    3. Qt::DisplayRole, // search the text as displayed
    4. stringValue, // there is a QVariant(QString) conversion constructor
    5. 1, // first hit only
    6. Qt::MatchExactly // or Qt::MatchFixedString
    7. );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Select specific item in QListView

    When executing this

    Qt Code:
    1. auto model = ui->clientsOnline->selectionModel()->model();
    2.  
    3. QModelIndexList indexes = model->match(
    4. model->index(0,0), // first row, first column
    5. Qt::DisplayRole, // search the text as displayed
    6. currentUser, // there is a QVariant(QString) conversion constructor
    7. 1, // first hit only
    8. Qt::MatchExactly // or Qt::MatchFixedString
    9. );
    To copy to clipboard, switch view to plain text mode 

    I get a Segmentation fault!?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Select specific item in QListView

    So model is 0 or invalid. Why are you going via the selection model anyway?

    Something like this:
    When the data model emits modelAboutToBeReset() you want to store the current value selected in the list view.
    Qt Code:
    1. const QModelIndex currentIndex = ui->clientsOnline->currentIndex(); // may be an invalid index
    2. m_previouslySelected = (currentIndex.isValid())? currentIndex->data(Qt::DisplayRole): QVariant();
    To copy to clipboard, switch view to plain text mode 
    When the data model later emits modelReset() find and restore the list view's current item if possible:
    Qt Code:
    1. if (!m_previouslySelected.isNull()) {
    2. const QAbstractItemModel *model = ui->clientsOnline->model();
    3. const QModelIndexList indexes = model->match(
    4. model->index(0,0),
    5. Qt::DisplayRole,
    6. m_previouslySelected,
    7. 1, // first hit only
    8. Qt::MatchExactly
    9. );
    10. if (index.size() > 0) { // found a match
    11. ui->clientsOnline->setCurrentIndex(indexes.at(0));
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Select specific item in QListView

    Worked thanks

Similar Threads

  1. Replies: 9
    Last Post: 31st August 2015, 16:00
  2. Select QListView Item on MouseOver
    By renevolution in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2015, 11:25
  3. Replies: 10
    Last Post: 22nd June 2013, 00:05
  4. QListView - To select a disabled item
    By agarny in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2011, 01:23
  5. QListView: How to move the cursor to a specific row
    By muellerp in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2008, 07:29

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.