创建qml的工程通常有两种:
- 使用常规的创建方式,在pro文件种添加 quickwidgets 模块。
- 创建QQuick工程,下面是一些常见的创建方式:
创建方式:
- 新建项目;
- 新建–QQuick–Empty
- 这个项目是一个helloword的相关的空的项目。项目是用在main函数中调用的main.qml的文件。
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
// 高清分辨率的开启
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
新建–QQuick–Scroll
这个建立的是一个添加了许多item的工程
主要代码:
ScrollView {
anchors.fill: parent
ListView {
id: listView
width: parent.width
model: 20
delegate: ItemDelegate {
text: "Item " + (index + 1)
width: listView.width
}
}
}
// 加载图片
Image {
// 图片名
id:image
focus: true
// 加载图片资源(资源文件,本地文件,链接 都可以)
source: 'qrc:/images/noimage.jpg'
// 充满父对象
anchors.fill: parent
// 填充的方式(两边留白)
fillMode: Image.PreserveAspectFit
}
控件的简单介绍:
ApplicationWindow:样式的顶级窗口,支持页眉和页脚。
ApplicationWindow是一个窗口,它可以方便地添加一个菜单栏,页眉和页脚项目到窗口。
您可以将ApplicationWindow声明为应用程序的根项,并通过使用QQmlApplicationEngine来运行它。通过这种方式,您可以从QML控制窗口的属性、外观和布局。
界面布局:
示例:
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
menuBar: MenuBar {
// ...
}
header: ToolBar {
// ...
}
footer: TabBar {
// ...
}
StackView {
anchors.fill: parent
}
}