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

How to set CurrentText of QComboBox?



  • I want to set the text of this widget when no item selected yet. At the beginning my comboBox is empty, then in runtime program adds elements to this box, and i want instantly display in comboBox some string("Elemens inside"+QString::number(i)). But i can't find proper function.

    The

    ui->comboBox->setCurrentText(QString::number(i));
    

    or:

    ui->comboBox->setWindowTitle(QString::number(i));
    

    is not working, i don't know why...

    P.S. i dont need change text of first item, i need only set initial text of the header of that box.



  • @Engelard
    You want to set the text in an editable QComboBox's QLineEdit widget to some string, right? In what way does ui->comboBox->setCurrentText(QString::number(i)); not work? Please be precise.


  • Moderators

    is your Combobox editable? if not, setCurrentText will have no effect

    The setter setCurrentText() simply calls setEditText() if the combo box is editable. Otherwise, if there is a matching text in the list, currentIndex is set to the corresponding index.

    http://doc.qt.io/qt-5/qcombobox.html#currentText-prop


Log in to reply