QML笔记-4中方式运行qml文件
程序员文章站
2022-05-30 15:22:08
...
目录
使用QQmlApplicationEngine运行qml
程序结构如下:
源码如下:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Text {
anchors.centerIn: parent
id: text
text: qsTr("Hello World")
color: "red"
font.pointSize: 30
}
Component.onCompleted: {
console.debug("finished")
}
}
运行运行截图都是;
使用qml工具运行
这里要先设置好环境变量,我的电脑Qt路径如下:D:\Qt5.9\Qt\5.9.8\mingw53_32\bin
程序运行截图如下:
使用qmlScene工具运行qml文件
使用QtQuick Prototype运行qml文件
程序运行截图如下:
程序结构如下:
PrototypeDemo.qmlproject
/* File generated by Qt Creator */
import QmlProject 1.1
Project {
mainFile: "PrototypeDemo.qml"
/* Include .qml, .js, and image files from current directory and subdirectories */
QmlFiles {
directory: "."
}
JavaScriptFiles {
directory: "."
}
ImageFiles {
directory: "."
}
/* List of plugin directories passed to QML runtime */
// importPaths: [ "../exampleplugin" ]
}
PrototypeDemo.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Text {
anchors.centerIn: parent
id: text
text: qsTr("Hello World")
color: "red"
font.pointSize: 30
}
Component.onCompleted: {
console.debug("finished")
}
}
上一篇: 在QML 中,嵌入QWidget 对象
下一篇: Qt实战--主窗口布局
推荐阅读