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 在特定顯示;注意,這不會將 Widget 移至虛擬桌麵設置屏幕。

QAction, QActionGroup

這些類已被移入 QtGui 模塊。從屬類型的成員函數的定義在 QtWidgets (譬如 QAction::menu () 和 QAction::setMenu ()) 的實現是作為實例化模闆,僅當調用時。

小部件

QAbstractButton 類

默認超時參數為 QAbstractButton::animateClick () 被移除以允許現代連接句法無需 qOverload .

QComboBox 類

The QComboBox::setModel () 函數現在是虛擬的。

QDateTimeEdit 類

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.

項視圖

QAbstractItemView 類

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() 方法都是虛擬的。

樣式錶變化

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 模塊。