Results 1 to 7 of 7

Thread: Find data in QCombobox invisible column

  1. #1
    Join Date
    Jun 2013
    Location
    Northern Rivers, NSW Australia
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Find data in QCombobox invisible column

    Hi all,

    I'm trying to find a method of selecting an item of a combo box populated from a SQL query model.

    Qt Code:
    1. query1->setQuery("SELECT ID, Name FROM Cat order by Name");
    2. myQComboBox->setModel(query1);
    3. myQComboBox->setModelColumn(1);
    To copy to clipboard, switch view to plain text mode 

    Now I want to find data in the invisible column 0 that matches a given ID so I can set the combo box text from the index that's returned from the found record. I can't find a way of doing this. Is it possible ?

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

    Default Re: Find data in QCombobox invisible column

    You iterate through the model and access its data using the data() method.
    Once you've found what you are looking for you take the row() value of the model index and use setCurrentIndex() on the combobox.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Gretsch (29th June 2013)

  4. #3
    Join Date
    Jun 2013
    Location
    Northern Rivers, NSW Australia
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Find data in QCombobox invisible column

    I worked out a solution:
    Qt Code:
    1. QAbstractItemModel * model = myQComboBox->model();
    2. int ndx = 0;
    3. for (int i = 0; i < model->rowCount(); ++i)
    4. {
    5. index = myQComboBox->model()->index(i,0);
    6. if (model->data(index)==id)
    7. {
    8. ndx = i;
    9. break;
    10. }
    11. }
    12. myQComboBox->setCurrentIndex(ndx);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Find data in QCombobox invisible column

    You can also use QAbstractItemModel::match()

  6. The following user says thank you to ChrisW67 for this useful post:

    Gretsch (1st July 2013)

  7. #5
    Join Date
    Jun 2013
    Location
    Northern Rivers, NSW Australia
    Posts
    12
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Find data in QCombobox invisible column

    Quote Originally Posted by ChrisW67 View Post
    You can also use QAbstractItemModel::match()
    That's handy to know, it's a one liner compared to a for...loop, not sure which method would be more efficient though.

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

    Default Re: Find data in QCombobox invisible column

    match() can search the entire column in table or tree from an arbitrary starting index, optionally wrapping, so it is probably more involved internally.

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Find data in QCombobox invisible column

    Efficiency probably depends on the model's implementation of match. Since it is virtual a model could have a non-linear way of looking for the value, e.g. some form of index or lookup-table.

    It also doesn't necessarily have to go through data() every time, which, as a public method, has to check its arguments for validity on every call.

    The explicit loop allows more flexibility on how the matching is done though, e.g. how to compare the values, whether to do any weighting of match results, etc.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 28th May 2012, 15:06
  2. Replies: 5
    Last Post: 26th April 2010, 06:59
  3. QTableView column header is invisible
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2009, 12:04
  4. how to add column in QComboBox??
    By anupamgee in forum Qt Programming
    Replies: 7
    Last Post: 25th September 2009, 15:10
  5. How to find which column clicked from a treeview.
    By mekos in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 16:44

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.