Results 1 to 4 of 4

Thread: Search QTableView with a value(e.g. "ID") which is not shown in the table

  1. #1
    Join Date
    Feb 2016
    Posts
    6
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Search QTableView with a value(e.g. "ID") which is not shown in the table

    Scenario:
    Say, I have a person class
    Qt Code:
    1. class Person{
    2. int id; // only unique value
    3. QString name;
    4. QString address;
    5. QString age;
    6. etc etc
    7. }
    To copy to clipboard, switch view to plain text mode 

    The list of Person class is maintained in class called MyAllData class which is outside of my model class

    The model class I am using; inherits QAbstractTableModel
    Qt Code:
    1. MyCustomModelClass : QAbstractTableModel
    To copy to clipboard, switch view to plain text mode 

    MyCustomModelClass has a reference to the person list. The table does not display the ID number of a person. But it is the only thing with which one can identify a person separately. If I want to search my table data with ID then how can I do that?

  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: Search QTableView with a value(e.g. "ID") which is not shown in the table

    Two options:

    1) Add ID as a column in the model but hide it in the view, then use QAbstractItemModel::match for searching

    2) Implement a search function in your model that gets and ID value and returns the QModelIndex for the respective row

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    6
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Search QTableView with a value(e.g. "ID") which is not shown in the table

    Quote Originally Posted by anda_skoa View Post
    2) Implement a search function in your model that gets and ID value and returns the QModelIndex for the respective row
    _
    I understood Option 1.
    Unfortunately I did not understand Option 2

  4. #4
    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: Search QTableView with a value(e.g. "ID") which is not shown in the table

    For option 2 you would add a method for getting an index of the model for a given ID.

    You could do that outside the model but it is probably more convenient to put it into the model class.

    Something like

    Qt Code:
    1. QModelIndex MyCustomModelClass::indexForID(int id) const
    2. {
    3. for (int i = 0; i < data.count(); ++i) {
    4. if (data.at(i).id == id) {
    5. return index(i, 0);
    6. }
    7. }
    8.  
    9. return QModelIndex();
    10. }
    To copy to clipboard, switch view to plain text mode 
    I.e. find the row the person with the given ID is in, return the model index for that row.
    "data" is your reference to the MyAllData object, assuming that it can be queried for number of entries and can be asked for a specific entry.

    You could also have a findById function in the data class and then use its return value to create the index, etc.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 2
    Last Post: 21st August 2011, 07:57
  3. Replies: 0
    Last Post: 20th September 2010, 09:58
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. Replies: 2
    Last Post: 6th April 2006, 08:21

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.