Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate QWidget in QML (Qt Quick 2.0)

Tags:

qt

qml

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;
};
like image 536
Andrew P. Avatar asked Jul 14 '14 11:07

Andrew P.


People also ask

How connect C++ to QML?

Connecting to QML Signals All QML signals are automatically available to C++, and can be connected to using QObject::connect() like any ordinary Qt C++ signal. In return, any C++ signal can be received by a QML object using signal handlers.

What is Qwidget in Qt?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.


1 Answers

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

like image 56
nib Avatar answered Oct 13 '22 09:10

nib