坚持QtQML,坚持移动互联网

http://www.qtcn.org/bbs/u/121778  [收藏] [复制]

toby520

将QtCoding进行到底,做Qt的宠儿

  • 89

    关注

  • 164

    粉丝

  • 3578

    访客

  • 等级:精灵王
  • 身份:论坛版主
  • 总积分:1262
  • 男,1986-11-17

最后登录:2024-04-28

更多资料

日志

Qt Widget嵌入到QML界面中

2014-10-16 16:34
C++部分使用 嵌入QWidget objects 到QML界面使用QGraphicsProxyWidget.
  1. #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界面部分
  1. 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
分类:默认分类|回复:2|浏览:2416|全站可见|转载
 

下一篇: 后端nodejs教程

上一篇: 即时通讯(IM)

 
删除

dss020466:qtquick 2.0之后 这样的方法不好用了
qt5.0之前可以这样用

2014-10-17 15:26 -
删除

xianqingzh:这个还是有问题的。 比如说,如果采用ocx,那么这一套就没用了。

2014-10-16 18:41 -

Powered by phpwind v8.7 Certificate Copyright Time now is:04-28 19:15
©2005-2016 QTCN开发网 版权所有 Gzip disabled