Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QComboBox - Select no entry

    General and Desktop
    3
    3
    1468
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      BentF7 last edited by

      I have a QComboBox on my ui and set the model like this:

      QStringListModel* model = new QStringListModel;
      QStringList stringlist;
      stringlist << "Test1" << "Test2" << "Test3";
      
      model->setStringList(stringlist);
      ui->comboBox->setModel(model);
      

      Now I want to change the current index to be none (so that I get a blank combo box).

      I already tried setting the current index to -1 with

      ui->comboBox->setCurrentIndex(-1);
      

      But that results to an index aout of range exeption in qlist:

      ASSERT failure in QList<T>::operator[]: "index out of range", file F:/Qt/5.4/mingw491_32/include/QtCore/qlist.h, line 486
      
      1 Reply Last reply Reply Quote 0
      • M
        michelson last edited by

        I not sure but i think thats your model fault. Looks like you since you want to use your own model you have to specify what to do with -1 index, couse it seems it is not implemented by default. Im guessing you really have to use custom QStringListModel but for such trivial use why just use default QComboBox and add items by QComboBox::addItems(stringlist) ? (just a sugestion)

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi and welcome to devnet,

          AFAIK, you need to have that blank element as part of your data or implement your own model to handle that use case.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • First post
            Last post