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?? -
Hi,
You should use
currentIndex
notcurrentText
.itemData
expects anint
. -
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;
}