views:

932

answers:

3

I have a class Pkg and I need to use it under form of QVariant.

At the end of my Pkg.h I have:

Q_DECLARE_METATYPE(Pkg)

and this does not give compile errors, but in my main.cpp I have to do:

qRegisterMetaType<Pkg>("Pkg");

and this does not give errors too, but when I try to create a QVariant(Pkg) I get lots of errors like:

In member function 'void MainWindow::FillPackagesList()':  
mainWin.cpp:233: error: 'qRegisterMetaType' isnot a member of 'QMetaType' mainWin.cpp:234: error: no matching function for call to QVariant::QVariant(Pkg&)'                                             

/usr/lib/qt/include/QtCore/qvariant.h:208: note: QVariant::QVariant(Qt::GlobalColor)           
/usr/lib/qt/include/QtCore/qvariant.h:206: note: QVariant::QVariant(const QRegExp&)            
/usr/lib/qt/include/QtCore/qvariant.h:204: note: QVariant::QVariant(const QLocale&)            
/usr/lib/qt/include/QtCore/qvariant.h:203: note: QVariant::QVariant(const QUrl&)               
/usr/lib/qt/include/QtCore/qvariant.h:201: note: QVariant::QVariant(const QRectF&)             
/usr/lib/qt/include/QtCore/qvariant.h:200: note: QVariant::QVariant(const QRect&)

and errors over errors again...

+6  A: 

The QVariant constructor won't just take any old type. You need to use QVariant::setValue() or qVariantFromValue.

Michael Bishop
A: 

thanks, it works, but now how can I regain the data I've set into a QTreeWidgetItem by this way:

packRow->setData(1, Qt::UserRole,qv); ??

(packRow is the QTreeWidgetItem)

Giancarlo
A: 

ahhhhh solved, many thanks, I've used:

 item->data(1,Qt::UserRole).value<Pkg>();

item is the QTreeWidgetItem selected in this event:

connect(packList,SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(setActualPackage(QTreeWidgetItem*)));
Giancarlo