Important: Please read the Qt Code of Conduct -

access selected value of QcomboBox problem

  • I set a QcomboBox in gui widget and ,I add item

    for(int i = 1; i < 31; i++)

    and in QComboBox slot I want to get selected value by

    int index =ui->combo->itemData( ui->combo->currentText());

    but have error :316: error: no matching function for call to 'QComboBox::itemData(QString)'
    how can get slected value??

  • Lifetime Qt Champion


    You should use currentIndex not currentText. itemData expects an int.

  • addItem get Qstring ,

    void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())

    and when i use current index instead currentText return 0
    so how can I set 1 to 31 number to comboBox and get selected value correctly??

  • Hi,
    By using below code you can get seleted value in Qstring format.
    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    cbox = new QComboBox;
    glayout = new QGridLayout(this);
    QStringList list;
    for(int i=0;i<31;i++){

    void Widget:: SLTcurrentText(QString str){
    qDebug()<<"CurrentText ::"<<str<<endl;

  • tnx it's work

Log in to reply