新版本中項(xiàng)目文件全部放在了一起,見
- 【QtQuick3D學(xué)習(xí)】使用Qt Creator創(chuàng)建Qt Design Studio項(xiàng)目測(cè)試運(yùn)行——基于C++和Cmake
- 【QtQuick3D學(xué)習(xí)】使用Qt Design Studio創(chuàng)建項(xiàng)目并使用Qt Creator打開測(cè)試運(yùn)行——基于C++和Cmake
這里測(cè)試前后端分離,即使用Qt Design Studio設(shè)計(jì)前端和交互邏輯,C++寫后端,處理數(shù)據(jù)
項(xiàng)目創(chuàng)建
1. Qt Creator
按照【QtQuick3D學(xué)習(xí)】使用Qt Creator創(chuàng)建Qt Quick項(xiàng)目測(cè)試運(yùn)行——基于C++和Cmake創(chuàng)建項(xiàng)目,得到基本項(xiàng)目框架,或者使用之前創(chuàng)建好的項(xiàng)目
這里使用新創(chuàng)建的項(xiàng)目
2. Qt Design Studio
按照【QtQuick3D學(xué)習(xí)】使用Qt Design Studio創(chuàng)建項(xiàng)目并使用Qt Creator打開測(cè)試運(yùn)行——基于C++和Cmake創(chuàng)建項(xiàng)目,得到基本項(xiàng)目框架,或者使用之前創(chuàng)建好的項(xiàng)目
這里使用之前創(chuàng)建的項(xiàng)目
文件導(dǎo)入
這里要將Qt Design Studio項(xiàng)目中的部分文件導(dǎo)入到Qt Creator項(xiàng)目中使用
文件復(fù)制
將Qt Design Studio項(xiàng)目DesignTest文件夾中的前三個(gè)文件夾復(fù)制到Qt Creator項(xiàng)目
cImportDesign中的qml文件夾(ps:需要先創(chuàng)建該文件夾)內(nèi)
得到如下的文件結(jié)構(gòu)
│ CMakeLists.txt
│ CMakeLists.txt.user
│ main.cpp
│ main.qml
│ qml.qrc
│
└─qml
├─asset_imports
│ asset_imports.txt
│
├─content
│ │ App.qml
│ │ CMakeLists.txt
│ │ Screen01.ui.qml
│ │
│ └─fonts
│ fonts.txt
│
└─imports
│ CMakeLists.txt
│
└─DesignTest
│ CMakeLists.txt
│ Constants.qml
│ DirectoryFontLoader.qml
│ EventListModel.qml
│ EventListSimulator.qml
│ qmldir
│
└─designer
plugin.metainfo
qrc管理
在Qt Creator項(xiàng)目選擇qml.qrc,右鍵選擇添加現(xiàn)有文件夾
勾選qml文件夾
成功添加后可以在項(xiàng)目結(jié)構(gòu)中看到增加的文件
代碼修改
此時(shí)直接構(gòu)建運(yùn)行,還是原來的窗口,所以需要更改代碼
找到main.cpp文件,將13行修改為下面的內(nèi)容
const QUrl url(QStringLiteral("qrc:/qml/content/App.qml"));
或者直接復(fù)制下面的代碼替換
#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"));
const QUrl url(QStringLiteral("qrc:/qml/content/App.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();
}
重新構(gòu)建運(yùn)行,在應(yīng)用程序輸出窗口得到下面的錯(cuò)誤輸出
直接點(diǎn)擊藍(lán)色部分跳轉(zhuǎn)到錯(cuò)誤處,或者自行打開App.qml文件
將報(bào)錯(cuò)處(第5行)修改為下面的內(nèi)容
import "qrc:/qml/imports/DesignTest"
或者直接復(fù)制下面的代碼替換
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
import QtQuick 6.5
import "qrc:/qml/imports/DesignTest"
Window {
width: mainScreen.width
height: mainScreen.height
visible: true
title: "DesignTest"
Screen01 {
id: mainScreen
}
}
重新構(gòu)建運(yùn)行,在應(yīng)用程序輸出窗口得到下面的錯(cuò)誤輸出
同上,修改錯(cuò)誤處代碼為
import "qrc:/qml/imports/DesignTest"
由于Screen01.ui.qml文件的特殊性,會(huì)跳轉(zhuǎn)到設(shè)計(jì)窗口,不用擔(dān)心
重新構(gòu)建運(yùn)行,在應(yīng)用程序輸出窗口得到下面的錯(cuò)誤輸出
同樣的跳轉(zhuǎn)到錯(cuò)誤處,將錯(cuò)誤內(nèi)容注釋,根據(jù)內(nèi)容推測(cè),跟字體相關(guān),暫時(shí)不用
pragma Singleton
import QtQuick 6.5
//import QtQuick.Studio.Application
QtObject {
readonly property int width: 640
readonly property int height: 480
property string relativeFontDirectory: "fonts"
/* Edit this comment to add your custom font */
readonly property font font: Qt.font({
family: Qt.application.font.family,
pixelSize: Qt.application.font.pixelSize
})
readonly property font largeFont: Qt.font({
family: Qt.application.font.family,
pixelSize: Qt.application.font.pixelSize * 1.6
})
readonly property color backgroundColor: "#c2c2c2"
// property StudioApplication application: StudioApplication {
// fontPath: Qt.resolvedUrl("../../content/" + relativeFontDirectory)
// }
}
運(yùn)行結(jié)果
再次構(gòu)建運(yùn)行,得到應(yīng)用窗口,與此前【QtQuick3D學(xué)習(xí)】使用Qt Design Studio創(chuàng)建項(xiàng)目并使用Qt Creator打開測(cè)試運(yùn)行——基于C++和Cmake結(jié)果一致文章來源:http://www.zghlxwxcb.cn/news/detail-789318.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-789318.html
到了這里,關(guān)于【QtQuick3D學(xué)習(xí)】使用Qt Design Studio設(shè)計(jì)UI并導(dǎo)入Qt Creator項(xiàng)目中使用——基于C++和Cmake的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!