Results 1 to 2 of 2

Thread: Setting CurrentIndex of comboBox with model

  1. #1
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Setting CurrentIndex of comboBox with model

    I have combobox with model like this
    Qt Code:
    1. cityModel = new QSqlTableModel(this);
    2. cityModel->setTable("cities"); // (city_id, city_name)
    3. cityModel->select();
    4. comboBox_city->setModel(cityModel);
    5. comboBox_city->setModelColumn(1);
    6. comboBox_city->setCurrentIndex(0);
    To copy to clipboard, switch view to plain text mode 

    I want to change current index of combobox to row of city_id has 15 value?
    Please help me.

    Following line doesnt work.
    Qt Code:
    1. comboBox_city->setCurrentIndex(comboBox_city->findData(query.value(rec.indexOf("city_id"))) );
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 16th November 2007 at 12:02. Reason: missing [code] tags

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

    Default Re: Setting CurrentIndex of comboBox with model

    How about:
    Qt Code:
    1. for(int i=0;i<cityModel->rowCount();i++){
    2. QModelIndex index = cityModel->index(i, rec.indexOf("city_id"));
    3. if(index.data().toInt()==15){
    4. comboBox_city->setCurrentIndex(i);
    5. return;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

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.