Qt – How to integrate QWidget in QML (Qt Quick 2.0)

qmlqt

I have closed library that return QFrame.
GUI of my program is developed with QML (Qt Quick 2.0).
I need solution to integrate QFrame (QWidget) to QML

Note:
I found example: Qt_DIR/Examples/Qt-5.3/declarative/cppextensions/qwidgets, that do something as I need. In ths example QWidged is addeed to QGraphicsProxyWidget. I write my code like this, but when I run my aplication it show me in console: "Cannot add a QtQuick 1.0 item (MyPushButton) into a QtQuick 2.0 scene!". This is this code:

class MyPushButton : public QGraphicsProxyWidget
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)

public:
    MyPushButton(QGraphicsItem* parent = 0)
        : QGraphicsProxyWidget(parent)
    {
        widget = new QPushButton("MyPushButton");
        widget->setAttribute(Qt::WA_NoSystemBackground);
        setWidget(widget);

        QObject::connect(widget, SIGNAL(clicked(bool)), this, SIGNAL(clicked(bool)));
    }

    QString text() const
    {
        return widget->text();
    }

    void setText(const QString& text)
    {
        if (text != widget->text()) {
            widget->setText(text);
            emit textChanged();
        }
    }

Q_SIGNALS:
    void clicked(bool);
    void textChanged();

private:
    QPushButton *widget;
};


private:
    QPushButton *widget;
};

Best Answer

QGraphicsProxyWidget is intended to use with QtQuick 1. Already there is answer Qt5. Embed QWidget object in QML

Another thought - you can embed your QWidget inside QQuickItem. Or look into QtQUickControls how they are painted with QtQuick2

Related Topic