From d49a7412f55390e461773f4ffc36a82958d59b6d Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 3 Sep 2020 10:51:40 +0200 Subject: Derive some item delegates from QStyledItemDelegate Address a ### Qt6 comment from change 283008e123e5eacb83869682528b2024186634f8, and start using QStyledItemDelegate in more places, so those get proper look and feel. Change-Id: I39767ba99b7942faada1fba0ac241deb35563b63 Reviewed-by: Volker Hilsheimer Reviewed-by: Christian Ehrlicher --- examples/sql/books/bookdelegate.cpp | 7 +--- src/sql/models/qsqlrelationaldelegate.h | 55 ++++++++++++++--------------- src/widgets/itemviews/qdatawidgetmapper.cpp | 4 +-- src/widgets/widgets/qcombobox_p.h | 19 +++++----- 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/examples/sql/books/bookdelegate.cpp b/examples/sql/books/bookdelegate.cpp index 00bdd1708a..56d9b1f414 100644 --- a/examples/sql/books/bookdelegate.cpp +++ b/examples/sql/books/bookdelegate.cpp @@ -61,10 +61,7 @@ void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() != 5) { - QStyleOptionViewItem opt = option; - // Since we draw the grid ourselves: - opt.rect.adjust(0, 0, -1, -1); - QSqlRelationalDelegate::paint(painter, opt, index); + QSqlRelationalDelegate::paint(painter, option, index); } else { const QAbstractItemModel *model = index.model(); QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? @@ -87,8 +84,6 @@ void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, painter->drawPixmap(x, y, star); x += width; } - // Since we draw the grid ourselves: - drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); } QPen pen = painter->pen(); diff --git a/src/sql/models/qsqlrelationaldelegate.h b/src/sql/models/qsqlrelationaldelegate.h index 189fc876fd..130c82868e 100644 --- a/src/sql/models/qsqlrelationaldelegate.h +++ b/src/sql/models/qsqlrelationaldelegate.h @@ -46,7 +46,7 @@ QT_REQUIRE_CONFIG(sqlmodel); #ifdef QT_WIDGETS_LIB -#include +#include #if QT_CONFIG(listview) #include #endif @@ -58,8 +58,7 @@ QT_REQUIRE_CONFIG(sqlmodel); #include QT_BEGIN_NAMESPACE -// ### Qt6: QStyledItemDelegate -class QSqlRelationalDelegate: public QItemDelegate +class QSqlRelationalDelegate : public QStyledItemDelegate { static int fieldIndex(const QSqlTableModel *const model, const QSqlDriver *const driver, @@ -73,31 +72,31 @@ class QSqlRelationalDelegate: public QItemDelegate public: -explicit QSqlRelationalDelegate(QObject *aParent = nullptr) - : QItemDelegate(aParent) -{} + explicit QSqlRelationalDelegate(QObject *aParent = nullptr) + : QStyledItemDelegate(aParent) + {} -~QSqlRelationalDelegate() -{} + ~QSqlRelationalDelegate() + {} -QWidget *createEditor(QWidget *aParent, - const QStyleOptionViewItem &option, - const QModelIndex &index) const override -{ - const QSqlRelationalTableModel *sqlModel = qobject_cast(index.model()); - QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; - if (!childModel) - return QItemDelegate::createEditor(aParent, option, index); - const QSqlDriver *const driver = childModel->database().driver(); - - QComboBox *combo = new QComboBox(aParent); - combo->setModel(childModel); - combo->setModelColumn(fieldIndex(childModel, driver, - sqlModel->relation(index.column()).displayColumn())); - combo->installEventFilter(const_cast(this)); - - return combo; -} + QWidget *createEditor(QWidget *aParent, + const QStyleOptionViewItem &option, + const QModelIndex &index) const override + { + const QSqlRelationalTableModel *sqlModel = qobject_cast(index.model()); + QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; + if (!childModel) + return QStyledItemDelegate::createEditor(aParent, option, index); + const QSqlDriver *const driver = childModel->database().driver(); + + QComboBox *combo = new QComboBox(aParent); + combo->setModel(childModel); + combo->setModelColumn(fieldIndex(childModel, driver, + sqlModel->relation(index.column()).displayColumn())); + combo->installEventFilter(const_cast(this)); + + return combo; + } void setEditorData(QWidget *editor, const QModelIndex &index) const override { @@ -117,7 +116,7 @@ QWidget *createEditor(QWidget *aParent, return; } } - QItemDelegate::setEditorData(editor, index); + QStyledItemDelegate::setEditorData(editor, index); } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override @@ -129,7 +128,7 @@ void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; QComboBox *combo = qobject_cast(editor); if (!sqlModel || !childModel || !combo) { - QItemDelegate::setModelData(editor, model, index); + QStyledItemDelegate::setModelData(editor, model, index); return; } const QSqlDriver *const driver = childModel->database().driver(); diff --git a/src/widgets/itemviews/qdatawidgetmapper.cpp b/src/widgets/itemviews/qdatawidgetmapper.cpp index 403ea33c0b..0f6e36cc6a 100644 --- a/src/widgets/itemviews/qdatawidgetmapper.cpp +++ b/src/widgets/itemviews/qdatawidgetmapper.cpp @@ -43,6 +43,7 @@ #include "qitemdelegate.h" #include "qmetaobject.h" #include "qwidget.h" +#include "qstyleditemdelegate.h" #include "private/qobject_p.h" #include "private/qabstractitemmodel_p.h" @@ -326,8 +327,7 @@ void QDataWidgetMapperPrivate::_q_modelDestroyed() QDataWidgetMapper::QDataWidgetMapper(QObject *parent) : QObject(*new QDataWidgetMapperPrivate, parent) { - // ### Qt6: QStyledItemDelegate - setItemDelegate(new QItemDelegate(this)); + setItemDelegate(new QStyledItemDelegate(this)); } /*! diff --git a/src/widgets/widgets/qcombobox_p.h b/src/widgets/widgets/qcombobox_p.h index a7e7af64a1..16ded8f1a4 100644 --- a/src/widgets/widgets/qcombobox_p.h +++ b/src/widgets/widgets/qcombobox_p.h @@ -56,7 +56,7 @@ #include "QtWidgets/qabstractslider.h" #include "QtWidgets/qapplication.h" -#include "QtWidgets/qitemdelegate.h" +#include "QtWidgets/qstyleditemdelegate.h" #include "QtGui/qstandarditemmodel.h" #include "QtWidgets/qlineedit.h" #include "QtWidgets/qlistview.h" @@ -293,14 +293,13 @@ private: int pressedIndex; }; -// ### Qt6: QStyledItemDelegate ? -// Note that this class is intentionally not using QStyledItemDelegate -// Vista does not use the new theme for combo boxes and there might -// be other side effects from using the new class -class Q_AUTOTEST_EXPORT QComboBoxDelegate : public QItemDelegate -{ Q_OBJECT +class Q_AUTOTEST_EXPORT QComboBoxDelegate : public QStyledItemDelegate +{ + Q_OBJECT public: - QComboBoxDelegate(QObject *parent, QComboBox *cmb) : QItemDelegate(parent), mCombo(cmb) {} + QComboBoxDelegate(QObject *parent, QComboBox *cmb) + : QStyledItemDelegate(parent), mCombo(cmb) + {} static bool isSeparator(const QModelIndex &index) { return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator"); @@ -324,7 +323,7 @@ protected: opt.rect = rect; mCombo->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, mCombo); } else { - QItemDelegate::paint(painter, option, index); + QStyledItemDelegate::paint(painter, option, index); } } @@ -334,7 +333,7 @@ protected: int pm = mCombo->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, mCombo); return QSize(pm, pm); } - return QItemDelegate::sizeHint(option, index); + return QStyledItemDelegate::sizeHint(option, index); } private: QComboBox *mCombo; -- cgit v1.2.3