示例介绍
之前看到的示例都比较简单,这次搞一个相对复杂的例子:
具体介绍
1 main.cpp
int main(int argc, char *argv[])
{
QtQuickControlsApplication app(argc, argv);
if (QCoreApplication::arguments().contains(QLatin1String("--coreprofile"))) {
QSurfaceFormat fmt;
fmt.setVersion(4, 4);
fmt.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(fmt);
}
QQmlApplicationEngine engine(QUrl("qrc:/main.qml"));
return app.exec();
}
这里使用QQmlApplicationEngine,之前示例1中使用的是QQuickView ,可以看到QQuickView中用到了view.engine()即QQmlEngine类型的,是QQmlApplicationEngine的父类。
查看相关帮助:
Unlike QQuickView, QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window.
2 main.qml
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.1
import QtQuick.Controls 1.2
import "qml/UI.js" as UI
/* UI.js内容
.pragma library
var margin = 2
var tabPosition = Qt.TopEdge
var label = ""
*/
import "qml" // 引入整个目录
ApplicationWindow { // 如QQmlApplicationEngine帮助中说的,这里需要自己定义一个Window
visible: true
title: "Qt Quick Controls Gallery"
width: 640
height: 480
MessageDialog { // 帮助对话框
id: aboutDialog
icon: StandardIcon.Information
title: "About"
text: "Qt Quick Controls Gallery"
informativeText: "This example demonstrates most of the available Qt Quick Controls."
}
Action { // 定义几个Action
id: copyAction
text: "&Copy"
shortcut: StandardKey.Copy
iconName: "edit-copy"
enabled: (!!activeFocusItem && !!activeFocusItem["copy"])
onTriggered: activeFocusItem.copy()
}
Action {
id: cutAction
text: "Cu&t"
shortcut: StandardKey