欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > QtQuick-第一个程序

QtQuick-第一个程序

2024/11/30 14:53:25 来源:https://blog.csdn.net/qq_51470638/article/details/140591000  浏览:    关键词:QtQuick-第一个程序

新建Qt Quick Application。

main.cpp(保持原有的即可):

#include <QGuiApplication>
#include <QQmlApplicationEngine>int
main (int argc, char *argv[])
{QGuiApplication app (argc, argv);QQmlApplicationEngine engine;const QUrl url (QStringLiteral ("qrc:/MainWindow/Main.qml"));QObject::connect (&engine,&QQmlApplicationEngine::objectCreationFailed,&app,[] () { QCoreApplication::exit (-1); },Qt::QueuedConnection);engine.load (url);return app.exec();
}

main.qml:

import QtQuickWindow {width: 640height: 480visible: truetitle: qsTr("Hello World")Image{id:logosource:"qrc:/images/img.png"anchors.centerIn:parent}MyText{anchors.top:logo.bottomanchors.horizontalCenter:logo.horizontalCenter}
}

新建MyText.qml:

import QtQuickText{text: qsTr("这是一行绿色的文字")color: "green"
}

新建images.qrc(注意设置CMAKE_AUTORCC并添加到CMakeLists.txt的qt_add_executable):

<RCC><qresource prefix="/"><file>images/img.png</file></qresource>
</RCC>

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)project(MainWindow VERSION 0.1 LANGUAGES CXX)set(CMAKE_CXX_STANDARD_REQUIRED ON)find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)qt_standard_project_setup()set(CMAKE_AUTORCC ON)qt_add_executable(appMainWindowmain.cppimages.qrc
)qt_add_qml_module(appMainWindowURI MainWindowVERSION 1.0QML_FILES Main.qmlQML_FILES MyText.qmlRESOURCES images.qrc)# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appMainWindow PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appMainWindowMACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}MACOSX_BUNDLE TRUEWIN32_EXECUTABLE TRUE
)target_link_libraries(appMainWindowPRIVATE Qt6::Quick
)include(GNUInstallDirs)
install(TARGETS appMainWindowBUNDLE DESTINATION .LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

效果

在这里插入图片描述

另外如果只进行UI设计,可以新建Qt Quick UI项目,避免了编译C++程序,可以更快地预览效果。
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com