PDA

View Full Version : access selected value of QcomboBox has problem



isan
26th April 2016, 07:04
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)'

if I use currentIndex instead of currentText return 0 when print it; addItem get Qstring ,


void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant())
and ItemData work with currentIndex,

I use insertItem and it has sae error ,so how can set value or text and get slected value??

Lesiok
26th April 2016, 08:33
1. You have to set the item value :
for(int i = 1; i < 31; i++)
{
ui->combo->addItem(QString::number(i),i);
}
2. To get selected item value :
int value = ui->combo->itemValue(ui->combo->currentIndex());

isan
1st May 2016, 15:38
By using below code I 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)),t his,SLOT(SLTcurrentText(QString)));
}

void Widget:: SLTcurrentText(QString str){
qDebug()<<"CurrentText ::"<<str<<endl;
}
I should use SIGNAL(currentTextChanged(QString)
tnx..

anda_skoa
1st May 2016, 21:37
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,
_