PDA

View Full Version : Accessing QVariant of QComboBox



lnxusr
9th November 2009, 08:00
I'm wanting to use a QComboBox as a sort of data storage for my application. It's not a large ammount of data, and I think this would be the easiest way to not be constantly reading from the database file. This little snippit shows what I'm trying to do.


QStringList dataList;

dataList << "D1" << "D2" << "D3";

ui->ShowDate->insertItem(0, QDate(query.value(2).toInt(), query.value(0).toInt(),
query.value(1).toInt()).toString(("MMMM d, yyyy")), dataList);

I know how to access the text with


ui->lineEdit->setText(ui->ShowDate->currentText());

but how do I access the QVariant?

I'm new to not only Qt, but c++ as well, so a small snippit showing how to put, say dataList[0] into a lable...


ui->label->setText(<however to get dataList[0] in here>);

Qt version 4.5

aamer4yu
9th November 2009, 08:59
I know how to access the text with
Qt Code:
1
ui->lineEdit->setText(ui->ShowDate->currentText());
but how do I access the QVariant?
I dont think QLineEdit has QVariant...


I'm new to not only Qt, but c++ as well, so a small snippit showing how to put, say dataList[0] into a lable...
ui->label->setText(dataList.at(0));

wysota
9th November 2009, 09:19
QComboBox::itemData().

You can also use "dynamic properties" on every widget.

lnxusr
9th November 2009, 17:42
@aamer4yu: I think you misunderstood my question. The QVariant I was talking about was attached to my QComboBox. I was trying to get that data into the lineEdit and/or label.

@wysota: Thank you so much. Looking into the QComboBox::itemData() and Qt::UserRole got me access to data... and no code snippit needed, I used your info to discover how to do it on my own.

You have been officially thanked.. :)