Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstyleditemdelegate.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7#include <qapplication.h>
8#include <qbrush.h>
9#if QT_CONFIG(lineedit)
10#include <qlineedit.h>
11#endif
12#if QT_CONFIG(textedit)
13#include <qtextedit.h>
14#include <qplaintextedit.h>
15#endif
16#include <qpainter.h>
17#include <qpalette.h>
18#include <qpoint.h>
19#include <qrect.h>
20#include <qsize.h>
21#include <qstyle.h>
22#include <qdatetime.h>
23#include <qstyleoption.h>
24#include <qevent.h>
25#include <qpixmap.h>
26#include <qbitmap.h>
27#include <qpixmapcache.h>
28#include <qitemeditorfactory.h>
29#include <private/qitemeditorfactory_p.h>
30#include <qmetaobject.h>
31#include <qtextlayout.h>
32#include <private/qabstractitemdelegate_p.h>
33#include <private/qabstractitemmodel_p.h>
34#include <private/qtextengine_p.h>
35#include <private/qlayoutengine_p.h>
36#include <qdebug.h>
37#include <qlocale.h>
38#if QT_CONFIG(tableview)
39#include <qtableview.h>
40#endif
41
42#include <array>
43#include <limits.h>
44
46
76
223
230
245{
246 return d_func()->textForRole(Qt::DisplayRole, value, locale);
247}
248
257 const QModelIndex &index) const
258{
259 option->index = index;
260
261 Q_D(const QStyledItemDelegate);
262 QModelRoleDataSpan modelRoleDataSpan = d->modelRoleData;
263 index.multiData(modelRoleDataSpan);
264
265 const QVariant *value;
266 value = modelRoleDataSpan.dataForRole(Qt::FontRole);
267 if (value->isValid() && !value->isNull()) {
268 option->font = qvariant_cast<QFont>(*value).resolve(option->font);
269 option->fontMetrics = QFontMetrics(option->font);
270 }
271
272 value = modelRoleDataSpan.dataForRole(Qt::TextAlignmentRole);
273 if (value->isValid() && !value->isNull())
274 option->displayAlignment = QtPrivate::legacyFlagValueFromModelData<Qt::Alignment>(*value);
275
276 value = modelRoleDataSpan.dataForRole(Qt::ForegroundRole);
277 if (value->canConvert<QBrush>())
278 option->palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(*value));
279
280 value = modelRoleDataSpan.dataForRole(Qt::CheckStateRole);
281 if (value->isValid() && !value->isNull()) {
282 option->features |= QStyleOptionViewItem::HasCheckIndicator;
283 option->checkState = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(*value);
284 }
285
286 value = modelRoleDataSpan.dataForRole(Qt::DecorationRole);
287 if (value->isValid() && !value->isNull()) {
288 option->features |= QStyleOptionViewItem::HasDecoration;
289 switch (value->userType()) {
290 case QMetaType::QIcon: {
291 option->icon = qvariant_cast<QIcon>(*value);
292 if (option->icon.isNull()) {
293 option->features &= ~QStyleOptionViewItem::HasDecoration;
294 break;
295 }
297 if (!(option->state & QStyle::State_Enabled))
299 else if (option->state & QStyle::State_Selected)
301 else
304 QSize actualSize = option->icon.actualSize(option->decorationSize, mode, state);
305 // For highdpi icons actualSize might be larger than decorationSize, which we don't want. Clamp it to decorationSize.
306 option->decorationSize = QSize(qMin(option->decorationSize.width(), actualSize.width()),
307 qMin(option->decorationSize.height(), actualSize.height()));
308 break;
309 }
310 case QMetaType::QColor: {
311 QPixmap pixmap(option->decorationSize);
312 pixmap.fill(qvariant_cast<QColor>(*value));
313 option->icon = QIcon(pixmap);
314 break;
315 }
316 case QMetaType::QImage: {
317 QImage image = qvariant_cast<QImage>(*value);
319 option->decorationSize = image.deviceIndependentSize().toSize();
320 break;
321 }
322 case QMetaType::QPixmap: {
323 QPixmap pixmap = qvariant_cast<QPixmap>(*value);
324 option->icon = QIcon(pixmap);
325 option->decorationSize = pixmap.deviceIndependentSize().toSize();
326 break;
327 }
328 default:
329 break;
330 }
331 }
332
333 value = modelRoleDataSpan.dataForRole(Qt::DisplayRole);
334 if (value->isValid() && !value->isNull()) {
335 option->features |= QStyleOptionViewItem::HasDisplay;
336 option->text = displayText(*value, option->locale);
337 }
338
339 value = modelRoleDataSpan.dataForRole(Qt::BackgroundRole);
340 option->backgroundBrush = qvariant_cast<QBrush>(*value);
341
342 // disable style animations for checkboxes etc. within itemviews (QTBUG-30146)
343 option->styleObject = nullptr;
344}
345
369 const QStyleOptionViewItem &option, const QModelIndex &index) const
370{
371 Q_ASSERT(index.isValid());
372
373 QStyleOptionViewItem opt = option;
375
377 QStyle *style = widget ? widget->style() : QApplication::style();
379}
380
391QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option,
392 const QModelIndex &index) const
393{
395 if (value.isValid())
396 return qvariant_cast<QSize>(value);
397
398 QStyleOptionViewItem opt = option;
401 QStyle *style = widget ? widget->style() : QApplication::style();
403}
404
413 const QStyleOptionViewItem &option,
414 const QModelIndex &index) const
415{
417 Q_D(const QStyledItemDelegate);
418 if (!index.isValid())
419 return nullptr;
420 return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent);
421}
422
433{
435 QByteArray n = editor->metaObject()->userProperty().name();
436
437 if (!n.isEmpty()) {
438 if (!v.isValid())
439 v = QVariant(editor->property(n).metaType());
440 editor->setProperty(n, v);
441 }
442}
443
456 const QModelIndex &index) const
457{
458 Q_D(const QStyledItemDelegate);
460 Q_ASSERT(editor);
461 QByteArray n = editor->metaObject()->userProperty().name();
462 if (n.isEmpty())
463 n = d->editorFactory()->valuePropertyName(
465 if (!n.isEmpty())
466 model->setData(index, editor->property(n), Qt::EditRole);
467}
468
474 const QStyleOptionViewItem &option,
475 const QModelIndex &index) const
476{
477 if (!editor)
478 return;
479 Q_ASSERT(index.isValid());
481
482 QStyleOptionViewItem opt = option;
484 opt.showDecorationSelected = editor->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, editor);
485
486 QStyle *style = widget ? widget->style() : QApplication::style();
488 editor->setGeometry(geom);
489}
490
498{
499 Q_D(const QStyledItemDelegate);
500 return d->factory;
501}
502
515
516
549{
551 return d->editorEventFilter(object, event);
552}
553
559 const QStyleOptionViewItem &option,
560 const QModelIndex &index)
561{
564
565 // make sure that the item is checkable
566 Qt::ItemFlags flags = model->flags(index);
568 || !(flags & Qt::ItemIsEnabled))
569 return false;
570
571 // make sure that we have a check state
573 if (!value.isValid())
574 return false;
575
577 QStyle *style = widget ? widget->style() : QApplication::style();
578
579 // make sure that we have the right event type
580 if ((event->type() == QEvent::MouseButtonRelease)
581 || (event->type() == QEvent::MouseButtonDblClick)
582 || (event->type() == QEvent::MouseButtonPress)) {
583 QStyleOptionViewItem viewOpt(option);
584 initStyleOption(&viewOpt, index);
586 QMouseEvent *me = static_cast<QMouseEvent*>(event);
587 if (me->button() != Qt::LeftButton || !checkRect.contains(me->position().toPoint()))
588 return false;
589
590 if ((event->type() == QEvent::MouseButtonPress)
591 || (event->type() == QEvent::MouseButtonDblClick))
592 return true;
593
594 } else if (event->type() == QEvent::KeyPress) {
595 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
596 && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
597 return false;
598 } else {
599 return false;
600 }
601
602 Qt::CheckState state = QtPrivate::legacyEnumValueFromModelData<Qt::CheckState>(value);
604 state = ((Qt::CheckState)((state + 1) % 3));
605 else
608}
609
611
612#include "moc_qstyleditemdelegate.cpp"
The QAbstractItemDelegate class is used to display and edit data items from a model.
virtual Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
static QStyle * style()
Returns the application's style object.
\inmodule QtGui
Definition qbrush.h:30
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qcoreevent.h:45
@ KeyPress
Definition qcoreevent.h:64
@ MouseButtonPress
Definition qcoreevent.h:60
@ MouseButtonDblClick
Definition qcoreevent.h:62
@ MouseButtonRelease
Definition qcoreevent.h:61
\reentrant \inmodule QtGui
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition qicon.h:22
@ Disabled
Definition qicon.h:22
@ Selected
Definition qicon.h:22
@ Normal
Definition qicon.h:22
State
This enum describes the state for which a pixmap is intended to be used.
Definition qicon.h:23
@ Off
Definition qicon.h:23
@ On
Definition qicon.h:23
\inmodule QtGui
Definition qimage.h:37
The QItemEditorFactory class provides widgets for editing item data in views and delegates.
static const QItemEditorFactory * defaultFactory()
Returns the default item editor factory.
The QKeyEvent class describes a key event.
Definition qevent.h:424
int key() const
Returns the code of the key that was pressed or released.
Definition qevent.h:434
\inmodule QtCore
\inmodule QtCore
\inmodule QtGui
Definition qevent.h:196
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition qpixmap.cpp:1437
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qrect.h:30
QPointF position() const
Returns the position of the point in this event, relative to the widget or item that received the eve...
Definition qevent.h:119
Qt::MouseButton button() const
Returns the button that caused the event.
Definition qevent.h:116
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const override
Returns the value for the specified item and role.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.
Definition qstyle.h:29
@ State_Open
Definition qstyle.h:85
@ State_Enabled
Definition qstyle.h:67
@ State_Selected
Definition qstyle.h:82
@ CT_ItemViewItem
Definition qstyle.h:569
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
@ SH_ItemView_ShowDecorationSelected
Definition qstyle.h:645
@ CE_ItemViewItem
Definition qstyle.h:230
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=nullptr) const =0
Returns the sub-area for the given element as described in the provided style option.
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=nullptr) const =0
Draws the given element with the provided painter with the style options specified by option.
@ SE_ItemViewItemCheckIndicator
Definition qstyle.h:275
@ SE_ItemViewItemText
Definition qstyle.h:305
static const QWidget * widget(const QStyleOptionViewItem &option)
std::array< QModelRoleData, 7 > modelRoleData
const QItemEditorFactory * editorFactory() const
The QStyledItemDelegate class provides display and editing facilities for data items from a model.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Sets the data to be displayed and edited by the editor from the data model item specified by the mode...
virtual QString displayText(const QVariant &value, const QLocale &locale) const
This function returns the string that the delegate will use to display the Qt::DisplayRole of the mod...
QStyledItemDelegate(QObject *parent=nullptr)
Constructs an item delegate with the given parent.
~QStyledItemDelegate()
Destroys the item delegate.
void setItemEditorFactory(QItemEditorFactory *factory)
Sets the editor factory to be used by the item delegate to be the factory specified.
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
\reimp
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Gets data from the editor widget and stores it in the specified model at the item index.
virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
Initialize option with the values using the index index.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Updates the editor for the item specified by index according to the style option given.
QItemEditorFactory * itemEditorFactory() const
Returns the editor factory used by the item delegate.
bool eventFilter(QObject *object, QEvent *event) override
Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns...
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Returns the widget used to edit the item specified by index for editing.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Returns the size needed by the delegate to display the item specified by index, taking into account t...
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Renders the delegate using the given painter and style option for the item specified by index.
\inmodule QtCore
Definition qvariant.h:65
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
int userType() const
Definition qvariant.h:339
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QStyle * style() const
Definition qwidget.cpp:2600
QOpenGLWidget * widget
[1]
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
CheckState
@ Unchecked
@ Checked
@ LeftButton
Definition qnamespace.h:58
@ FontRole
@ TextAlignmentRole
@ ForegroundRole
@ DecorationRole
@ BackgroundRole
@ EditRole
@ CheckStateRole
@ DisplayRole
@ SizeHintRole
@ Key_Select
@ Key_Space
Definition qnamespace.h:513
@ ItemIsUserTristate
@ ItemIsUserCheckable
@ ItemIsEnabled
Definition image.cpp:4
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLsizei const GLfloat * v
[13]
GLenum mode
GLuint64 key
GLuint index
[2]
GLbitfield flags
GLfloat n
struct _cl_event * event
GLuint GLenum option
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
QSqlQueryModel * model
[16]
QObject::connect nullptr
QItemEditorFactory * factory
widget render & pixmap
QPainter painter(this)
[7]