There are usually two types of projects to create qml:
- Add the quickwidgets module in the pro file using the usual creation method.
- To create a QQuick project, the following are some common creation methods:
How to create:
- New Project;
- New –QQuick –Empty
- This project is a helloword related empty project. The project is the main.qml file used in the main function.
#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();
}
New-QQuick-Scroll
This build is a project with many items added
Main code:
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
}
A brief introduction to the control:
ApplicationWindow: The top-level window of the style, supporting header and footer.
ApplicationWindow is a window, which can conveniently add a menu bar, header and footer items to the window.
You can declare ApplicationWindow as the root item of your application and run it by using QQmlApplicationEngine. In this way you can control the window's properties, appearance and layout from QML.
Interface layout:
Example:
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
menuBar: MenuBar {
// ...
}
header: ToolBar {
// ...
}
footer: TabBar {
// ...
}
StackView {
anchors.fill: parent
}
}