Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQuickWidget and QWidget integration
Forum Updated to NodeBB v4.3 + New Features

QQuickWidget and QWidget integration

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eti_nne
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      birsingh
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eti_nne
        wrote on last edited by
        #3

        @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.

        1. start is on widget : you can observe the mouse hover behavior (yellow / red / yellow / etc.)
        2. click -> switch to qml
        3. you can observe the mouse hover behavior (green / white / green / etc.)
        4. click -> switch bvack to qpushbutton
        5. you can observe the mouse hover behavior : nothing (no hover behavior)
        6. 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'
            }
        }
        
        
        1 Reply Last reply
        0
        • E Offline
          E Offline
          eti_nne
          wrote on last edited by
          #4

          Same behavior with Qt 5.11.2

          1 Reply Last reply
          0
          • E Offline
            E Offline
            eti_nne
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved