C++ QVariant 到 QObject*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3887064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QVariant to QObject*
提问by Leonardo M. Ramé
I'm trying to attach a pointer to an QListWidgetItem
, to be used in the slot itemActivated
.
我正在尝试将指针附加到QListWidgetItem
,以在 slot 中使用itemActivated
。
The pointer I'm trying to attach is a QObject*
descendant, so, my code is something like this:
我试图附加的指针是一个QObject*
后代,所以,我的代码是这样的:
Image * im = new Image();
// here I add data to my Image object
// now I create my item
QListWidgetItem * lst1 = new QListWidgetItem(*icon, serie->getSeriesInstanceUID(), m_iconView);
// then I set my instance to a QVariant
QVariant v(QMetaType::QObjectStar, &im)
// now I "attach" the variant to the item.
lst1->setData(Qt::UserRole, v);
//After this, I connect the SIGNAL and SLOT
...
Now my problem, the itemActivated
slot. Here I need to extract my Image*
from the variant, and I don't know how to.
现在我的问题,itemActivated
插槽。在这里,我需要Image*
从变体中提取我的,但我不知道该怎么做。
I tried this, but I get the error:
我试过这个,但我收到错误:
‘qt_metatype_id' is not a member of ‘QMetaTypeId'
“qt_metatype_id”不是“QMetaTypeId”的成员
void MainWindow::itemActivated( QListWidgetItem * item )
{
Image * im = item->data(Qt::UserRole).value<Image *>();
qDebug( im->getImage().toAscii() );
}
Any hint?
任何提示?
Image * im = item->data(Qt::UserRole).value<Image *>();
回答by Leonardo M. Ramé
The answer is this
答案是这个
// From QVariant to QObject *
QObject * obj = qvariant_cast<QObject *>(item->data(Qt::UserRole));
// from QObject* to myClass*
myClass * lmyClass = qobject_cast<myClass *>(obj);
回答by aschepler
That looks like an unusual use of QVariant
. I'm not even sure if QVariant
would support holding a QObject
or QObject*
that way. Instead, I would try deriving from QListWidgetItem
in order to add custom data, something like this:
这看起来像QVariant
. 我什至不确定是否QVariant
会支持持有QObject
或QObject*
那样。相反,我会尝试派生QListWidgetItem
以添加自定义数据,如下所示:
class ImageListItem : public QListWidgetItem
{
// (Not a Q_OBJECT)
public:
ImageListItem(const QIcon & icon, const QString & text,
Image * image,
QListWidget * parent = 0, int type = Type);
virtual ~ImageListItem();
virtual QListWidgetItem* clone(); // virtual copy constructor
Image * getImage() const;
private:
Image * _image;
};
void MainWindow::itemActivated( QListWidgetItem * item )
{
ImageListItem *image_item = dynamic_cast<ImageListItem*>(item);
if ( !image_item )
{
qDebug("Not an image item");
}
else
{
Image * im = image_item->getImage();
qDebug( im->getImage().toAscii() );
}
}
Plus, the destructor of this new class gives you somewhere logical to make sure your Image
gets cleaned up.
另外,这个新类的析构函数为您提供了一个合乎逻辑的地方,以确保您Image
得到清理。
回答by Kamil Klimek
You've inserted your Image class as QObject *, so get it out as QObject * also. Then perform qobject_cast and everything should be fine
您已将 Image 类作为 QObject * 插入,因此也将其作为 QObject * 取出。然后执行 qobject_cast 一切都应该没问题