Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QComboBox - Select no entry

QComboBox - Select no entry

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.7k Views
  • 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 Offline
    B Offline
    BentF7
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      michelson
      wrote on last edited by
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved