Qt5 - c++嵌入qml示例

本文介绍如何使用Qt5与Visual Studio 2008进行编译,包含.pro文件配置、源文件main.cpp及.qml文件的示例代码。通过简单的Qt5应用程序示例,展示了如何创建和运行一个包含QML界面的应用。

.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值