Hello World
To test your installation, we will create a small hello world application. Please, open Qt Creator and create a Qt Quick UI Project ( File ‣ New File or Project ‣ Other Project ‣ Qt Quick UI prototype
) and name the project HelloWorld
.
为了测试您的安装,我们将创建一个小的hello world应用程序。请打开Qt Creator并创建一个Qt Quick UI项目(文件‣
新建文件或项目‣
其他项目‣
Qt Quick UI原型),并将该项目命名为HelloWorld。
TIP
注
The Qt Creator IDE allows you to create various types of applications. If not otherwise stated, we always use a Qt Quick UI prototype project. For a production application you would often prefer a CMake
based project, but for fast prototyping this type is better suited.
Qt Creator IDE允许您创建各种类型的应用程序。如果没有特别说明,我们总是使用Qt Quick UI原型项目。对于生产应用程序,您通常更喜欢基于CMake的项目,但对于快速原型开发,这种类型更适合。
TIP
注
A typical Qt Quick application is made out of a runtime called the QmlEngine which loads the initial QML code. The developer can register C++ types with the runtime to interface with the native code. These C++ types can also be bundled into a plugin and then dynamically loaded using an import statement. The qml
tool is a pre-made runtime which is used directly. For the beginning, we will not cover the native side of development and focus only on the QML aspects of Qt 6. This is why we start from a prototype project.
一个典型的Qt Quick应用程序是由一个名为QmlEngine的运行时组成的,它加载初始QML代码。开发者可以在运行时中注册c++类型以调用本地代码。这些c++类型也可以绑定到一个插件中,然后使用import语句动态载入。qml工具是一个预先制作的运行时,可以直接使用。首先,我们将不讨论本地开发方面,而只关注Qt 6的QML方面。这就是为什么我们要从原型项目开始。
Qt Creator creates several files for you. The HelloWorld.qmlproject
file is the project file, where the relevant project configuration is stored. This file is managed by Qt Creator, so don’t edit it yourself.
Qt Creator为您创建了几个文件。helloworld.qmlproject文件是项目文件,其中存储了项目的配置信息。这个文件由Qt Creator管理,所以不要随意编辑它。
Another file, HelloWorld.qml
, is our application code. Open it and try to understand what the application does before you read on.
另一个文件,HelloWorld.qml。是我们的应用程序代码。在继续阅读之前,打开它并尝试理解它的作用。
// HelloWorld.qml
import QtQuick
import QtQuick.Window
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
}
The HelloWord.qml
program is written in the QML language. We’ll discuss the QML language more in-depth in the next chapter. QML describes the user interface as a tree of hierarchical elements. In this case, a window of 640 x 480 pixels, with a window title if “Hello World”.
HelloWord.qml文件是用qml语言编写的。我们将在下一章更深入地讨论QML语言。QML将用户界面描述为层次结构元素的树。在本例中,窗口为640 x 480像素,窗口标题为Hello World。
To run the application on your own, press the Run tool on the left side, or select Build > Run from the menu.
要自己运行应用程序,请按左侧的run按钮,或从菜单中选择构建>运行。
In the background, Qt Creator runs qml
and passes your QML document as the first argument. The qml
application parses the document, and launches the user interface. You should see something like this:
在后台,Qt Creator运行qml并将qml文件作为第一个参数传递。qml应用程序解析qml文件,并运行出用户界面。你应该看到类似这样的窗口:
Qt 6 works! That means we’re ready to continue.
Qt 6示例程序运行成功!然后我们可继续学习其他部分了。
TIP
注
If you are a system integrator, you’ll want to have Qt SDK installed to get the latest stable Qt release, as well as a Qt version compiled from source for your specific device target.
最新的稳定的Qt发行版
TIP
注
Build from Scratch
从头构建
If you’d like to build Qt 6 from the command line, you’ll first need to grab a copy of the code repository and build it. Visit Qt’s wiki for an up-to-date explanation of how to build Qt from git.
如果您想从命令行构建Qt 6,那么首先需要从代码库获取一个副本并构建。访问Qt的wiki,了解如何从git获取并构建Qt。
After a successful compilation (and 2 cups of coffee), Qt 6 will be available in the qtbase
folder. Any beverage will suffice, however, we suggest coffee for best results.
在成功编译(需要两杯喝咖啡的时间)之后,Qt 6将在qtbase文件夹中可用。编译时什么饮料都可以,但我们感觉喝咖啡效果最好。
If you want to test your compilation, you can now run the example with the default runtime that comes with Qt 6:
如果想要测试下编译的库,现在可以使用Qt 6附带的默认运行时,运行下示例:
$ qtbase/bin/qml