Qt Quick Controls 之间的差异

Qt Quick Controls were originally developed to support desktop platforms, with mobile and embedded support coming shortly afterwards. They have a very broad scope, in that they provide a styling system flexible enough to allow the development of applications that have either a platform-dependent or platform-independent style.

On embedded systems, where the hardware has limited resources, this approach can be inefficient. Qt Quick Controls 2 were designed to solve this problem, using benchmarks to guide the development.

C++ 和 QML

In many cases, the internal state of a control can be more efficiently processed in C++. For example, handling input events in C++ makes a difference for controls that would otherwise need to create internal MouseAreas and attached Keys objects.

样式

Not only does handling events and logic in C++ increase performance, but it allows the visual QML layer to be a simple, declarative layer on top. This is reflected in the structure of the controls project: all visual implementations sit in the imports folder, so that users who want to create their own complete style can copy the folder and start tweaking. Read more about implementing a style plugin here .

In Qt Quick Controls 2, styles no longer provide components that are dynamically instantiated by controls, but controls themselves consist of item delegates that can be replaced. In effect, this means that delegates are Qt Quick items that are instantiated on the spot, as properties of the control, and are simply parented to the control.

模块化和简单性

When it comes to more complex controls, it is sometimes better to split them up into separate building blocks. As an example, the complex ScrollView control:

ScrollView {
    horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
    Flickable {
        // ...
    }
}
					

Is replaced with simple ScrollBar / ScrollIndicator controls that can be attached to any Flickable:

Flickable {
    // ...
    ScrollBar.vertical: ScrollBar { }
}
					

The API of Qt Quick Controls 2 aims to be clean and simple. Common operations are easy, and more advanced ones are liberally documented with snippets that can be copied into your code.

特征比较表

Qt Quick Controls Qt Quick Controls 2
Stylable delegates Yes Yes
Pre-built native styles Yes No
Runtime style/theme changes Yes 1 Yes 2
Can be used on Desktop Yes Yes
Can be used on Mobile Yes 3 Yes
Can be used on Embedded Yes 3 Yes
Internal event handling QML C++
  1. Not officially supported, but technically possible via private APIs
  2. Only themes for specific styles can be changed at runtime, styles are fixed
  3. Performance may not be optimal

移植 Qt Quick Controls 代码

The API of Qt Quick Controls 2 is very similar to Qt Quick Controls, but it does come with some changes necessary to facilitate the improvements. The majority of changes are to do with styling; all of a control's delegates are now accessible in the control itself, instead of in a separate style object.

For example, to style a button in Qt Quick Controls:

Button {
    style: ButtonStyle {
        label: Label {
            // ...
        }
    }
}
					

To style a button in Qt Quick Controls 2:

Button {
    contentItem: Label {
        // ...
    }
}
					
					

Preparing for Migration

With this in mind, a good way to prepare for a migration to Qt Quick Controls 2 is to place each control that you have a custom style for in its own QML file. For example, the Qt Quick Controls button above could be moved to a file named Button.qml in a directory named controls , and used in the following manner:

import "controls" as Controls
Controls.Button {
    ...
}
					

This works with both modules, and will reduce the amount of work needed when the migration begins.

类型比较表

The first column lists all types available in Qt Quick Controls 1 , Qt Quick Dialogs ,和 Qt Quick Extras . The second column documents the respective type in Qt Quick Controls 2. When a direct alternative is not available, the third column contains an alternative that provides related functionality. The last column contains some remarks about the differences between the types in the different modules.

Qt Quick Controls 1 Qt Quick Controls 2 替代 备注
动作 Shortcut
(Qt Quick)
  • Qt Quick Controls 1 : 动作 is an abstract user interface action that is bound to buttons and menu items, and can provide shortcuts.
  • Qt Quick : Shortcut provides the shortcut functionality that was previously only provided by 动作 .
ApplicationWindow ApplicationWindow
BusyIndicator BusyIndicator
Button Button
Calendar MonthGrid ,
DayOfWeekRow ,
WeekNumberColumn
(Qt Labs Calendar)
  • Qt Labs Calendar : MonthGrid , DayOfWeek ,和 WeekNumberColumn are experimental unstyled building blocks for calendar views.
CheckBox CheckBox
ComboBox ComboBox
ExclusiveGroup ButtonGroup
(Qt Quick Controls 2)
  • Qt Quick Controls 2 : ButtonGroup offers similar functionality.
GroupBox GroupBox
Label Label
Menu Menu Menu
(Qt Labs Platform)
  • Qt Quick Controls 1 : Menu is native on platforms where an implementation is available in the Qt Platform Abstraction Layer. Other platforms use a QML-based top-level menu popup window. Menu supports traditional desktop style cascading submenus, but does not work on Embedded Linux because EGLFS does not support multiple top-level windows.
  • Qt Quick Controls 2 : Menu is a non-native Item-based popup that is stacked above the application content. Due to this, menu popups are restricted within window boundaries. Menu is fully customizable using QML and Qt Quick, and allows adding any Items . Traditional desktop oriented features, such as cascading submenus and visualizing keyboard shortcuts are missing.
  • Qt Labs Platform : Menu experimental native menu that uses Qt Widgets as a fallback on platforms where a native implementation is not available in the Qt Platform Abstraction Layer.
