对Qt Widgets
Qt 6 的改动是有意识地使框架更高效、更易用的结果。
我们努力在每个版本中保持所有公共 API 的二进制和源代码兼容性。但为了使 Qt 成为更好的框架,有些改动是不可避免的。
在本专题中,我们总结了Qt Widgets 中的这些变化,并提供了处理这些变化的指导。
内核类
QWidget 类
虚拟QWidget::enterEvent() 处理程序现在接收一个包含指针位置信息的QEnterEvent* 参数,而不是简单的QEvent*。
QDesktopWidget 和 QApplication::desktop()
QDesktopWidget 在 Qt 5 中已被弃用,在 Qt 6 中与 QApplication::desktop() 一起被移除。
QScreen 提供了查询可用屏幕、组成虚拟桌面的屏幕以及屏幕几何形状等信息的同等功能。
使用QWidget::setScreen() 可在特定显示屏上创建QWidget ;请注意,这并不能将部件移动到虚拟桌面设置中的屏幕上。
QAction、QActionGroup
这些类已移入QtGui 模块。依赖于QtWidgets 中定义的类型(如QAction::menu() 和QAction::setMenu() )的成员函数将作为模板实现,只有在调用时才会实例化。
小工具
QAbstractButton 类
删除了QAbstractButton::animateClick() 的默认超时参数,以便使用现代连接语法而无需qOverload 。
QComboBox 类
QComboBox::setModel() 函数现在是虚拟的。
QDateTimeEdit 类
当QDateTimeEdit::setDateTime() 被调用时,如果日期时间的时间规格与QDateTimeEdit 实例的时间规格不一致,日期时间就会被转换为QDateTimeEdit 的时间规格。这样,日期时间就能描述相同的时间瞬间,但与QDateTimeEdit 使用的术语相同。在此之前,传递的日期时间中的日期和时间与 widget 的时间规格相结合,而忽略了日期时间的时间规格;这可能会描述与传递的日期时间所描述的时间点不同的时间点。
项目视图
QAbstractItemView 类
以前返回QStyleOptionViewItem 对象的虚拟 viewOptions() 方法已更名为 initViewItemOption,该方法将初始化一个通过指针传入的QStyleOptionViewItem 对象。
样式类和相关应用程序接口
合并了所有版本化的QStyleOption 子类,并将版本号重置为 1。
部件类中的各种 initStyleOption() 方法现在都是虚拟的。
样式表更改
在 Qt 5 和 Qt 6 中,通过 widget 的属性为其设计样式是不同的,尤其是当属性是一个枚举时。在 Qt 5 中,此类属性的选择器值是枚举值的整数等价物,而在 Qt 6 中使用的是字符串值。下面的示例演示了这种差异:
// Qt 5 style sheet QToolButton[popupMode="1"] { padding-right: 20px; }
// Qt 6 style sheet QToolButton[popupMode=MenuButtonPopup] { padding-right: 20px; }
实用工具类
QUndoCommand、QUndoStack 和 QUndoGroup
撤消/重做框架中与部件无关的类已移入QtGui 模块。
© 2025 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.