Qt Widgets 的变化

Qt 6 是努力使框架更高效,且更易于使用的结果。

为兼容每个发行的所有公共 API,我们试着维护二进制和源代码。但是,为使 Qt 成为更优框架,一些改变是不可避免的。

在此话题中,我们汇总了那些 Qt Widgets 变化,并提供处理它们的指导。

内核类

The QWidget class

虚拟 QWidget::enterEvent () 处理程序现在接收 QEnterEvent * 参数 (拥有指针位置相关信息),而不是纯 QEvent *.

QDesktopWidget 和 QApplication::desktop()

QDesktopWidget 在 Qt 5 已弃用,且在 Qt 6 同 QApplication::desktop() 一起被移除。

QScreen 提供等效功能以查询可用屏幕、形成虚拟桌面的屏幕及屏幕几何体的有关信息。

使用 QWidget::setScreen () 以创建 QWidget 在特定显示;注意,这不会将 Widget 移至虚拟桌面设置屏幕。

QAction, QActionGroup

这些类已被移入 QtGui 模块。从属类型的成员函数的定义在 QtWidgets (譬如 QAction::menu () 和 QAction::setMenu ()) are implemented as templates that will be instantiated only when called.

小部件

The QAbstractButton class

默认超时参数为 QAbstractButton::animateClick () 被移除以允许现代连接句法无需 qOverload .

The QComboBox class

The QComboBox::setModel () 函数现在是虚拟的。

The QDateTimeEdit class

QDateTimeEdit::setDateTime () is called with a date-time whose time-spec doesn't match that of the QDateTimeEdit instance, the date-time is converted to the time-spec of the QDateTimeEdit . This gives a date-time that describes the same instant in time, but does so in the same terms as the QDateTimeEdit uses. Previously, the date and time from the passed date-time were combined with the time-spec of the widget, ignoring the time-spec of the date-time; this could describe a different point in time than the one described by the date-time passed.

ItemViews

The QAbstractItemView class

The virtual viewOptions() method that previously returned a QStyleOptionViewItem object has been renamed to initViewItemOption, and initializes a QStyleOptionViewItem object that's passed in through a pointer.

所有版本 QStyleOption 子类被整合,且版本号重置为 1。

现在,Widget 类的各种 initStyleOption() 方法都是虚拟的。

Style sheet changes

Styling a widget by its property in Qt 5 vs Qt 6 is different, especially if the property is an enum. In Qt 5, the selector value for such a property is the integer equivalent of the enum value, while in Qt 6 the string value is used. The following example demonstrates this difference:

// Qt 5 style sheet
QToolButton[popupMode="1"] {
    padding-right: 20px;
}
					
// Qt 6 style sheet
QToolButton[popupMode=MenuButtonPopup] {
    padding-right: 20px;
}
					

实用类

QUndoCommand、QUndoStack 及 QUndoGroup

撤消/重做框架的 Widget 无关类已被移入 QtGui 模块。