Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox - setCurrentText???



  • I can "getCurrentText". Why not be able to "set" current text as if the user just typed it. I don't want to add it to the drop down list, just change the current text. Is that possible?



  • This should work:

    @
    QString textToFind = "abc";
    int index = ui->comboBox->findText(textToFind)
    ui->comboBox->setCurrentIndex(index);
    @

    There is no setter, as the texts need not be distinct, thus the setter would not know which index to make the current.



  • please correct me if I got confused here.

    If "abc" isn't found the index returned by "findText" is -1 in which case, the setCurrentIndex will not work. Your code only works if "abc" already exists somewhere within the dropdown items, setting item "0" (the currently shown) to it.

    My intention was to programmatically simulate a situation where a user clicks on the edit-box portion of the drop down, and types something, regardless of whether it exists in the dropdown items, which brings me to this question: will a user-typing actually change the "item 0" in real time?



  • You are right in your analysis. You can get access to the embedded line edit in the combo box...



  • Yes. Thanks Volker and Andre. I now see the:

    @QLineEdit * lineEdit () const@



  • Maybe "QComboBox::setItemText() ":http://doc.qt.nokia.com/4.7/qcombobox.html#setItemText in pair with currentIndex () fits your need too.


Log in to reply