Convert "Color Editor Factory" Example to snippets

The color editor factory example is removed and part of the codes are
used as snippets.

Fixes: QTBUG-119985
Pick-to: 6.6
Change-Id: I421e473e7db09a5af7543b80b87a338d8ff2ab7e
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 4e1a1f3697563124588d7d00089b15084220ca42)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
MohammadHossein Qanbari 2024-01-08 16:41:56 +01:00 committed by Qt Cherry-pick Bot
parent 23b69855c0
commit 869e46613d
16 changed files with 36 additions and 365 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

View File

@ -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.
*/

View File

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

View File

@ -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})

View File

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

View File

@ -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 <QtWidgets>
ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget)
{
populateList();
}
//! [0]
QColor ColorListEditor::color() const
{
return qvariant_cast<QColor>(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]

View File

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

View File

@ -1,16 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "window.h"
int main(int argv, char **args)
{
QApplication app(argv, args);
Window window;
window.show();
return app.exec();
}

View File

@ -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 <QtWidgets>
//! [0]
Window::Window()
{
QItemEditorFactory *factory = new QItemEditorFactory;
QItemEditorCreatorBase *colorListCreator =
new QStandardItemEditorCreator<ColorListEditor>();
factory->registerEditor(QMetaType::QColor, colorListCreator);
QItemEditorFactory::setDefaultFactory(factory);
createGUI();
}
//! [0]
void Window::createGUI()
{
const QList<QPair<QString, QColor>> 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<QString, QColor> &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"));
}

View File

@ -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 <QWidget>
class Window : public QWidget
{
Q_OBJECT
public:
Window();
private:
void createGUI();
};
#endif

View File

@ -1,7 +1,6 @@
TEMPLATE = subdirs
SUBDIRS = addressbook \
basicsortfiltermodel \
coloreditorfactory \
combowidgetmapper \
customsortfiltermodel \
editabletreemodel \

View File

@ -14,12 +14,16 @@ QItemEditorFactory *factory = new QItemEditorFactory;
//! [1]
//! [setDefaultFactory]
//! [2]
QItemEditorFactory *editorFactory = new QItemEditorFactory;
QItemEditorCreatorBase *creator = new QStandardItemEditorCreator<MyFancyDateTimeEdit>();
editorFactory->registerEditor(QMetaType::QDateTime, creator);
//! [2]
QItemEditorFactory::setDefaultFactory(editorFactory);
//! [setDefaultFactory]
//! [3]
Q_PROPERTY(QColor color READ color WRITE setColor USER true)

View File

@ -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}.

View File

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

View File

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

View File

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