access selected value of QcomboBox has problem
I set a QcomboBox in gui widget and ,I add item
Code:
for(int i = 1; i < 31; i++)
{
ui
->combo
->addItem
(QString::number(i
));
}
and in QComboBox slot I want to get selected value by
Code:
int index =ui->combo->itemData( ui->combo->currentText());
but have error :316: error: no matching function for call to 'QComboBox::itemData(QString)'
if I use currentIndex instead of currentText return 0 when print it; addItem get Qstring ,
and ItemData work with currentIndex,
I use insertItem and it has sae error ,so how can set value or text and get slected value??
Re: access selected value of QcomboBox has problem
1. You have to set the item value :
Code:
for(int i = 1; i < 31; i++)
{
ui
->combo
->addItem
(QString::number(i
),i
);
}
2. To get selected item value :
Code:
int value = ui->combo->itemValue(ui->combo->currentIndex());
Re: access selected value of QcomboBox has problem
By using below code I can get seleted value in Qstring format.
Code:
{
glayout->addWidget(cbox);
for(int i=0;i<31;i++){
}
cbox->addItems(list);
connect(cbox,
SIGNAL(currentTextChanged
(QString)),
this,
SLOT(SLTcurrentText
(QString)));
}
void Widget
:: SLTcurrentText(QString str
){ qDebug()<<"CurrentText ::"<<str<<endl;
}
I should use SIGNAL(currentTextChanged(QString)
tnx..
Re: access selected value of QcomboBox has problem
That's also an option since your text is just a number.
Lesiok answered your original question on how to use per-item data.
Cheers,
_