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 <volker.hilsheimer@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Lars Knoll 2020-09-03 10:51:40 +02:00 committed by Volker Hilsheimer
parent ae7442a4e9
commit d49a7412f5
4 changed files with 37 additions and 44 deletions

View File

@ -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();

View File

@ -46,7 +46,7 @@ QT_REQUIRE_CONFIG(sqlmodel);
#ifdef QT_WIDGETS_LIB
#include <QtWidgets/qitemdelegate.h>
#include <QtWidgets/qstyleditemdelegate.h>
#if QT_CONFIG(listview)
#include <QtWidgets/qlistview.h>
#endif
@ -58,8 +58,7 @@ QT_REQUIRE_CONFIG(sqlmodel);
#include <QtCore/qmetaobject.h>
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<const QSqlRelationalTableModel *>(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();
QWidget *createEditor(QWidget *aParent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(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<QSqlRelationalDelegate *>(this));
QComboBox *combo = new QComboBox(aParent);
combo->setModel(childModel);
combo->setModelColumn(fieldIndex(childModel, driver,
sqlModel->relation(index.column()).displayColumn()));
combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this));
return combo;
}
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<QComboBox *>(editor);
if (!sqlModel || !childModel || !combo) {
QItemDelegate::setModelData(editor, model, index);
QStyledItemDelegate::setModelData(editor, model, index);
return;
}
const QSqlDriver *const driver = childModel->database().driver();

View File

@ -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));
}
/*!

View File

@ -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;