Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why does adding a QWidget to a QQuickPaintedItem's QML fail to display?



  • #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <widgetitem.h>
    #include <QProgressBar>
    #include <QPushButton>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<WidgetItem>("WidgetItem", 1, 0, "WidgetItem");
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
    
        WidgetItem* widgetItem = nullptr;
    
        QPushButton button("good old push button");
    
        // Could we move this inside WidgetItem?
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         [&button, &widgetItem](QObject* object, const QUrl /*&url*/) {
            widgetItem = object->findChild<WidgetItem*>();
            Q_ASSERT(widgetItem);
            widgetItem->setWidget(&button);
        });
        //
        QQmlContext* pContext = engine.rootContext();
        pContext->setContextProperty("WidgetItem",widgetItem);
        engine.load(url);
    
        return app.exec();
    }
    
    
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import WidgetItem 1.0
    
    Window {
        visible: true
        width: 450
        height: 70
        title: qsTr("Hello Widget")
    
        Rectangle {
            anchors.fill: parent
            Text {
                id: text
                anchors.margins: 20
                text: "This text is created with QML, the item below is a QWidget:"
            }
    
            WidgetItem {
                anchors.top: text.bottom
                anchors.topMargin: 15
                width: parent.width
                height: parent.height - text.height
            }
        }
    
        Shortcut {
            sequence: "Ctrl+q"
            onActivated: Qt.quit()
        }
    }
    
    
    #ifndef WIDGETITEM_H
    #define WIDGETITEM_H
    
    #include <QQuickPaintedItem>
    #include <QWidget>
    #include <QPointer>
    
    class WidgetItem : public QQuickPaintedItem
    {
        Q_OBJECT
    
    public:
        WidgetItem(QQuickItem* parent = nullptr);
    
        void setWidget(QWidget* widget);
    
        virtual void paint(QPainter* painter) override;
    
    protected:
    
        void mousePressEvent(QMouseEvent* event) override;
        void mouseReleaseEvent(QMouseEvent* event) override;
    
    private:
        QPointer<QWidget> m_widget;
    };
    
    #include "widgetitem.h"
    
    #include <QPainter>
    #include <QTimer>
    
    WidgetItem::WidgetItem(QQuickItem *parent) : QQuickPaintedItem(parent)
    {
        setAcceptTouchEvents(true);
        setAcceptedMouseButtons(Qt::AllButtons);
    }
    
    void WidgetItem::setWidget(QWidget *widget)
    {
        m_widget = widget;
    }
    
    void WidgetItem::paint(QPainter *painter)
    {
        if(m_widget) {
            m_widget->render(painter);
        }
    }
    
    void WidgetItem::mousePressEvent(QMouseEvent *event)
    {
        if(m_widget) {
            QCoreApplication::sendEvent(m_widget, event);
            update();
        }
    }
    
    void WidgetItem::mouseReleaseEvent(QMouseEvent *event)
    {
        if(m_widget) {
            QCoreApplication::sendEvent(m_widget, event);
            update();
        }
    }
    
    

  • Lifetime Qt Champion

    Hi,

    You should take a look at KDAB's DeclarativeWidgets project.


Log in to reply