How can i get quickview pointer from QWidget when use QWidget::createWindowContainer(quickview, this);
-
class TestPage : public QWidget { Q_OBJECT public: explicit TestPage(QWidget *parent = nullptr); ~TestPage(); private: QStackedLayout *l; QQuickView *quickview; }; TestPage::TestPage(QWidget *parent) : QWidget(parent) { quickview = nullptr; quickview = new QQuickView(); QWidget *container = QWidget::createWindowContainer(quickview, this); container->setMinimumSize(400, 400); container->setMaximumSize(400, 400); quickview->setSource(QUrl("qrc:/test.qml")); //embedded qquickview to qwidget l = new QStackedLayout(this); l->addWidget(container); this->setLayout(l); }
For some reason ,I would like to get the quickview pointer (which emedded in QWidget with QWidget::createWindowContainer) from another thread. First i have got the TestPage pointer of my widget, then enumerate childdren of widget, i get the container pointer. But how can i get the pointer of quickview through container pointer.
QWidget::createWindowContainer return a QWidget pointer. QWidget have a function windowHandle. i tried container->windowHandle() ,but return nullptr.
-
class TestPage : public QWidget { Q_OBJECT public: explicit TestPage(QWidget *parent = nullptr); ~TestPage(); private: QStackedLayout *l; QQuickView *quickview; }; TestPage::TestPage(QWidget *parent) : QWidget(parent) { quickview = nullptr; quickview = new QQuickView(); QWidget *container = QWidget::createWindowContainer(quickview, this); container->setMinimumSize(400, 400); container->setMaximumSize(400, 400); quickview->setSource(QUrl("qrc:/test.qml")); //embedded qquickview to qwidget l = new QStackedLayout(this); l->addWidget(container); this->setLayout(l); }
For some reason ,I would like to get the quickview pointer (which emedded in QWidget with QWidget::createWindowContainer) from another thread. First i have got the TestPage pointer of my widget, then enumerate childdren of widget, i get the container pointer. But how can i get the pointer of quickview through container pointer.
QWidget::createWindowContainer return a QWidget pointer. QWidget have a function windowHandle. i tried container->windowHandle() ,but return nullptr.
@tony_yuan Demo:
#include <QApplication> #include <QGuiApplication> #include <QQuickView> #include <QVBoxLayout> #include <QWidget> #include <QWindow> #include <QDebug> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QApplication app(argc, argv); QWidget widget; widget.resize(640, 480); QVBoxLayout *layout = new QVBoxLayout(&widget); QQuickView *view = new QQuickView; view->setObjectName("Fooview"); view->setSource(QUrl("qrc:/main.qml")); QWidget *container = QWidget::createWindowContainer(view); layout->addWidget(container); // method 1 for(QWindow *window : QGuiApplication::allWindows()){ QQuickView *qview; if((qview = qobject_cast<QQuickView*>(window)) && (qview->objectName() == "Fooview")){ qDebug() << "METHOD 1:" << qview; } } // method 2: show() must be called to create the QWindow widget.show(); if(QWindow *window = container->window()->windowHandle()){ QQuickView *view = window->findChild<QQuickView *>("Fooview"); qDebug() << "METHOD 2:" << view; } return app.exec(); }
Output:
METHOD 1: QQuickView(0x557bc723ff10, visibility=QWindow::Hidden, flags=QFlags<Qt::WindowType>(Window), name="Fooview", parent=0x557bc7338440, geometry=0,0 640x480) METHOD 2: QQuickView(0x557bc723ff10, visibility=QWindow::Windowed, flags=QFlags<Qt::WindowType>(Window), name="Fooview", parent=0x557bc7423fb0, geometry=9,9 622x462)
-
@tony_yuan Demo:
#include <QApplication> #include <QGuiApplication> #include <QQuickView> #include <QVBoxLayout> #include <QWidget> #include <QWindow> #include <QDebug> int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QApplication app(argc, argv); QWidget widget; widget.resize(640, 480); QVBoxLayout *layout = new QVBoxLayout(&widget); QQuickView *view = new QQuickView; view->setObjectName("Fooview"); view->setSource(QUrl("qrc:/main.qml")); QWidget *container = QWidget::createWindowContainer(view); layout->addWidget(container); // method 1 for(QWindow *window : QGuiApplication::allWindows()){ QQuickView *qview; if((qview = qobject_cast<QQuickView*>(window)) && (qview->objectName() == "Fooview")){ qDebug() << "METHOD 1:" << qview; } } // method 2: show() must be called to create the QWindow widget.show(); if(QWindow *window = container->window()->windowHandle()){ QQuickView *view = window->findChild<QQuickView *>("Fooview"); qDebug() << "METHOD 2:" << view; } return app.exec(); }
Output:
METHOD 1: QQuickView(0x557bc723ff10, visibility=QWindow::Hidden, flags=QFlags<Qt::WindowType>(Window), name="Fooview", parent=0x557bc7338440, geometry=0,0 640x480) METHOD 2: QQuickView(0x557bc723ff10, visibility=QWindow::Windowed, flags=QFlags<Qt::WindowType>(Window), name="Fooview", parent=0x557bc7423fb0, geometry=9,9 622x462)
@eyllanesc Thanks for your reply, your solution works properly and is really userful for me.