QT - How to get QVariant values ​​from combobox?

I am using QVariant to store an object inside a Qcombobox. It seems to be working fine. This is the implementing code:

Add type to QVariant in title:

Q_DECLARE_METATYPE(CDiscRecorder*) 

pDiscRecorder Sent as a CDiscRecorder:

 CDiscRecorder* pDiscRecorder = new CDiscRecorder(); 

Then stored in the combo box

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

The problem occurs when I try to pull it out:

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

I get an error message:

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

I tried to implement a hint in the error code to no avail, I followed the Add QObject stream to the Qt combo box to implement this behavior, how can my object return?

thanks

+4
source share
1 answer

The compiler gives you a hint that the argument list is missing - all you have to do is add brackets to say that you are trying to call this function. Therefore change it to

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

And that should work. This rather long line may be cleaner to break.

 QVariant variant = this->ui->cbDrives->itemData(index); CDiscRecorder* discRecorder = variant.value<CDiscRecorder*>(); 
+5
source

Source: https://habr.com/ru/post/1494588/


All Articles