Results 1 to 9 of 9

Thread: QComboBox QSqlQueryModel

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QComboBox QSqlQueryModel

    Dear All

    We setup our combo like this.

    Qt Code:
    1. modKod->setQuery("SELECT kod, id FROM stok_kod UNION SELECT '', null ORDER BY kod");
    2. comboStokKod->setModel(modKod);
    3. comboStokKod->setModelColumn(0);
    4. comboStokKod->setEditable(true);
    To copy to clipboard, switch view to plain text mode 

    But after that we get a value from another query that id = 5, after that I would like to change the combo to value id = 5?. Anyway I could do it?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox QSqlQueryModel

    Try using QAbstractItemModel::match() to find the appropriate QModelIndex.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox QSqlQueryModel

    We normaly do it like

    Qt Code:
    1. combo->setCurrentIndex(combo->findData(sqlquery.value(8)));
    To copy to clipboard, switch view to plain text mode 

    But in this case it should find it from a model?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox QSqlQueryModel

    Well you're interested in searching the model column that combo box is presenting, but another model column, right?
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox QSqlQueryModel

    yes

    what we have in the combo is

    id and the name,

    we show the name and hide the id, but also save the id.

    But when we call the widget we create our combo and fill inside. Afther that we run a query and get the id from the query. We would like to find the same id in the query id.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox QSqlQueryModel

    Quote Originally Posted by aekilic View Post
    what we have in the combo is

    id and the name
    Both? I thought "kod" was the name.

    we show the name and hide the id, but also save the id.
    What do you mean with saving the id? Isn't it already available in the second column?

    Afther that we run a query and get the id from the query. We would like to find the same id in the query id.
    Yeah, that's what I suggested the match() for...
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    aekilic (17th December 2008)

  8. #7
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox QSqlQueryModel

    Let me explain with examples

    Qt Code:
    1. modKod->setQuery("SELECT kod, id FROM stok_kod ");
    2. comboStokKod->setModel(modKod);
    3. comboStokKod->setModelColumn(0);
    4. comboStokKod->setEditable(true);
    To copy to clipboard, switch view to plain text mode 

    We create our combo like this.

    kod1, 1
    kod2, 2
    kod3, 3


    and after that we get another value from another query for the id of the query. So if we have 2 for the id, we try to set the combo text kod2.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox QSqlQueryModel

    Qt Code:
    1. int id = 2;
    2. QModelIndex index = modKod->index(0, 1); // column 1
    3. QModelIndexList result = modKod->match(index, Qt::EditRole, id, 1, Qt::MatchExactly);
    4. int row = result.value(0).row(); // might be -1
    5. comboStokKod->setCurrentRow(row);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    aekilic (17th December 2008)

  11. #9
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox QSqlQueryModel

    Thank you very much guys,

    I have solved it like this

    Thank you jpn...

    Qt Code:
    1. QModelIndexList result = comboStokKod->model()->match(comboStokKod->model()->index(0, 1), Qt::EditRole, query.value(3), 1, Qt::MatchExactly);
    2. comboStokKod->setCurrentIndex(result.value(0).row());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 17:53
  2. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 16:08
  3. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  4. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 04:56
  5. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14

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.