MenuBar MenuBar
(Qt Labs Platform)
  • Qt Quick Controls 1 : MenuBar is native on platforms where an implementation is available in the Qt Platform Abstraction Layer. Other platforms use a QML-based menubar item stacked at the top of the window.
  • Qt Labs Platform : MenuBar experimental native menubar. It is only available on platforms where a native implementation is available in the Qt Platform Abstraction Layer.
MenuItem ,
MenuSeparator
MenuItem ,
MenuSeparator
MenuItem ,
MenuSeparator
(Qt Labs Platform)
  • Qt Quick Controls 1 : MenuItem and MenuSeparator are native on platforms where an implementation is available in the Qt Platform Abstraction Layer. Other platforms use QML-based menu items and separators.
  • Qt Quick Controls 2 : MenuItem and MenuSeparator are a non-native QML-based menu items and separators that can be fully customized using QML and Qt Quick.
  • Qt Labs Platform : MenuItem and MenuSeparator are experimental native menu items and separators.
ProgressBar ProgressBar
RadioButton RadioButton
ScrollView ScrollView
Slider Slider
SpinBox SpinBox
SplitView
StackView ,
StackViewDelegate ,
Stack
StackView
  • Qt Quick Controls 2 : StackView provides customizable transitions and attached properties via a single StackView 类型。
StatusBar ToolBar
(Qt Quick Controls 2)
  • Qt Quick Controls 2 : ApplicationWindow allows assigning any item or control, such as ToolBar , as a header or footer.
Switch Switch
TabView ,
Tab
TabBar ,
TabButton
(Qt Quick Controls 2)
  • Qt Quick Controls 2 : TabBar and TabButton offer similar functionality, and can be used to build tabbed views.
TableView
TextArea TextArea
  • Qt Quick Controls 1 : TextArea 继承 ScrollView and is therefore always a scrollable editor.
  • Qt Quick Controls 2 : TextArea is a simpler multi-line editor that can be optionally attached to a Flickable to provide scrolling functionality. This allows using TextArea in a scrollable page without having two nested scrollable areas, which can be problematic and cause usability issues.
TextField TextField
ToolBar ToolBar
ToolButton ToolButton
TreeView
Qt Quick Dialogs Qt Quick Controls 2 替代 备注
Dialog Dialog
  • Qt Quick Dialogs : Dialog is either a top-level window or an Item-based popup depending on whether the underlying platform supports multiple top-level windows.
  • Qt Quick Controls 2 : Dialog is not a top-level window, but an Item-based popup that is stacked above the application content. Due to this, dialogs are restricted within window boundaries.
ColorDialog ,
FileDialog ,
FontDialog ,
MessageDialog
ColorDialog ,
FileDialog ,
FolderDialog ,
FontDialog ,
MessageDialog
(Qt Labs Platform)
  • Qt Quick Dialogs : Dialogs are native on platforms where an implementation is available in the Qt Platform Abstraction Layer. Other platforms use either Qt Widgets or QML-based dialogs depending on whether the underlying platform supports multiple top-level windows.
  • Qt Labs Platform : Experimental native dialogs that use Qt Widgets as a fallback on platforms where a native implementation is not available in the Qt Platform Abstraction Layer.
Qt Quick Extras Qt Quick Controls 2 替代 备注
CircularGauge
DelayButton DelayButton
Dial Dial
Gauge
Picture
PieMenu
StatusIndicator
ToggleButton
Tumbler ,
TumblerColumn
Tumbler
  • Qt Quick Extras : Tumbler can consist of multiple columns.
  • Qt Quick Controls 2 : Tumbler presents a single spinnable wheel. Multiple columns can be created by placing multiple Tumblers next to each other.
No Predecessor Qt Quick Controls 2 替代 备注
AbstractButton
ButtonGroup ExclusiveGroup
(Qt Quick Controls 1)
  • Qt Quick Controls 1 : ExclusiveGroup offers similar functionality.
CheckDelegate
容器
Control
Drawer
Frame
ItemDelegate
Page
PageIndicator
Pane
Popup
RadioDelegate
RangeSlider
RoundButton
ScrollBar ,
ScrollIndicator
ScrollView
(Qt Quick Controls 1)
  • Qt Quick Controls 1 : ScrollView offers similar functionality. It combines horizontal and vertical scrollbars, and the background and frame around the scrollable view.
StandardPaths
(Qt Labs Platform)
  • Qt Quick Dialogs : FileDialog offers a shortcut property that can be used to access the most common standard paths.
  • Qt Labs Platform : StandardPaths offers a separate type to give full access to the standard paths.
SwipeDelegate
SwipeView
SwitchDelegate
SystemTrayIcon
(Qt Labs Platform)
  • Qt Labs Platform : SystemTrayIcon experimental native system tray icon that uses Qt Widgets as a fallback on platforms where a native implementation is not available in the Qt Platform Abstraction Layer.
TabBar ,
TabButton
TabView
(Qt Quick Controls 1)
  • Qt Quick Controls 1 : TabView offers similar functionality. It combines the tab bar, background and frame around the tabs.
ToolSeparator
ToolTip
  • Qt Quick Controls 1 : Button and 动作 have built-in Qt Widgets-based tooltips.
  • Qt Quick Controls 2 : ToolTip can be attached to any Item .