Results 1 to 3 of 3

Thread: QComboBox -> setModel -> Strange behaviour

  1. #1
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default QComboBox -> setModel -> Strange behaviour

    Hi,

    If I associate a QComboBox to a table with setModel, then I can only manually add one item to this combobox. If I add 2 items, only one empty item appears at the end of the list.
    How can I do? (I don't want to put the missing items into the database, I want them to be added dynamically).

    Here is the source code:

    Qt Code:
    1. myModel = new QSqlRelationalTableModel();
    2. myModel->setTable("woundType");
    3. myModel->select();
    4. typeCbx->setModel(myModel);
    5. typeCbx->setModelColumn(2);
    6. typeCbx->insertItem(2, QString(""));
    7. typeCbx->addItem(QString("Ajout/Modification/Suppression"));
    8. typeCbx->addItem(QString("Others")); //if I add this item, only one empty line appears at the end of the combobox
    To copy to clipboard, switch view to plain text mode 

    Thanks for yout help
    Regards,
    Oscar
    Last edited by jpn; 13th August 2008 at 14:41. 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: QComboBox -> setModel -> Strange behaviour

    In general you have to decide whether you want to use the model approach or the manual approach, mixing both is not advised. In your situation I'd suggest inserting those items from the database manually into the widget. Otherwise implement a simple proxy model around your model (or subclass the model you are using) to unite both the model data and the manual data into a single model.

  3. #3
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: QComboBox -> setModel -> Strange behaviour

    Thanks for your reply.
    so, I finally chose the manual approach like this:

    Qt Code:
    1. QSqlQuery query;
    2. QString queryStr = QString("select id,value from woundType order by displayOrder");
    3. query.exec(queryStr);
    4. if (!query.isActive())
    5. Logger::log(Logger::fatalLevel, QString("200808131608: ") + qPrintable(query.lastError().text()));
    6.  
    7. typeCbx->addItem("");
    8.  
    9. while (query.next()) {
    10. typeCbx->addItem(query.value(1).toString());
    11. typeCbx->setItemData(typeCbx->count()-1, query.value(0).toInt(), Qt::UserRole);
    12. }
    13. typeCbx->addItem("Ajout/Modification/Suppression");
    14. typeCbx->addItem("Others");
    To copy to clipboard, switch view to plain text mode 


    which is the only one which really suits my needs but the counter part is more code to maintain...

    the use of proxy is indeed maybe the best solution, I'll have a look.

    All the best,
    Oscar

Similar Threads

  1. qinputdialog strange behaviour
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2008, 19:29
  2. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38
  3. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49
  4. Replies: 1
    Last Post: 26th February 2006, 05:52
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

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.