QComboBox - Select no entry



  • 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
    


  • 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)


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.