Solved QQuickWidget and QWidget integration
-
Good morning,
I am working on a software [Win10 64, Qt5.9].
We have added a new ui item : QQuickWidget, that contains a qml file that handle rendering some GL stuff.
The QQuickWidget is part from a QFrame, which is sometimes shown, sometimes hidden.
When the quickwidget is visible, everything is fine.
When the quickwidget is not visible, the UI loses a lot of its behavior: hover, onClick, etc. The event happen (f.i a qDebug in the onCLick) but the UI doesn't.I'm running out of idea, so I'm trying to update to Qt5.11.2, to see if it changes anything :)
Does someone have an idea ?
-
I will like to see the code snippet. But my general suggestion is that you need to change the anchors of items dependent on the widget that is hidden. You may not be able to anchor an item to a hidden item. So change the anchors at run time. Also if the parent widget is hidden then all the child widgets are hidden. If you post a code snippet then I can suggest specifically.
-
@birsingh
Thanks for your answer. Here is a way to reproduce my problem.
The following code creates a Stack, with two buttons : a qml one, and qpushbutton one.- start is on widget : you can observe the mouse hover behavior (yellow / red / yellow / etc.)
- click -> switch to qml
- you can observe the mouse hover behavior (green / white / green / etc.)
- click -> switch bvack to qpushbutton
- you can observe the mouse hover behavior : nothing (no hover behavior)
- click -> switch to qml
etc.
main.cpp --> Creating the app and the main window
#include <QApplication> #include "MyMainWindow.h" int main(int argc, char **argv) { QApplication application(argc, argv); MyMainWindow window; window.show(); window.raise(); int status = application.exec(); return status; }
MyMainWindow.h --> declaring my main window
#pragma once #include <QMainWindow> #include <QStackedLayout> class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget *parent = nullptr); ~MyMainWindow(void){} private: QStackedLayout *layout; public slots: void switchToWidget(void); };
MYMainWindow.cpp -> Creating the stacked layout, with two widgets. The first on is a QPushButton. The second one is a QML Button.
#include <QApplication> #include <QDebug> #include <QFrame> #include <QMainWindow> #include <QPushButton> #include <QtQuickWidgets/QQuickWidget> #include <QQuickItem> #include <QQmlProperty> #include "MyMainWindow.h" MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent) { // Central Widget QFrame *central = new QFrame; central->setFixedSize(300,300); this->setCentralWidget(central); // BUTTON - QWidget QPushButton *buttonWidget = new QPushButton("WIDGET", central); buttonWidget->setStyleSheet("QPushButton{" " background-color: red;" "}" "QPushButton:hover{" " background-color: yellow;" "}"); // BUTTON - QQuickWidget QQuickWidget *buttonQML = new QQuickWidget(central); buttonQML->setResizeMode(QQuickWidget::SizeRootObjectToView); buttonQML->setSource(QUrl("qrc:/qml/ButtonQML.qml")); buttonQML->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // Main Stack Layout this->layout = new QStackedLayout(central); this->layout->addWidget(buttonWidget); this->layout->addWidget(buttonQML); this->layout->setCurrentIndex(0); // Stack siwtch QObject::connect(buttonWidget, &QPushButton::clicked, [=](){ this->layout->setCurrentIndex(1); }); QQuickItem *rootObject = buttonQML->rootObject(); QObject::connect(rootObject, SIGNAL(clicked()), this, SLOT(switchToWidget())); } void MyMainWindow::switchToWidget(void) { this->layout->setCurrentIndex(0); }
ButtonQML.qml
import QtQuick 2.0 import QtQuick.Controls 2.1 Button { text: "ButtonQML" id: root background: Rectangle { color: root.hovered ? 'green' : 'white' } }
-
Same behavior with Qt 5.11.2
-
Apparently, the QQuickWidget seems to have a lot of impact on the UI :
Note: Using QQuickWidget disables the threaded render loop on all platforms. This means that some of the benefits of threaded rendering, for example Animator classes and vsync driven animations, will not be availab
Using a QQuickView instead fixed the behavior. Maybe they are side effects. I'm not sure.