summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qcombobox_p.h
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2019-12-02 18:54:14 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2019-12-11 13:12:31 +0100
commit3e9895f3de7f118beab3068b938ff6b9f2f5ef73 (patch)
tree2143f8987db7942e63325edf650c26a7c2b16913 /src/widgets/widgets/qcombobox_p.h
parent5a26bf9d65b75db3abefa967beb13b2510719bd5 (diff)
Support checkable QComboBox items with styles using a popup dropdown
The dropdown of a combobox is rendered using menu items when the style request it to do so via the SH_ComboBox_Popup style hint. In that case, checkable items were not supported; the QComboBox didn't pass the checked state correctly to the style, and the delegate used for rendering the items into the list view did not implement modifying the checked state of the item. However, the QStyleOptionMenuItem's checked state and checkType members were set anyway, as on e.g. macOS style we use a checkmark to show which item is currently selected in the combobox. The QStyle::State enum defines State_On and State_Off for toggleable things, so in addition to setting QStyleOptionMenuItem::checked, we are now also adding State_On or State_Off if the model provides a valid checked/unchecked state. Otherwise, we only set the checked state if the item is currently selected. In addition, we implement the delegate to support toggling of checkable model data with mouse and keyboard, using a simplified version of the QItemDelegate implementation. To avoid spurious item toggles when the popup is opened, we only handle mouse releases when the press was on the same row. In the fusion style, we ignore the workaround to let QtQuickControls render comboboxes if State_On or State_Off are set. [ChangeLog][QtWidgets][QComboBox] Support checkable items in styles that use a popup for the dropdown. Change-Id: Ia01519694b0419d777dc66b1ef683482fb01754c Fixes: QTBUG-60310 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Diffstat (limited to 'src/widgets/widgets/qcombobox_p.h')
-rw-r--r--src/widgets/widgets/qcombobox_p.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/widgets/widgets/qcombobox_p.h b/src/widgets/widgets/qcombobox_p.h
index c79406eafd..7a3fcf6e0f 100644
--- a/src/widgets/widgets/qcombobox_p.h
+++ b/src/widgets/widgets/qcombobox_p.h
@@ -266,7 +266,9 @@ class Q_AUTOTEST_EXPORT QComboMenuDelegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
- QComboMenuDelegate(QObject *parent, QComboBox *cmb) : QAbstractItemDelegate(parent), mCombo(cmb) {}
+ QComboMenuDelegate(QObject *parent, QComboBox *cmb)
+ : QAbstractItemDelegate(parent), mCombo(cmb), pressedIndex(-1)
+ {}
protected:
void paint(QPainter *painter,
@@ -282,11 +284,14 @@ protected:
return mCombo->style()->sizeFromContents(
QStyle::CT_MenuItem, &opt, option.rect.size(), mCombo);
}
+ bool editorEvent(QEvent *event, QAbstractItemModel *model,
+ const QStyleOptionViewItem &option, const QModelIndex &index) override;
private:
QStyleOptionMenuItem getStyleOption(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QComboBox *mCombo;
+ int pressedIndex;
};
// ### Qt6: QStyledItemDelegate ?