diff --git a/doc/src/images/coloreditorfactoryimage.png b/doc/src/images/coloreditorfactoryimage.png deleted file mode 100644 index cd839a6db60..00000000000 Binary files a/doc/src/images/coloreditorfactoryimage.png and /dev/null differ diff --git a/examples/widgets/doc/src/coloreditorfactory.qdoc b/examples/widgets/doc/src/coloreditorfactory.qdoc deleted file mode 100644 index f43a7eb95ca..00000000000 --- a/examples/widgets/doc/src/coloreditorfactory.qdoc +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example itemviews/coloreditorfactory - \title Color Editor Factory Example - \examplecategory {User Interface Components} - \ingroup examples-itemviews - \brief This example shows how to create an editor that can be used by - a QStyledItemDelegate. - - \image coloreditorfactoryimage.png - - When editing data in a QListView, QTableView, or QTreeView, - editors are created and displayed by a \l{Delegate - Classes}{delegate}. QStyledItemDelegate, which is the default delegate - used by Qt's \l{View Classes}{item views}, uses a - QItemEditorFactory to create editors for it. A unique instance - provided by QItemEditorFactory is by default installed on all - item delegates. - - An item editor factory contains a collection of - QItemEditorCreatorBase instances, which are specialized factories - that produce editors for one particular QVariant data type (all - models in Qt store their data in \l{QVariant}s). An editor can be any - Qt or custom widget. - - In this example, we will create an editor (implemented in the \c - ColorListEditor class) that can edit the QColor data type and be - used by \l{QStyledItemDelegate}s. We do this by creating a new - QItemEditorCreatorBase that produces \c ColorListEditors and - register it with a new factory, which we set as the default editor - item factory (the unique factory instance). To test our editor, we - have implemented the \c Window class, which displays a - QTableWidget in which \l{QColor}s can be edited. - - \section1 Window Class Implementation - - In the Window class, we create the item editor creator - base for our color editor and add it to the default factory. - We also create a QTableWidget in which our editor can be - tested. It is filled with some data and displayed in a window. - - We take a closer look at the constructor: - - \snippet itemviews/coloreditorfactory/window.cpp 0 - - The QStandardItemEditorCreator is a convenience class that - inherits QItemEditorCreatorBase. Its constructor takes a template - class, of which instances are returned from - \l{QItemEditorCreatorBase::}{createWidget()}. The creator uses a - constructor that takes a QWidget as its only parameter; the - template class must provide this. This way, there is no need to - subclass QStandardItemEditorCreator. - - After the new factory has been set, all standard item delegates - will use it (i.e, also delegates that were created before the new - default factory was set). - - The \c createGUI() function sets up the table and fills it - with data. - - \section1 ColorListEditor Definition - - The ColorListEditor inherits QComboBox and lets the user - select a QColor from its popup list. - - \snippet itemviews/coloreditorfactory/colorlisteditor.h 0 - - QStyledItemDelegate manages the interaction between the editor 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 - by an editor is stored in the editor's user data property, and the - delegate uses Qt's \l{Qt's Property System}{property system} to - access it by name. We declare our user data property with the - Q_PROPERTY macro. The property is set to be the user type with the - USER keyword. - - \section1 ColorListEditor Implementation - - The constructor of \c ColorListEditor simply calls \c - populateList(), which we will look at later. We move on to the - \c color() function: - - \snippet itemviews/coloreditorfactory/colorlisteditor.cpp 0 - - We return the data that is selected in the combobox. The data - is stored in the Qt::DecorationRole as the color is then also - displayed in the popup list (as shown in the image above). - - \snippet itemviews/coloreditorfactory/colorlisteditor.cpp 1 - - The \c findData() function searches the items in the combobox - and returns the index of the item that has \c color in the - Qt::Decoration role. - - \snippet itemviews/coloreditorfactory/colorlisteditor.cpp 2 - - Qt knows some predefined colors by name. We simply loop - through these to fill our editor with items. - - \section1 Further Customization of Item View Editors - - You can customize Qt's \l{Model/View Programming}{model view - framework} in many ways. The procedure shown in this example is - usually sufficient to provide custom editors. Further - customization is achieved by subclassing QItemEditorFactory - and QItemEditorCreatorBase. It is also possible to subclass - QStyledItemDelegate if you don't wish to use a factory at all. - - Possible suggestions are: - - \list - \li If the editor widget has no user property defined, the delegate - asks the factory for the property name, which it in turn - asks the item editor creator for. In this case, you can use - the QItemEditorCreator class, which takes the property - name to use for editing as a constructor argument. - \li If the editor requires other constructors or other - initialization than provided by QItemEditorCreatorBase, you - must reimplement - QItemEditorCreatorBase::createWidget(). - \li You could also subclass QItemEditorFactory if you only want - to provide editors for certain kinds of data or use another - method of creating the editors than using creator bases. - \endlist - - In this example, we use a standard QVariant data type. You can - also use custom types. In the \l{Star Delegate Example}, we - show how to store a custom data type in a QVariant and paint - and edit it in a class that inherits QStyledItemDelegate. -*/ diff --git a/examples/widgets/itemviews/CMakeLists.txt b/examples/widgets/itemviews/CMakeLists.txt index 0c2912178f6..e80d90160be 100644 --- a/examples/widgets/itemviews/CMakeLists.txt +++ b/examples/widgets/itemviews/CMakeLists.txt @@ -3,7 +3,6 @@ qt_internal_add_example(addressbook) qt_internal_add_example(basicsortfiltermodel) -qt_internal_add_example(coloreditorfactory) qt_internal_add_example(combowidgetmapper) qt_internal_add_example(customsortfiltermodel) qt_internal_add_example(editabletreemodel) diff --git a/examples/widgets/itemviews/coloreditorfactory/CMakeLists.txt b/examples/widgets/itemviews/coloreditorfactory/CMakeLists.txt deleted file mode 100644 index 5dbc780027b..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(coloreditorfactory LANGUAGES CXX) - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) - -qt_standard_project_setup() - -qt_add_executable(coloreditorfactory - colorlisteditor.cpp colorlisteditor.h - main.cpp - window.cpp window.h -) - -set_target_properties(coloreditorfactory PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(coloreditorfactory PRIVATE - Qt6::Core - Qt6::Gui - Qt6::Widgets -) - -install(TARGETS coloreditorfactory - BUNDLE DESTINATION . - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} -) - -qt_generate_deploy_app_script( - TARGET coloreditorfactory - OUTPUT_SCRIPT deploy_script - NO_UNSUPPORTED_PLATFORM_ERROR -) -install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/itemviews/coloreditorfactory/coloreditorfactory.pro b/examples/widgets/itemviews/coloreditorfactory/coloreditorfactory.pro deleted file mode 100644 index 2d16ab5e2b7..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/coloreditorfactory.pro +++ /dev/null @@ -1,12 +0,0 @@ -QT += widgets -requires(qtConfig(combobox)) - -HEADERS = colorlisteditor.h \ - window.h -SOURCES = colorlisteditor.cpp \ - window.cpp \ - main.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/coloreditorfactory -INSTALLS += target diff --git a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp deleted file mode 100644 index 44ce168c931..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "colorlisteditor.h" - -#include - -ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget) -{ - populateList(); -} - -//! [0] -QColor ColorListEditor::color() const -{ - return qvariant_cast(itemData(currentIndex(), Qt::DecorationRole)); -} -//! [0] - -//! [1] -void ColorListEditor::setColor(const QColor &color) -{ - setCurrentIndex(findData(color, Qt::DecorationRole)); -} -//! [1] - -//! [2] -void ColorListEditor::populateList() -{ - const QStringList colorNames = QColor::colorNames(); - - for (int i = 0; i < colorNames.size(); ++i) { - QColor color(colorNames[i]); - - insertItem(i, colorNames[i]); - setItemData(i, color, Qt::DecorationRole); - } -} -//! [2] diff --git a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h deleted file mode 100644 index 8dfa9d9f059..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef COLORLISTEDITOR_H -#define COLORLISTEDITOR_H - -#include - -QT_BEGIN_NAMESPACE -class QColor; -class QWidget; -QT_END_NAMESPACE - -//! [0] -class ColorListEditor : public QComboBox -{ - Q_OBJECT - Q_PROPERTY(QColor color READ color WRITE setColor USER true) - -public: - ColorListEditor(QWidget *widget = nullptr); - -public: - QColor color() const; - void setColor(const QColor &color); - -private: - void populateList(); -}; -//! [0] - -#endif diff --git a/examples/widgets/itemviews/coloreditorfactory/main.cpp b/examples/widgets/itemviews/coloreditorfactory/main.cpp deleted file mode 100644 index c4339e22c17..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/main.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include - -#include "window.h" - -int main(int argv, char **args) -{ - QApplication app(argv, args); - - Window window; - window.show(); - - return app.exec(); -} diff --git a/examples/widgets/itemviews/coloreditorfactory/window.cpp b/examples/widgets/itemviews/coloreditorfactory/window.cpp deleted file mode 100644 index 31c8c1ae137..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/window.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "window.h" -#include "colorlisteditor.h" - -#include - -//! [0] -Window::Window() -{ - QItemEditorFactory *factory = new QItemEditorFactory; - - QItemEditorCreatorBase *colorListCreator = - new QStandardItemEditorCreator(); - - factory->registerEditor(QMetaType::QColor, colorListCreator); - - QItemEditorFactory::setDefaultFactory(factory); - - createGUI(); -} -//! [0] - -void Window::createGUI() -{ - const QList> list = { { tr("Alice"), QColor("aliceblue") }, - { tr("Neptun"), QColor("aquamarine") }, - { tr("Ferdinand"), QColor("springgreen") } }; - - QTableWidget *table = new QTableWidget(3, 2); - table->setHorizontalHeaderLabels({ tr("Name"), tr("Hair Color") }); - table->verticalHeader()->setVisible(false); - table->resize(150, 50); - - for (int i = 0; i < 3; ++i) { - const QPair &pair = list.at(i); - - QTableWidgetItem *nameItem = new QTableWidgetItem(pair.first); - QTableWidgetItem *colorItem = new QTableWidgetItem; - colorItem->setData(Qt::DisplayRole, pair.second); - - table->setItem(i, 0, nameItem); - table->setItem(i, 1, colorItem); - } - table->resizeColumnToContents(0); - table->horizontalHeader()->setStretchLastSection(true); - - QGridLayout *layout = new QGridLayout; - layout->addWidget(table, 0, 0); - - setLayout(layout); - - setWindowTitle(tr("Color Editor Factory")); -} diff --git a/examples/widgets/itemviews/coloreditorfactory/window.h b/examples/widgets/itemviews/coloreditorfactory/window.h deleted file mode 100644 index 4130b2d4ac2..00000000000 --- a/examples/widgets/itemviews/coloreditorfactory/window.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef WINDOW_H -#define WINDOW_H - -#include - -class Window : public QWidget -{ - Q_OBJECT - -public: - Window(); - -private: - void createGUI(); -}; - -#endif diff --git a/examples/widgets/itemviews/itemviews.pro b/examples/widgets/itemviews/itemviews.pro index 92997782a2c..db06874ba52 100644 --- a/examples/widgets/itemviews/itemviews.pro +++ b/examples/widgets/itemviews/itemviews.pro @@ -1,7 +1,6 @@ TEMPLATE = subdirs SUBDIRS = addressbook \ basicsortfiltermodel \ - coloreditorfactory \ combowidgetmapper \ customsortfiltermodel \ editabletreemodel \ diff --git a/src/widgets/doc/snippets/code/src_gui_itemviews_qitemeditorfactory.cpp b/src/widgets/doc/snippets/code/src_gui_itemviews_qitemeditorfactory.cpp index d314c88fd90..1a311015c49 100644 --- a/src/widgets/doc/snippets/code/src_gui_itemviews_qitemeditorfactory.cpp +++ b/src/widgets/doc/snippets/code/src_gui_itemviews_qitemeditorfactory.cpp @@ -14,12 +14,16 @@ QItemEditorFactory *factory = new QItemEditorFactory; //! [1] +//! [setDefaultFactory] //! [2] QItemEditorFactory *editorFactory = new QItemEditorFactory; QItemEditorCreatorBase *creator = new QStandardItemEditorCreator(); editorFactory->registerEditor(QMetaType::QDateTime, creator); //! [2] +QItemEditorFactory::setDefaultFactory(editorFactory); +//! [setDefaultFactory] + //! [3] Q_PROPERTY(QColor color READ color WRITE setColor USER true) diff --git a/src/widgets/itemviews/qitemdelegate.cpp b/src/widgets/itemviews/qitemdelegate.cpp index e4a90278b49..d1c7bb3d581 100644 --- a/src/widgets/itemviews/qitemdelegate.cpp +++ b/src/widgets/itemviews/qitemdelegate.cpp @@ -257,15 +257,25 @@ QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const When subclassing QItemDelegate to create a delegate that displays items using a custom renderer, it is important to ensure that the delegate can - render items suitably for all the required states; e.g. selected, + render items suitably for all the required states; such as selected, disabled, checked. The documentation for the paint() function contains some hints to show how this can be achieved. - You can provide custom editors by using a QItemEditorFactory. The - \l{Color Editor Factory Example} shows how a custom editor can be - made available to delegates with the default item editor - factory. This way, there is no need to subclass QItemDelegate. An - alternative is to reimplement createEditor(), setEditorData(), + You can provide custom editors by using a QItemEditorFactory. The following + code shows how a custom editor can be made available to delegates with the + default item editor factory. + + \snippet code/src_gui_itemviews_qitemeditorfactory.cpp setDefaultFactory + + After the default factory has been set, all standard item delegates + will use it (also the delegates that were created before setting the + default factory). + + This way, you can avoid subclassing QItemDelegate, and all values of the + specified type (for example QMetaType::QDateTime) will be edited using the + provided editor (like \c{MyFancyDateTimeEdit} in the above example). + + An alternative is to reimplement createEditor(), setEditorData(), setModelData(), and updateEditorGeometry(). This process is described in the \l{A simple delegate}{Model/View Programming overview documentation}. diff --git a/src/widgets/itemviews/qitemeditorfactory.cpp b/src/widgets/itemviews/qitemeditorfactory.cpp index 609df364cfa..70d11e1b38f 100644 --- a/src/widgets/itemviews/qitemeditorfactory.cpp +++ b/src/widgets/itemviews/qitemeditorfactory.cpp @@ -120,7 +120,7 @@ Q_SIGNALS: Additional editors can be registered with the registerEditor() function. - \sa QStyledItemDelegate, {Model/View Programming}, {Color Editor Factory Example} + \sa QStyledItemDelegate, {Model/View Programming} */ /*! @@ -363,7 +363,7 @@ void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory) to register widgets without the need to subclass QItemEditorCreatorBase. \sa QStandardItemEditorCreator, QItemEditorFactory, - {Model/View Programming}, {Color Editor Factory Example} + {Model/View Programming} */ /*! @@ -432,7 +432,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() property, you should use QStandardItemEditorCreator instead. \sa QItemEditorCreatorBase, QStandardItemEditorCreator, - QItemEditorFactory, {Color Editor Factory Example} + QItemEditorFactory */ /*! @@ -488,7 +488,7 @@ QItemEditorCreatorBase::~QItemEditorCreatorBase() \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 3 \sa QItemEditorCreatorBase, QItemEditorCreator, - QItemEditorFactory, QStyledItemDelegate, {Color Editor Factory Example} + QItemEditorFactory, QStyledItemDelegate */ /*! diff --git a/src/widgets/itemviews/qstyleditemdelegate.cpp b/src/widgets/itemviews/qstyleditemdelegate.cpp index 0587e8d0bea..54c1fb4f528 100644 --- a/src/widgets/itemviews/qstyleditemdelegate.cpp +++ b/src/widgets/itemviews/qstyleditemdelegate.cpp @@ -137,12 +137,17 @@ public: instance provided by QItemEditorFactory is installed on all item delegates. You can set a custom factory using setItemEditorFactory() or set a new default factory with - QItemEditorFactory::setDefaultFactory(). It is the data stored in - the item model with the \l{Qt::}{EditRole} that is edited. See the - QItemEditorFactory class for a more high-level introduction to - item editor factories. The \l{Color Editor Factory Example}{Color - Editor Factory} example shows how to create custom editors with a - factory. + QItemEditorFactory::setDefaultFactory(). + + \snippet code/src_gui_itemviews_qitemeditorfactory.cpp setDefaultFactory + + After the new factory has been set, all standard item delegates + will use it (i.e, also delegates that were created before the new + default factory was set). + + It is the data stored in the item model with the \l{Qt::}{EditRole} + that is edited. See the QItemEditorFactory class for a more + high-level introduction to item editor factories. \section1 Subclassing QStyledItemDelegate @@ -204,7 +209,7 @@ public: documentation for details. \sa {Delegate Classes}, QItemDelegate, QAbstractItemDelegate, QStyle, - {Star Delegate Example}, {Color Editor Factory Example} + {Star Delegate Example} */ diff --git a/tests/auto/guiapplauncher/examples.txt b/tests/auto/guiapplauncher/examples.txt index 4848da3f405..7aef8399250 100644 --- a/tests/auto/guiapplauncher/examples.txt +++ b/tests/auto/guiapplauncher/examples.txt @@ -23,7 +23,6 @@ "itemviews/addressbook Example", "examples/widgets/itemviews/addressbook", "addressbook", 0, -1 "itemviews/basicsortfiltermodel Example", "examples/widgets/itemviews/basicsortfiltermodel", "basicsortfiltermodel", 10, -1 "itemviews/chart Example", "examples/widgets/itemviews/chart", "chart", 0, -1 -"itemviews/coloreditorfactory Example", "examples/widgets/itemviews/coloreditorfactory", "coloreditorfactory", 10, -1 "itemviews/combowidgetmapper Example", "examples/widgets/itemviews/combowidgetmapper", "combowidgetmapper", 6, -1 "itemviews/customsortfiltermodel Example", "examples/widgets/itemviews/customsortfiltermodel", "customsortfiltermodel", 6, -1 "itemviews/dirview Example", "examples/widgets/itemviews/dirview", "dirview", 0, -1