示例效果]
源码
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
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();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
Window
{
id: root
visible: true
width: 800
height: 600
title: qsTr("Hello World")
ColumnLayout
{
anchors.top: parent.top
Layout.topMargin: 10
spacing: 5
RowLayout
{
Layout.alignment:Qt.AlignHCenter
Text
{
text: qsTr("当前时间:")
font.family: "微软雅黑"
font.pixelSize: 20
}
Text
{
id: currentTimeID
text: qsTr("--")
color: "pink"
font.family: "Courier New"
font.pixelSize: 40
font.bold: true
}