PDA

View Full Version : QVariant can't save QWidget Pointer ?!!



srazi
16th August 2011, 20:06
I get problem when converting a QWidget* to QVariant, please see the following code and its output:


#include <QApplication>
#include <QWidget>
#include <QVariant>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget= new QWidget();
QObject *obj = widget;

//you can use all other methods like qVariantFromValue(widget)
QVariant var1 = QVariant::fromValue(widget);
QVariant var2 = QVariant::fromValue(obj);

qDebug() << "widget-addr=" << widget << "address saved to variant=" << var1;
qDebug() << "object-addr=" << obj << "address saved to variant=" << var2;
}

output:


widget-addr= QWidget(0x347748) address saved to variant= QVariant(QWidget*, )
object-addr= QWidget(0x347748) address saved to variant= QVariant(QObject*, QWidget(0x347748) )


Why line 13 doesn't work? Why it doesn't convert QWidget* to QObject* automatically? [I'm using Qt-4.7.3 and minGW]

wysota
16th August 2011, 23:17
Why it doesn't convert QWidget* to QObject* automatically?
Your value is saved successfully, it's just the debug statement doesn't show it.