Converting UI Projects to Applications
Qt Quick UI Prototype projects are useful for creating user interfaces. To use them for application development in Qt Creator you have to add:
- Project configuration file (CMakeLists.txt or .pro)
- C++ code (.cpp)
- Resource files
- Code needed for deploying applications to devices
For more information about integrating QML and C++, see Overview - QML and C++ Integration.
Note: Since Qt Design Studio 2.3.0, Qt Design Studio project wizard templates generate projects that can be built with CMake. You can open the CMakeLists.txt project file in Qt Creator to continue developing the project. Also, you can use Qt Creator to create a Qt Quick Application project that you can open in Qt Design Studio.
Note: Since Qt Design Studio 3.9.0, Qt Design Studio project wizard templates generate projects that automatically checkout and build the Qt Quick Studio Components from Qt Code Review, using CMake. To turn off this feature, use the option BUILD_QDS_COMPONENTS in the CMake configuration.
For more information about using Qt Design Studio to create projects, see Qt Design Studio Manual.
If you want to use qmake as the build system, you can use a Qt Creator wizard template to create a Qt Quick application that is built using the qmake build system and then copy the source files from the Qt UI Quick project to the application project.
You can use the RESOURCES
option in the project configuration file to automatically add all the QML files and related assets to a Qt resource collection file (.qrc). However, large files should be included as external binary resources instead of compiling them into the binary.
The wizard automatically adds the QML_IMPORT_PATH
option to the project file for specifying the required QML import path. The path is only needed if more than one subdirectory has QML files.
Then you can use the QQuickView class in the main C++ source file to show the main QML file when the application starts.
The Qt Quick Designer Components module is installed when you install Qt Design Studio. If you use Qt Quick Studio Components or Effects from the module in a project that you want to edit in Qt Creator, you have to build the module and install it to your Qt to be able to build your project. For more information, see Adding Qt Quick Designer Components to Qt Installations.
The Qt Quick Timeline module is installed when you install Qt Design Studio. If you only install Qt Creator and Qt, remember to also select the Qt Quick Timeline module for installation. If your Qt is older than 5.14, you must build the Qt Quick Timeline module and install it to your Qt to be able to build your project.
Converting into qmake Projects
To convert a project that has a .qmlproject file to one that has a .pro file:
- Select File > New Project > Application (Qt) > Qt Quick Application > Choose.
- In the Build system field, select qmake as the build system to use for building and running the project, and then select Next (or Continue on macOS).
- Follow the instructions of the wizard to create the project.
- In the file explorer, copy the source files from the Qt Quick UI project directory to a subdirectory in the application project directory. For the purpose of these instructions, the directory is called
qml
. - Open the application project file, and edit the value of the
RESOURCES
option to add the following line:RESOURCES += \ $$files(qml/*)
- Also edit the value of the
QML_IMPORT_PATH
option to specify the QML import path:QML_IMPORT_PATH = qml/imports
Where
qml/imports
is the import path. - Select Build > Run qmake to apply the
RESOURCES
option to the build configuration. - Open the main.cpp file and replace the QQmlApplicationEngine object with a QQuickView object:
QQuickView view; view.engine()->addImportPath("qrc:/qml/imports"); view.setSource(QUrl("qrc:/qml/ProgressBar.ui.qml")); if (!view.errors().isEmpty()) return -1; view.show();
Where
qrc:/qml/imports
is the import path andqrc:/qml/ProgressBar.ui.qml
is the path to and the name of the main QML file in the Qt Quick UI project. - Select Build > Run to build and run your project.
Note: If you get error messages related to modules, perfom the steps described in Adding Qt Quick Designer Components to Qt Installations.
For example, if you copy the source files of the ProgressBar example from your Qt Design Studio installation (located in the \share\qtcreator\examples\ProgressBar
directory) to an empty Qt Quick application project and make the necessary changes, the main.cpp file should look as follows:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQuickView view; view.engine()->addImportPath("qrc:/qml/imports"); view.setSource(QUrl("qrc:/qml/ProgressBar.ui.qml")); if (!view.errors().isEmpty()) return -1; view.show(); app.exec(); }
Handling Large Data Files
Graphical assets used in the UI, such as images, effects, or 3D scenes are a typical cause for performance problems in UIs. Even building the application requires huge amounts of memory if you try to include large asset files, such as 100-MB 3D models or 64-MB textures, into the .qrc
file for compiling them into the binary.
First try to optimize your assets, as described in Optimizing Designs and Creating Optimized 3D Scenes.
If you really need large assets, they should either be loaded directly from the file system or use the Qt resource system in the dynamic way. For more information, see The Qt Resource System in the Qt documentation.
Adding Custom Fonts
To use custom fonts from the Qt Quick UI project, call the QFontDatabase::addApplicationFont() function from the main.cpp file.
Adding Qt Quick Designer Components to Qt Installations
Since Qt Design Studio 3.9, the Qt Quick Studio Components module is installed by default as part of the application. You can also install the module manually.
For example:
- Clone the module repository.
git clone https://code.qt.io/qt-labs/qtquickdesigner-components.git
- Install the Qt Quick Designer Components module. Enter the following commands:
mkdir build cd build cmake -GNinja -DCMAKE_INSTALL_PREFIX=<path_to_qt_install_directory> <path_to_qtquickdesigner-components> cmake --build . cmake --install .
Note: Here, <path_to_qt_install_directory> and <path_to_qtquickdesigner-components> needs to be replaced with the real location on your local drive. For example, <path_to_qt_install_directory> can be something like /Qt/6.3.0/msvc2019_64 and <path_to_qtquickdesigner-components> like this ../qtquickdesigner-components/
See also Create Qt Quick UI Prototypes.
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.