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
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*>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With