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;
};
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With