现在的位置: 首页 > 综合 > 正文

Qt5 – c++嵌入qml示例

2013年09月18日 ⁄ 综合 ⁄ 共 1044字 ⁄ 字号 评论关闭
.pro
+++++++++++++++++++++++++++++++++++++++++++++
#-------------------------------------------------

#
# Project created by QtCreator 2012-03-15T09:57:33
#
#-------------------------------------------------

QT       += declarative  quick

TARGET = Qt5Test
TEMPLATE = app


SOURCES += main.cpp


OTHER_FILES += test.qml
 
 
main.cpp
+++++++++++++++++++++++++++++++++++++++++++++++++++
#include <QQuickView>
#include <QGuiApplication>
#include <QDeclarativeEngine>
#include <QObject>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv, 5);

    QQuickView viewer;
    QObject::connect(viewer.engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
    viewer.setSource(QUrl::fromLocalFile("test.qml"));
    viewer.show();

    return app.exec();
}
 
 
.qml
+++++++++++++++++++++++++++++++++++++++++++++++++++++
import QtQuick 2.0

Rectangle {
    width: 640
    height: 480
    Text  {
        anchors.centerIn: parent
        text: qsTr("\u6211\u662f\u4e2d\u6587Hello Qt")
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}
 
参考:
http://qt-project.org/forums/%20/viewthread/13857
http://qt-project.org/forums/viewthread/6907

抱歉!评论已关闭.