Interconversion of Qt types
-
Hi All,
Just want to if it is possible to convert a variable of QVariant type to QWidget type.
-
If you hold a pointer to QWidget in your variant, then yes. Otherwise - no.
-
@sierdzio Can you give an example of it?
-
@sierdzio Can you give an example of it?
@Swati777999 There are examples in the documentation (https://doc.qt.io/qt-6/qvariant.html and https://doc.qt.io/qt-6/qvariant.html#value), like:
QVariant variant; ... QColor color = variant.value<QColor>();
-
@Swati777999 There are examples in the documentation (https://doc.qt.io/qt-6/qvariant.html and https://doc.qt.io/qt-6/qvariant.html#value), like:
QVariant variant; ... QColor color = variant.value<QColor>();
-
QVariant *l = new QVariant; QWidget *window = new QWidget; QHBoxLayout *lay = new QHBoxLayout(window); lay->addWidget(l,0,0); lay->addWidget(img,0,1);
What's wrong in the above lines , in terms of conversion?
-
addWidget()
accepts a QWidget, not QVariant. These are 2 completely unrelated classes. If yourl
variant is holding a QWidget, then you can extract it like @jsulm mentioned:l = QVariant::fromValue(window); lay->addWidget(l.value<QWidget*>(),0,0);
-
QVariant *l = new QVariant; QWidget *window = new QWidget; QHBoxLayout *lay = new QHBoxLayout(window); lay->addWidget(l,0,0); lay->addWidget(img,0,1);
What's wrong in the above lines , in terms of conversion?
l
points to an invalid variant.addWidget(QWidget *)
will not accept aQVariant *
.