变更内容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 所使用的表述方式一致。 此前,系统会将传入的日期和时间与控件的时间规范相结合,而忽略该日期和时间本身的时间规范;这可能会描述一个与传入的日期和时间所指代的时间点不同的时刻。
ItemViews
QAbstractItemView 类
以前返回QStyleOptionViewItem 对象的虚拟方法 viewOptions() 已重命名为 initViewItemOption,并初始化通过指针传递的QStyleOptionViewItem 对象。
样式类及相关 API
所有带版本号的QStyleOption 子类均已合并,且版本号已重置为1。
控件类中的各种 initStyleOption() 方法现已成为虚方法。
样式表变更
在 Qt 5 与 Qt 6 中,通过属性设置小部件样式的方式有所不同,尤其是当该属性为枚举类型时。在 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 模块。
© 2026 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.