感觉Qt creator的QML例子太复杂,不容易学习,特意选择了一些搜集的例子学习。希望自己学习的过程给大家带来帮助。
QML之第一篇:hello world
main.cpp
#include <QtWidgets/qapplication.h>
#include <qtdeclarative/QDeclarativeview.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDeclarativeView *m_qmlView = new QDeclarativeView();
m_qmlView->setSource(QUrl::fromLocalFile("source.qml"));
m_qmlView->show();
return a.exec();
}
source.qml
import QtQuick 1.0
Rectangle { //root object
id: page //Rectangle property, We give it an id to be able to refer to it later.
width: 320; height: 480
color: "lightgray"
Text {
id: helloText
text: "Hello world!"
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
}
}
库文件:Qt5Declaratived.lib Qt5Guid.lib Qt5Widgetsd.lib Qt5Cored.lib
环境: VS2010 + win7-64位 + Qt5
显示效果: