C++部分使用 嵌入QWidget objects 到QML界面使用QGraphicsProxyWidget.- #include <QtDeclarative/QDeclarativeExtensionPlugin>
#include <QtDeclarative/qdeclarative.h> #include <QtGui/QGraphicsProxyWidget> #include <QtGui/QPushButton> #include <QDebug>
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; };
class QWidgetsPlugin : public QDeclarativeExtensionPlugin { Q_OBJECT public: void registerTypes(const char *uri) { qmlRegisterType<MyPushButton>(uri, 1, 0, "MyPushButton"); } };
#include "qwidgets.moc"
Q_EXPORT_PLUGIN2(qmlqwidgetsplugin, QWidgetsPlugin);
QML界面部分 - import QtQuick 1.0
import "QWidgets" 1.0
Rectangle { id: window
property int margin: 30
width: 640; height: 480 color: palette.window
SystemPalette { id: palette }
MyPushButton { id: button1 x: margin; y: margin text: "Right" transformOriginPoint: Qt.point(width / 2, height / 2)
onClicked: window.state = "right" }
MyPushButton { id: button2 x: margin; y: margin + 30 text: "Bottom" transformOriginPoint: Qt.point(width / 2, height / 2)
onClicked: window.state = "bottom" }
MyPushButton { id: button3 x: margin; y: margin + 60 text: "Quit" transformOriginPoint: Qt.point(width / 2, height / 2)
onClicked: Qt.quit() }
states: [ State { name: "right" PropertyChanges { target: button1; x: window.width - width - margin; text: "Left"; onClicked: window.state = "" } PropertyChanges { target: button2; x: window.width - width - margin } PropertyChanges { target: button3; x: window.width - width - margin } PropertyChanges { target: window; color: Qt.darker(palette.window) } }, State { name: "bottom" PropertyChanges { target: button1; y: window.height - height - margin; rotation: 180 } PropertyChanges { target: button2 x: button1.x + button1.width + 10; y: window.height - height - margin rotation: 180 text: "Top" onClicked: window.state = "" } PropertyChanges { target: button3; x: button2.x + button2.width + 10; y: window.height - height - margin; rotation: 180 } PropertyChanges { target: window; color: Qt.lighter(palette.window) } } ]
transitions: Transition { ParallelAnimation { NumberAnimation { properties: "x,y,rotation"; duration: 600; easing.type: Easing.OutQuad } ColorAnimation { target: window; duration: 600 } } } }
详细参考 C++扩展 QWidget
|