5

我正在使用 QVariant 将对象存储在 Qcombobox 内,这似乎工作正常。这是实现代码:

将类型添加到标题中的 QVariant:

Q_DECLARE_METATYPE(CDiscRecorder*)

pDiscRecorder 转换为 CDiscRecorder:

CDiscRecorder* pDiscRecorder = new CDiscRecorder();

然后存储在组合框中

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

当我尝试将其拉出时出现问题:

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

我收到错误:

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

我尝试在错误代码中实现提示无济于事,我已经按照线程在 Qt 的组合框中添加 QObject来实现此行为,如何才能找回我的对象​​?

谢谢

4

1 回答 1

6

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*>();
于 2013-07-31T21:50:29.200 回答