Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT - How to retrieve QVariant Values from combobox?

I'm using QVariant to store a object inside of a Qcombobox, This appears to work fine. This is the implementing code:

Add type to QVariant in header:

Q_DECLARE_METATYPE(CDiscRecorder*)

pDiscRecorder Casted as CDiscRecorder:

CDiscRecorder* pDiscRecorder = new CDiscRecorder();

Then stored in the combobox

ui->cbDrives->addItem(QString::fromWCharArray(strName), QVariant::fromValue(pDiscRecorder));

The problem arises when I try to pull it out:

CDiscRecorder* discRecorder = this->ui->cbDrives->itemData(index).value<CDiscRecorder*>;

I receive the error:

error C3867: 'QVariant::value': function call missing argument list; use '&QVariant::value' to create a pointer to member

I tried to implement the hint in the error code to no avail, I have followed the thread Add QObject in the combo box of Qt to implement this behavior, how can get my object back ?

Thanks

like image 664
rreeves Avatar asked Jul 31 '13 21:07

rreeves


1 Answers

The compiler is giving you the hint that the argument list is missing - all you should need to do is add the brackets to tell it that you're trying to call the function. So change it to

CDiscRecorder* discRecorder = this->ui->cbDrives->itemData(index).value<CDiscRecorder*>();

And it should work. That's quite a long line, might be cleaner to break it out

QVariant variant = this->ui->cbDrives->itemData(index);
CDiscRecorder* discRecorder = variant.value<CDiscRecorder*>();
like image 63
docsteer Avatar answered Sep 21 '22 08:09

docsteer