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. Why does adding a QWidget to a QQuickPaintedItem's QML fail to display?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 763 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1
    #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();
        }
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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