access selected value of QcomboBox problem



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

    for(int i = 1; i < 31; i++)
            {
                ui->combo->addItem(QString::number(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

    Hi,

    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);
    glayout->addWidget(cbox);
    QStringList list;
    for(int i=0;i<31;i++){
    list.append(QString::number(i));
    }
    cbox->addItems(list);
    connect(cbox,SIGNAL(currentTextChanged(QString)),this,SLOT(SLTcurrentText(QString)));
    }

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



  • tnx it's work


Log in to reply
 

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