Results 1 to 5 of 5

Thread: Searching a QTableView for a specific row

  1. #1
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Searching a QTableView for a specific row

    Folks,
    I am trying to create a method that can take a user's input and scan a QTableView object for a matching value in column 1. So far, my code is:
    Qt Code:
    1. void supervisormaint::searchNames(){
    2. QString findName = sui.leSearchText->text().trimmed();
    3. QAbstractItemModel *modl = sui.EmployeesView->model();
    4. QModelIndex curr = sui.EmployeesView->currentIndex();
    5. int rc = modl->rowCount(curr.parent());
    6. QString name;
    7. QVariant vname;
    8. QModelIndex rIndex;
    9. for(int row = 0; row < rc; row++){
    10. rIndex = modl->createIndex (row, 1, 0);
    11. vname = modl->data(rIndex, 0);
    12. name = vname.toString();
    13. if (name.length() > 0 && name.contains(findName) == 0){
    14. sui.EmployeesView->scrollTo(rIndex);
    15. return;
    16. }
    17. }
    18. QMessageBox::information(this,"Name Search:", findName + " NOT found!");
    19. }
    To copy to clipboard, switch view to plain text mode 
    but my stumbling block is
    rIndex = modl->createIndex (row, 1, 0);
    which returns the compiler error:
    'QAbstractItemModel::createIndex' : cannot access protected member declared in class 'QAbstractItemModel'
    It appears that modl allows only for a single item index selected by the user.
    I couldn't find a method to create an index array which would contain all 451 indexes that the view contains.

    How do I do it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Searching a QTableView for a specific row

    The shortest possible object oriented and nice code would be:
    Qt Code:
    1. QString findName = sui.leSearchText->text().trimmed();
    2. QAbstractItemModel *modl = sui.EmployeesView->model();
    3. proxy.setSourceModel(modl);
    4. proxy.setFilterKeyColumn(0);
    5. proxy.setFilterFixedString(findName);
    6. // now the proxy only contains rows that match the name
    7. // let's take the first one and map it to the original model
    8. QModelIndex matchingIndex = proxy.mapToSource(proxy.index(0,0));
    9. if(matchingIndex.isValid(){
    10. // ...
    11. }
    To copy to clipboard, switch view to plain text mode 
    As for your code - simply substitute createIndex() with index() - you don't need to create an index, just ask the model to give you one.

  3. The following 2 users say thank you to wysota for this useful post:

    GreyGeek (6th December 2006), namnguyen (17th February 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re:[SOLVED] Searching a QTableView for a specific row

    Wow, wysota, you must do "Mind Melds" with the API
    I'm going to give you a new nickname: SPOCK!

    Your proxy suggestion was fabulous! Much simpler, more elegant.

    Here is the final version:
    Qt Code:
    1. void supervisormaint::searchNames(){
    2. QString findName = sui.leSearchText->text().trimmed();
    3. QAbstractItemModel *modl = sui.EmployeesView->model();
    4. proxy.setSourceModel(modl);
    5. proxy.setFilterKeyColumn(1);
    6. proxy.setFilterFixedString(findName);
    7. // now the proxy only contains rows that match the name
    8. // let's take the first one and map it to the original model
    9. QModelIndex matchingIndex = proxy.mapToSource(proxy.index(0,0));
    10. if(matchingIndex.isValid()){
    11. sui.EmployeesView->scrollTo(matchingIndex,QAbstractItemView::EnsureVisible);
    12. } else {
    13. QMessageBox::information(this,"Name Search:", "Match not found!");
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to GreyGeek for this useful post:

    namnguyen (17th February 2012)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [SOLVED] Searching a QTableView for a specific row

    Quote Originally Posted by GreyGeek View Post
    Wow, wysota, you must do "Mind Melds" with the API
    Thanks. If I only knew what "Mind melds" were To be honest, I look up to the best. Attending Trolltech DevDays in Munich this year (greetings to all the guys and girls (ok, one girl) I met there!) opened my eyes on some issues.

    I'm going to give you a new nickname: SPOCK!
    I feel my ears growing already. Live long and prosper!

    Your proxy suggestion was fabulous! Much simpler, more elegant.
    And a bit slower

    Note, that you can use the same code to implement a "progressive search" (in the "search when you write" way). You only need to connect a line edit's textChanged() signal to setFilterRegExp() of the proxy model.

  7. #5
    Join Date
    Mar 2013
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Searching a QTableView for a specific row

    Hello all. My doubt is that why do we need QModelIndex when we are setting the QSortFilterProxyModel::setSourceModel. I have tried without using QModelIndex and still I am able to find the string.

    Qt code:
    void filterPane::searchStr()
    {
    QString m_searchString = ui->cQIconLineEdit->text();
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(alarmsModel);
    proxy.setFilterRegExp(QRegExp(m_searchString, Qt::CaseInsensitive,
    QRegExp::FixedString));
    proxy.setFilterKeyColumn(2);
    proxy.setFilterFixedString(m_searchString);

    fp->hideWithEffect();
    alarmPage *al = alarmPage::instance(&frame);
    al->ui->AlarmsTable->setModel(&proxy);
    }
    Last edited by QExcel(&ASAP); 20th March 2013 at 12:56.

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.