官方站点有一篇文章描述了如何开发一个Qt Quick app.
http://qt-project.org/doc/qt-5/gettingstartedqml.html#building-a-text-editor
但是这篇文章并不是真的写给初学者的。我花了一些时间阅读,还是不知道如何开始我的第一个工程。我应该把下面的描述语言添加到哪个文件中呢?
Rectangle {
id: simpleButton
...
MouseArea {
id: buttonMouseArea
// Anchor all sides of the mouse area to the rectangle's anchors
anchors.fill: parent
// onClicked handles valid mouse button clicks
onClicked: console.log(buttonLabel.text + " clicked")
}
}
并且,如何创建一个Qt Quick 工程。这个工程应该包含什么?
没有,官方的文章仅仅谈到了一些代码片段。你必需自己找到方法将这些片段集成起来。所以,这篇文章,并不是写給初学者的。
好,我来尝试一下。首先,我用VS 2012创建了一个Qt 工程,创建时选择了QML & Quick选项。这个工程有一些*.ui文件,里面包含的是XML定义,而不是JSON风格。我对QML不熟,不过显然这个工程不是我想要的。
好,现在来试一下Qt Creator. 使用这个IDE, 我能够创建一个真正的Quick工程,只要在开始选择创建Qt Quick Application. 我的工程名叫做helloworld.
在main.cpp文件中
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/helloworld/main.qml"));
viewer.showExpanded();
return app.exec();
}
QtQuick2ApplicationViewer是工程向导自动创建的。它继承自QQuickView,我们不需要修改它。它提供了一个有用的函数叫做setMainQmlFile。这个qml文件也由工程向导产生,它定义了主窗口的GUI。
import QtQuick 2.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
qtquick2applicationviewer.pri文件也是工程向导自动创建的。下面是这个文件的描述信息。也不需要修改它。
# checksum 0x21c9 version 0x90005
# This file was generated by the Qt Quick 2 Application wizard of Qt Creator.
# The code below adds the QtQuick2ApplicationViewer to the project and handles
# the activation of QML debugging.
# It is recommended not to modify this file, since newer versions of Qt Creator
# may offer an updated version of it.
qml文件是JSON风格, 非常容易阅读和理解。定义个了一个矩形,里面包含了两个元素:text和mouse area.
同时,工程向导已经创建了工程文件:helloworld.pro.
运行一下,一个hellowrold窗口跳出来了。
这才是真正面向新手的起步文档。