Preparations to deprecate QItemDelegate

QItemDelegate was superseded since Qt4 by QStyledItemDelegate but it
took until Qt6.7 to remove the last occurrences in qtbase.
 - remove unused includes / replace with qabstractitemdelegate.h
 - replace references in the documentation with QStyledItemDelegate
 - adjust the examples and tests to use QStyledItemDelegate

Pick-to: 6.5 6.6
Change-Id: I246755004ce2d01192a726ca0972106c237df0cc
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-10-04 16:34:16 +02:00
parent 9ed25b413f
commit 4e8b54eb81
18 changed files with 31 additions and 37 deletions

View File

@ -26,17 +26,17 @@ protected:
}; };
// TorrentViewDelegate is used to draw the progress bars. // TorrentViewDelegate is used to draw the progress bars.
class TorrentViewDelegate : public QItemDelegate class TorrentViewDelegate : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT
public: public:
inline TorrentViewDelegate(MainWindow *mainWindow) : QItemDelegate(mainWindow) {} inline TorrentViewDelegate(MainWindow *mainWindow) : QStyledItemDelegate(mainWindow) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index ) const override const QModelIndex &index ) const override
{ {
if (index.column() != 2) { if (index.column() != 2) {
QItemDelegate::paint(painter, option, index); QStyledItemDelegate::paint(painter, option, index);
return; return;
} }

View File

@ -115,7 +115,7 @@
\section1 Delegate Class Definition and Implementation \section1 Delegate Class Definition and Implementation
The delegate we use to mediate interaction between the widget mapper and The delegate we use to mediate interaction between the widget mapper and
the input widgets is a small QItemDelegate subclass: the input widgets is a small QStyledItemDelegate subclass:
\snippet sqlwidgetmapper/delegate.h Delegate class definition \snippet sqlwidgetmapper/delegate.h Delegate class definition
@ -126,7 +126,7 @@
Since we only provide an empty implementation of the constructor, we Since we only provide an empty implementation of the constructor, we
concentrate on the other two functions. concentrate on the other two functions.
The \l{QItemDelegate::}{setEditorData()} implementation takes the data The \l{QStyledItemDelegate::}{setEditorData()} implementation takes the data
referred to by the model index supplied and processes it according to referred to by the model index supplied and processes it according to
the presence of a \c currentIndex property in the editor widget: the presence of a \c currentIndex property in the editor widget:
@ -138,10 +138,10 @@
values needed for the \c currentIndex property. values needed for the \c currentIndex property.
As a result, instead of showing "0", "1" or "2" in the combo box, one of As a result, instead of showing "0", "1" or "2" in the combo box, one of
its predefined set of items is shown. We call QItemDelegate::setEditorData() its predefined set of items is shown. We call QStyledItemDelegate::setEditorData()
for widgets without the \c currentIndex property. for widgets without the \c currentIndex property.
The \l{QItemDelegate::}{setModelData()} implementation performs the reverse The \l{QStyledItemDelegate::}{setModelData()} implementation performs the reverse
process, taking the value stored in the widget's \c currentIndex property process, taking the value stored in the widget's \c currentIndex property
and storing it back in the model: and storing it back in the model:

View File

@ -7,13 +7,13 @@
\examplecategory {User Interface Components} \examplecategory {User Interface Components}
\ingroup examples-itemviews \ingroup examples-itemviews
\brief This example shows how to create an editor that can be used by \brief This example shows how to create an editor that can be used by
a QItemDelegate. a QStyledItemDelegate.
\image coloreditorfactoryimage.png \image coloreditorfactoryimage.png
When editing data in a QListView, QTableView, or QTreeView, When editing data in a QListView, QTableView, or QTreeView,
editors are created and displayed by a \l{Delegate editors are created and displayed by a \l{Delegate
Classes}{delegate}. QItemDelegate, which is the default delegate Classes}{delegate}. QStyledItemDelegate, which is the default delegate
used by Qt's \l{View Classes}{item views}, uses a used by Qt's \l{View Classes}{item views}, uses a
QItemEditorFactory to create editors for it. A unique instance QItemEditorFactory to create editors for it. A unique instance
provided by QItemEditorFactory is by default installed on all provided by QItemEditorFactory is by default installed on all
@ -27,7 +27,7 @@
In this example, we will create an editor (implemented in the \c In this example, we will create an editor (implemented in the \c
ColorListEditor class) that can edit the QColor data type and be ColorListEditor class) that can edit the QColor data type and be
used by \l{QItemDelegate}s. We do this by creating a new used by \l{QStyledItemDelegate}s. We do this by creating a new
QItemEditorCreatorBase that produces \c ColorListEditors and QItemEditorCreatorBase that produces \c ColorListEditors and
register it with a new factory, which we set as the default editor register it with a new factory, which we set as the default editor
item factory (the unique factory instance). To test our editor, we item factory (the unique factory instance). To test our editor, we
@ -67,7 +67,7 @@
\snippet itemviews/coloreditorfactory/colorlisteditor.h 0 \snippet itemviews/coloreditorfactory/colorlisteditor.h 0
QItemDelegate manages the interaction between the editor and QStyledItemDelegate manages the interaction between the editor and
the model, i.e., it retrieves data to edit from the model and the model, i.e., it retrieves data to edit from the model and
store data from the editor in the model. The data that is edited store data from the editor in the model. The data that is edited
by an editor is stored in the editor's user data property, and the by an editor is stored in the editor's user data property, and the
@ -106,7 +106,7 @@
usually sufficient to provide custom editors. Further usually sufficient to provide custom editors. Further
customization is achieved by subclassing QItemEditorFactory customization is achieved by subclassing QItemEditorFactory
and QItemEditorCreatorBase. It is also possible to subclass and QItemEditorCreatorBase. It is also possible to subclass
QItemDelegate if you don't wish to use a factory at all. QStyledItemDelegate if you don't wish to use a factory at all.
Possible suggestions are: Possible suggestions are:
@ -128,5 +128,5 @@
In this example, we use a standard QVariant data type. You can In this example, we use a standard QVariant data type. You can
also use custom types. In the \l{Star Delegate Example}, we also use custom types. In the \l{Star Delegate Example}, we
show how to store a custom data type in a QVariant and paint show how to store a custom data type in a QVariant and paint
and edit it in a class that inherits QItemDelegate. and edit it in a class that inherits QStyledItemDelegate.
*/ */

View File

@ -88,7 +88,7 @@
\section1 Delegate Class Definition and Implementation \section1 Delegate Class Definition and Implementation
The delegate we use to mediate interaction between the widget mapper and The delegate we use to mediate interaction between the widget mapper and
the input widgets is a small QItemDelegate subclass: the input widgets is a small QStyledItemDelegate subclass:
\snippet itemviews/combowidgetmapper/delegate.h Delegate class definition \snippet itemviews/combowidgetmapper/delegate.h Delegate class definition
@ -99,7 +99,7 @@
Since we only provide an empty implementation of the constructor, we Since we only provide an empty implementation of the constructor, we
concentrate on the other two functions. concentrate on the other two functions.
The \l{QItemDelegate::}{setEditorData()} implementation takes the data The \l{QStyledItemDelegate::}{setEditorData()} implementation takes the data
referred to by the model index supplied and processes it according to referred to by the model index supplied and processes it according to
the presence of a \c currentIndex property in the editor widget: the presence of a \c currentIndex property in the editor widget:
@ -111,10 +111,10 @@
values needed for the \c currentIndex property. values needed for the \c currentIndex property.
As a result, instead of showing "0", "1" or "2" in the combo box, one of As a result, instead of showing "0", "1" or "2" in the combo box, one of
its predefined set of items is shown. We call QItemDelegate::setEditorData() its predefined set of items is shown. We call QStyledItemDelegate::setEditorData()
for widgets without the \c currentIndex property. for widgets without the \c currentIndex property.
The \l{QItemDelegate::}{setModelData()} implementation performs the reverse The \l{QStyledItemDelegate::}{setModelData()} implementation performs the reverse
process, taking the value stored in the widget's \c currentIndex property process, taking the value stored in the widget's \c currentIndex property
and storing it back in the model: and storing it back in the model:

View File

@ -496,11 +496,11 @@
submitted. submitted.
The items in the view are rendered using a delegate. The default The items in the view are rendered using a delegate. The default
delegate, QItemDelegate, handles the most common data types (\c delegate, QStyledItemDelegate, handles the most common data types (\c
int, QString, QImage, etc.). The delegate is also responsible for int, QString, QImage, etc.). The delegate is also responsible for
providing editor widgets (e.g., a combobox) when the user starts providing editor widgets (e.g., a combobox) when the user starts
editing an item in the view. You can create your own delegates by editing an item in the view. You can create your own delegates by
subclassing QAbstractItemDelegate or QItemDelegate. See subclassing QAbstractItemDelegate or QStyledItemDelegate. See
\l{Model/View Programming} for more information. \l{Model/View Programming} for more information.
QSqlTableModel is optimized to operate on a single table at a QSqlTableModel is optimized to operate on a single table at a

View File

@ -4,7 +4,6 @@
#include "qdatawidgetmapper.h" #include "qdatawidgetmapper.h"
#include "qabstractitemmodel.h" #include "qabstractitemmodel.h"
#include "qitemdelegate.h"
#include "qmetaobject.h" #include "qmetaobject.h"
#include "qwidget.h" #include "qwidget.h"
#include "qstyleditemdelegate.h" #include "qstyleditemdelegate.h"
@ -234,7 +233,7 @@ void QDataWidgetMapperPrivate::modelDestroyed()
instead of the default user property. instead of the default user property.
It is possible to set an item delegate to support custom widgets. By default, It is possible to set an item delegate to support custom widgets. By default,
a QItemDelegate is used to synchronize the model with the widgets. a QStyledItemDelegate is used to synchronize the model with the widgets.
Let us assume that we have an item model named \c{model} with the following contents: Let us assume that we have an item model named \c{model} with the following contents:

View File

@ -3,7 +3,6 @@
#include "qlistwidget.h" #include "qlistwidget.h"
#include <qitemdelegate.h>
#include <private/qlistview_p.h> #include <private/qlistview_p.h>
#include <private/qwidgetitemdata_p.h> #include <private/qwidgetitemdata_p.h>
#include <private/qlistwidget_p.h> #include <private/qlistwidget_p.h>

View File

@ -18,7 +18,6 @@
#include <QtCore/qabstractitemmodel.h> #include <QtCore/qabstractitemmodel.h>
#include <QtWidgets/qabstractitemview.h> #include <QtWidgets/qabstractitemview.h>
#include <QtWidgets/qlistwidget.h> #include <QtWidgets/qlistwidget.h>
#include <qitemdelegate.h>
#include <private/qlistview_p.h> #include <private/qlistview_p.h>
#include <private/qwidgetitemdata_p.h> #include <private/qwidgetitemdata_p.h>

View File

@ -6,7 +6,7 @@
#include "qtableview.h" #include "qtableview.h"
#include <qheaderview.h> #include <qheaderview.h>
#include <qitemdelegate.h> #include <qabstractitemdelegate.h>
#include <qapplication.h> #include <qapplication.h>
#include <qpainter.h> #include <qpainter.h>
#include <qstyle.h> #include <qstyle.h>

View File

@ -3,7 +3,6 @@
#include "qtablewidget.h" #include "qtablewidget.h"
#include <qitemdelegate.h>
#include <qpainter.h> #include <qpainter.h>
#include <private/qtablewidget_p.h> #include <private/qtablewidget_p.h>

View File

@ -3,7 +3,7 @@
#include "qtreeview.h" #include "qtreeview.h"
#include <qheaderview.h> #include <qheaderview.h>
#include <qitemdelegate.h> #include <qabstractitemdelegate.h>
#include <qapplication.h> #include <qapplication.h>
#include <qscrollbar.h> #include <qscrollbar.h>
#include <qpainter.h> #include <qpainter.h>

View File

@ -5,7 +5,6 @@
#include <qheaderview.h> #include <qheaderview.h>
#include <qpainter.h> #include <qpainter.h>
#include <qitemdelegate.h>
#include <qstack.h> #include <qstack.h>
#include <qdebug.h> #include <qdebug.h>
#include <private/qtreewidget_p.h> #include <private/qtreewidget_p.h>

View File

@ -12,7 +12,7 @@
#if QT_CONFIG(tableview) #if QT_CONFIG(tableview)
#include <qtableview.h> #include <qtableview.h>
#endif #endif
#include <qitemdelegate.h> #include <qabstractitemdelegate.h>
#include <qmap.h> #include <qmap.h>
#if QT_CONFIG(menu) #if QT_CONFIG(menu)
#include <qmenu.h> #include <qmenu.h>

View File

@ -3,9 +3,9 @@
#include "qfontcombobox.h" #include "qfontcombobox.h"
#include <qabstractitemdelegate.h>
#include <qaccessible.h> #include <qaccessible.h>
#include <qstringlistmodel.h> #include <qstringlistmodel.h>
#include <qitemdelegate.h>
#include <qlistview.h> #include <qlistview.h>
#include <qpainter.h> #include <qpainter.h>
#include <qevent.h> #include <qevent.h>

View File

@ -13,7 +13,7 @@
#include <qsharedpointer.h> #include <qsharedpointer.h>
#include <qfiledialog.h> #include <qfiledialog.h>
#include <qabstractitemdelegate.h> #include <qabstractitemdelegate.h>
#include <qitemdelegate.h> #include <qstyleditemdelegate.h>
#include <qlistview.h> #include <qlistview.h>
#include <qcombobox.h> #include <qcombobox.h>
#include <qpushbutton.h> #include <qpushbutton.h>
@ -806,7 +806,7 @@ void tst_QFiledialog::itemDelegate()
{ {
QFileDialog fd; QFileDialog fd;
QVERIFY(fd.itemDelegate() != 0); QVERIFY(fd.itemDelegate() != 0);
QItemDelegate *id = new QItemDelegate(&fd); QStyledItemDelegate *id = new QStyledItemDelegate(&fd);
fd.setItemDelegate(id); fd.setItemDelegate(id);
QCOMPARE(fd.itemDelegate(), (QAbstractItemDelegate *)id); QCOMPARE(fd.itemDelegate(), (QAbstractItemDelegate *)id);
} }

View File

@ -11,7 +11,6 @@
#include <qdebug.h> #include <qdebug.h>
#include <qfiledialog.h> #include <qfiledialog.h>
#include <qabstractitemdelegate.h> #include <qabstractitemdelegate.h>
#include <qitemdelegate.h>
#include <qlistview.h> #include <qlistview.h>
#include <qcombobox.h> #include <qcombobox.h>
#include <qpushbutton.h> #include <qpushbutton.h>

View File

@ -5,7 +5,7 @@
#include <QApplication> #include <QApplication>
#include <QTableView> #include <QTableView>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QItemDelegate> #include <QStyledItemDelegate>
#include <QDebug> #include <QDebug>
#include <QComboBox> #include <QComboBox>
@ -16,10 +16,10 @@ public:
~ExampleEditor() { QApplication::instance()->quit(); } ~ExampleEditor() { QApplication::instance()->quit(); }
}; };
class ExampleDelegate : public QItemDelegate class ExampleDelegate : public QStyledItemDelegate
{ {
public: public:
ExampleDelegate() : QItemDelegate() ExampleDelegate() : QStyledItemDelegate()
{ {
m_editor = new ExampleEditor(0); m_editor = new ExampleEditor(0);
m_combobox = new QComboBox(0); m_combobox = new QComboBox(0);

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTableView> #include <QTableView>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QItemDelegate> #include <QStyledItemDelegate>
#include <QItemEditorFactory> #include <QItemEditorFactory>
#include <QDoubleSpinBox> #include <QDoubleSpinBox>
@ -38,7 +38,7 @@ LocaleWidget::LocaleWidget(QWidget *parent)
m_model = new LocaleModel(this); m_model = new LocaleModel(this);
m_view = new QTableView(this); m_view = new QTableView(this);
QItemDelegate *delegate = qobject_cast<QItemDelegate*>(m_view->itemDelegate()); QStyledItemDelegate *delegate = qobject_cast<QStyledItemDelegate*>(m_view->itemDelegate());
Q_ASSERT(delegate != 0); Q_ASSERT(delegate != 0);
static EditorFactory editor_factory; static EditorFactory editor_factory;
delegate->setItemEditorFactory(&editor_factory); delegate->setItemEditorFactory(&editor_factory);