Qt 4.8
Public Functions | Protected Functions | Properties | List of all members
QItemDelegate Class Reference

The QItemDelegate class provides display and editing facilities for data items from a model. More...

#include <qitemdelegate.h>

Inheritance diagram for QItemDelegate:
QAbstractItemDelegate QObject QCalendarDelegate QColumnViewDelegate QComboBoxDelegate QCompleterItemDelegate

Public Functions

QWidgetcreateEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
 Returns the widget used to edit the item specified by index for editing. More...
 
bool hasClipping () const
 
QItemEditorFactoryitemEditorFactory () const
 Returns the editor factory used by the item delegate. More...
 
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 Renders the delegate using the given painter and style option for the item specified by index. More...
 
 QItemDelegate (QObject *parent=0)
 Constructs an item delegate with the given parent. More...
 
void setClipping (bool clip)
 
void setEditorData (QWidget *editor, const QModelIndex &index) const
 Sets the data to be displayed and edited by the editor from the data model item specified by the model index. More...
 
void setItemEditorFactory (QItemEditorFactory *factory)
 Sets the editor factory to be used by the item delegate to be the factory specified. More...
 
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
 Gets data from the editor widget and stores it in the specified model at the item index. More...
 
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
 Returns the size needed by the delegate to display the item specified by index, taking into account the style information provided by option. More...
 
void updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
 Updates the editor for the item specified by index according to the style option given. More...
 
 ~QItemDelegate ()
 Destroys the item delegate. More...
 
- Public Functions inherited from QAbstractItemDelegate
 QAbstractItemDelegate (QObject *parent=0)
 Creates a new abstract item delegate with the given parent. More...
 
virtual ~QAbstractItemDelegate ()
 Destroys the abstract item delegate. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Functions

QRect check (const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
 Note that on Mac, if /usr/include/AssertMacros. More...
 
QPixmap decoration (const QStyleOptionViewItem &option, const QVariant &variant) const
 Returns the pixmap used to decorate the root of the item view. More...
 
void doLayout (const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
 
void drawBackground (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 Renders the item background for the given index, using the given painter and style option. More...
 
virtual void drawCheck (QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
 Renders a check indicator within the rectangle specified by rect, using the given painter and style option, using the given state. More...
 
virtual void drawDecoration (QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
 Renders the decoration pixmap within the rectangle specified by rect using the given painter and style option. More...
 
virtual void drawDisplay (QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
 Renders the item view text within the rectangle specified by rect using the given painter and style option. More...
 
virtual void drawFocus (QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
 Renders the region within the rectangle specified by rect, indicating that it has the focus, using the given painter and style option. More...
 
bool editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
 Reimplemented Function More...
 
bool eventFilter (QObject *object, QEvent *event)
 Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. More...
 
QRect rect (const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
 
QPixmapselected (const QPixmap &pixmap, const QPalette &palette, bool enabled) const
 Returns the selected version of the given pixmap using the given palette. More...
 
QStyleOptionViewItem setOptions (const QModelIndex &index, const QStyleOptionViewItem &option) const
 
QRect textRectangle (QPainter *painter, const QRect &rect, const QFont &font, const QString &text) const
 
- Protected Functions inherited from QAbstractItemDelegate
 QAbstractItemDelegate (QObjectPrivate &, QObject *parent=0)
 Creates a new abstract item delegate with the given parent. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Properties

bool clipping
 if the delegate should clip the paint events More...
 

Additional Inherited Members

- Public Types inherited from QAbstractItemDelegate
enum  EndEditHint {
  NoHint, EditNextItem, EditPreviousItem, SubmitModelCache,
  RevertModelCache
}
 This enum describes the different hints that the delegate can give to the model and view components to make editing data in a model a comfortable experience for the user. More...
 
- Public Slots inherited from QAbstractItemDelegate
bool helpEvent (QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
 Whenever a help event occurs, this function is called with the event view option and the index that corresponds to the item where the event occurs. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QAbstractItemDelegate
void closeEditor (QWidget *editor, QAbstractItemDelegate::EndEditHint hint=NoHint)
 This signal is emitted when the user has finished editing an item using the specified editor. More...
 
void commitData (QWidget *editor)
 This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model. More...
 
void sizeHintChanged (const QModelIndex &)
 This signal must be emitted when the sizeHint() of index changed. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QAbstractItemDelegate
static QString elidedText (const QFontMetrics &fontMetrics, int width, Qt::TextElideMode mode, const QString &text)
 Use QFontMetrics::elidedText() instead. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QItemDelegate class provides display and editing facilities for data items from a model.

QItemDelegate can be used to provide custom display features and editor widgets for item views based on QAbstractItemView subclasses. Using a delegate for this purpose allows the display and editing mechanisms to be customized and developed independently from the model and view.

The QItemDelegate class is one of the Model/View Classes and is part of Qt's Model/View Programming{model/view framework}. Note that QStyledItemDelegate has taken over the job of drawing Qt's item views. We recommend the use of QStyledItemDelegate when creating new delegates.

When displaying items from a custom model in a standard view, it is often sufficient to simply ensure that the model returns appropriate data for each of the roles that determine the appearance of items in views. The default delegate used by Qt's standard views uses this role information to display items in most of the common forms expected by users. However, it is sometimes necessary to have even more control over the appearance of items than the default delegate can provide.

This class provides default implementations of the functions for painting item data in a view and editing data from item models. Default implementations of the paint() and sizeHint() virtual functions, defined in QAbstractItemDelegate, are provided to ensure that the delegate implements the correct basic behavior expected by views. You can reimplement these functions in subclasses to customize the appearance of items.

When editing data in an item view, QItemDelegate provides an editor widget, which is a widget that is placed on top of the view while editing takes place. Editors are created with a QItemEditorFactory; a default static 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 Qt::EditRole that is edited.

Only the standard editing functions for widget-based delegates are reimplemented here:

The closeEditor() signal indicates that the user has completed editing the data, and that the editor widget can be destroyed.

Standard Roles and Data Types

The default delegate used by the standard views supplied with Qt associates each standard role (defined by Qt::ItemDataRole) with certain data types. Models that return data in these types can influence the appearance of the delegate as described in the following table.

Role Accepted Types
Warning
This function is not part of the public interface.
Qt::AccessibleDescriptionRole QString
Qt::AccessibleTextRole QString

Qt::BackgroundRole

QBrush

Qt::BackgroundColorRole

QColor (obsolete; use Qt::BackgroundRole instead)

Qt::CheckStateRole

Qt::CheckState

Qt::DecorationRole

QIcon, QPixmap and QColor

Qt::DisplayRole

QString and types with a string representation

Qt::EditRole

See QItemEditorFactory for details

Qt::FontRole

QFont

Qt::SizeHintRole

QSize

Warning
This function is not part of the public interface.

Qt::StatusTipRole

Qt::TextAlignmentRole

Qt::Alignment

Qt::ForegroundRole

QBrush

Qt::TextColorRole

QColor (obsolete; use Qt::ForegroundRole instead)

Warning
This function is not part of the public interface.

Qt::ToolTipRole

Qt::WhatsThisRole

If the default delegate does not allow the level of customization that you need, either for display purposes or for editing data, it is possible to subclass QItemDelegate to implement the desired behavior.

Subclassing

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, 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 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(), setModelData(), and updateEditorGeometry(). This process is described in the Spin Box Delegate Example.

.-QItemDelegate

QStyledItemDelegate vs. QItemDelegate

Since Qt 4.4, there are two delegate classes: QItemDelegate and QStyledItemDelegate. However, the default delegate is QStyledItemDelegate. These two classes are independent alternatives to painting and providing editors for items in views. The difference between them is that QStyledItemDelegate uses the current style to paint its items. We therefore recommend using QStyledItemDelegate as the base class when implementing custom delegates or when working with Qt style sheets. The code required for either class should be equal unless the custom delegate needs to use the style for drawing.

See also
{Delegate Classes}, QStyledItemDelegate, QAbstractItemDelegate, {Spin Box Delegate Example}, {Settings Editor Example}, {Icons Example}

Definition at line 61 of file qitemdelegate.h.

Constructors and Destructors

◆ QItemDelegate()

QItemDelegate::QItemDelegate ( QObject parent = 0)
explicit

Constructs an item delegate with the given parent.

Definition at line 319 of file qitemdelegate.cpp.

321 {
322 
323 }
QAbstractItemDelegate(QObject *parent=0)
Creates a new abstract item delegate with the given parent.

◆ ~QItemDelegate()

QItemDelegate::~QItemDelegate ( )

Destroys the item delegate.

Definition at line 329 of file qitemdelegate.cpp.

330 {
331 }

Functions

◆ check()

QRect QItemDelegate::check ( const QStyleOptionViewItem option,
const QRect bounding,
const QVariant value 
) const
protected

Note that on Mac, if /usr/include/AssertMacros.

Warning
This function is not part of the public interface.

h is included prior to QItemDelegate, and the application is building in debug mode, the check(assertion) will conflict with QItemDelegate::check.

To avoid this problem, add

#ifdef check #undef check #endif

after including AssertMacros.h

Definition at line 1141 of file qitemdelegate.cpp.

Referenced by doLayout(), editorEvent(), paint(), rect(), and updateEditorGeometry().

1143 {
1144  if (value.isValid()) {
1145  Q_D(const QItemDelegate);
1146  QStyleOptionButton opt;
1147  opt.QStyleOption::operator=(option);
1148  opt.rect = bounding;
1149  const QWidget *widget = d->widget(option); // cast
1150  QStyle *style = widget ? widget->style() : QApplication::style();
1151  return style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, widget);
1152  }
1153  return QRect();
1154 }
double d
Definition: qnumeric_p.h:62
QPointer< QWidget > widget
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QStyle * style()
Returns the application&#39;s style object.
#define Q_D(Class)
Definition: qglobal.h:2482
QStyle * style() const
Definition: qwidget.cpp:2742
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=0) const =0
Returns the sub-area for the given element as described in the provided style option.
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
The QStyleOptionButton class is used to describe the parameters for drawing buttons.
Definition: qstyleoption.h:279
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90

◆ createEditor()

QWidget * QItemDelegate::createEditor ( QWidget parent,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Returns the widget used to edit the item specified by index for editing.

The parent widget and style option are used to control how the editor widget appears.

See also
QAbstractItemDelegate::createEditor()

Reimplemented from QAbstractItemDelegate.

Definition at line 528 of file qitemdelegate.cpp.

531 {
532  Q_D(const QItemDelegate);
533  if (!index.isValid())
534  return 0;
535  QVariant::Type t = static_cast<QVariant::Type>(index.data(Qt::EditRole).userType());
536  const QItemEditorFactory *factory = d->f;
537  if (factory == 0)
539  return factory->createEditor(t, parent);
540 }
double d
Definition: qnumeric_p.h:62
The QItemEditorFactory class provides widgets for editing item data in views and delegates.
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
#define Q_D(Class)
Definition: qglobal.h:2482
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
static const QItemEditorFactory * defaultFactory()
Returns the default item editor factory.
virtual QWidget * createEditor(QVariant::Type type, QWidget *parent) const
Creates an editor widget with the given parent for the specified type of data, and returns it as a QW...

◆ decoration()

QPixmap QItemDelegate::decoration ( const QStyleOptionViewItem option,
const QVariant variant 
) const
protected

Returns the pixmap used to decorate the root of the item view.

Warning
This function is not part of the public interface.

The style option controls the appearance of the root; the variant refers to the data associated with an item.

Definition at line 1014 of file qitemdelegate.cpp.

Referenced by doLayout(), paint(), and updateEditorGeometry().

1015 {
1016  Q_D(const QItemDelegate);
1017  switch (variant.type()) {
1018  case QVariant::Icon: {
1019  QIcon::Mode mode = d->iconMode(option.state);
1020  QIcon::State state = d->iconState(option.state);
1021  return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); }
1022  case QVariant::Color: {
1023  static QPixmap pixmap(option.decorationSize);
1024  pixmap.fill(qvariant_cast<QColor>(variant));
1025  return pixmap; }
1026  default:
1027  break;
1028  }
1029 
1030  return qvariant_cast<QPixmap>(variant);
1031 }
double d
Definition: qnumeric_p.h:62
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
State
This enum describes the state for which a pixmap is intended to be used.
Definition: qicon.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition: qicon.h:63
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
QSize decorationSize
the size of the decoration for the item
Definition: qstyleoption.h:551
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
const char * variant
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

◆ doLayout()

void QItemDelegate::doLayout ( const QStyleOptionViewItem option,
QRect checkRect,
QRect pixmapRect,
QRect textRect,
bool  hint 
) const
protected
Warning
This function is not part of the public interface.

Code duplicated in QCommonStylePrivate::viewItemLayout

Definition at line 876 of file qitemdelegate.cpp.

Referenced by editorEvent(), paint(), sizeHint(), and updateEditorGeometry().

879 {
880  Q_ASSERT(checkRect && pixmapRect && textRect);
881  Q_D(const QItemDelegate);
882  const QWidget *widget = d->widget(option);
883  QStyle *style = widget ? widget->style() : QApplication::style();
884  const bool hasCheck = checkRect->isValid();
885  const bool hasPixmap = pixmapRect->isValid();
886  const bool hasText = textRect->isValid();
887  const int textMargin = hasText ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
888  const int pixmapMargin = hasPixmap ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
889  const int checkMargin = hasCheck ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
890  int x = option.rect.left();
891  int y = option.rect.top();
892  int w, h;
893 
894  textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding
895  if (textRect->height() == 0 && (!hasPixmap || !hint)) {
896  //if there is no text, we still want to have a decent height for the item sizeHint and the editor size
897  textRect->setHeight(option.fontMetrics.height());
898  }
899 
900  QSize pm(0, 0);
901  if (hasPixmap) {
902  pm = pixmapRect->size();
903  pm.rwidth() += 2 * pixmapMargin;
904  }
905  if (hint) {
906  h = qMax(checkRect->height(), qMax(textRect->height(), pm.height()));
909  w = textRect->width() + pm.width();
910  } else {
911  w = qMax(textRect->width(), pm.width());
912  }
913  } else {
914  w = option.rect.width();
915  h = option.rect.height();
916  }
917 
918  int cw = 0;
919  QRect check;
920  if (hasCheck) {
921  cw = checkRect->width() + 2 * checkMargin;
922  if (hint) w += cw;
923  if (option.direction == Qt::RightToLeft) {
924  check.setRect(x + w - cw, y, cw, h);
925  } else {
926  check.setRect(x + checkMargin, y, cw, h);
927  }
928  }
929 
930  // at this point w should be the *total* width
931 
932  QRect display;
934  switch (option.decorationPosition) {
936  if (hasPixmap)
937  pm.setHeight(pm.height() + pixmapMargin); // add space
938  h = hint ? textRect->height() : h - pm.height();
939 
940  if (option.direction == Qt::RightToLeft) {
941  decoration.setRect(x, y, w - cw, pm.height());
942  display.setRect(x, y + pm.height(), w - cw, h);
943  } else {
944  decoration.setRect(x + cw, y, w - cw, pm.height());
945  display.setRect(x + cw, y + pm.height(), w - cw, h);
946  }
947  break; }
949  if (hasText)
950  textRect->setHeight(textRect->height() + textMargin); // add space
951  h = hint ? textRect->height() + pm.height() : h;
952 
953  if (option.direction == Qt::RightToLeft) {
954  display.setRect(x, y, w - cw, textRect->height());
955  decoration.setRect(x, y + textRect->height(), w - cw, h - textRect->height());
956  } else {
957  display.setRect(x + cw, y, w - cw, textRect->height());
958  decoration.setRect(x + cw, y + textRect->height(), w - cw, h - textRect->height());
959  }
960  break; }
962  if (option.direction == Qt::LeftToRight) {
963  decoration.setRect(x + cw, y, pm.width(), h);
964  display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h);
965  } else {
966  display.setRect(x, y, w - pm.width() - cw, h);
967  decoration.setRect(display.right() + 1, y, pm.width(), h);
968  }
969  break; }
971  if (option.direction == Qt::LeftToRight) {
972  display.setRect(x + cw, y, w - pm.width() - cw, h);
973  decoration.setRect(display.right() + 1, y, pm.width(), h);
974  } else {
975  decoration.setRect(x, y, pm.width(), h);
976  display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h);
977  }
978  break; }
979  default:
980  qWarning("doLayout: decoration position is invalid");
981  decoration = *pixmapRect;
982  break;
983  }
984 
985  if (!hint) { // we only need to do the internal layout if we are going to paint
986  *checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
987  checkRect->size(), check);
988  *pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment,
989  pixmapRect->size(), decoration);
990  // the text takes up all available space, unless the decoration is not shown as selected
991  if (option.showDecorationSelected)
992  *textRect = display;
993  else
994  *textRect = QStyle::alignedRect(option.direction, option.displayAlignment,
995  textRect->size().boundedTo(display.size()), display);
996  } else {
997  *checkRect = check;
998  *pixmapRect = decoration;
999  *textRect = display;
1000  }
1001 }
double d
Definition: qnumeric_p.h:62
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition: qstyle.cpp:2120
void setHeight(int h)
Sets the height of the rectangle to the given height.
Definition: qrect.h:445
Qt::Alignment decorationAlignment
the alignment of the decoration for the item
Definition: qstyleoption.h:548
QPointer< QWidget > widget
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
Position decorationPosition
the position of the decoration for the item
Definition: qstyleoption.h:550
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
static QStyle * style()
Returns the application&#39;s style object.
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QStyle * style() const
Definition: qwidget.cpp:2742
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
QPixmap decoration(const QStyleOptionViewItem &option, const QVariant &variant) const
Returns the pixmap used to decorate the root of the item view.
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:434
Q_CORE_EXPORT void qWarning(const char *,...)
bool showDecorationSelected
whether the decoration should be highlighted on selected items
Definition: qstyleoption.h:553
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
void setRect(int x, int y, int w, int h)
Sets the coordinates of the rectangle&#39;s top-left corner to ({x}, {y}), and its size to the given widt...
Definition: qrect.h:400
Qt::Alignment displayAlignment
the alignment of the display value for the item
Definition: qstyleoption.h:547
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
Qt::LayoutDirection direction
the text layout direction that should be used when drawing text in the control
Definition: qstyleoption.h:89
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int height() const
Returns the height of the font.
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141

◆ drawBackground()

void QItemDelegate::drawBackground ( QPainter painter,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
protected

Renders the item background for the given index, using the given painter and style option.

Since
4.2

Definition at line 847 of file qitemdelegate.cpp.

Referenced by paint().

850 {
851  if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
854  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
855  cg = QPalette::Inactive;
856 
857  painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
858  } else {
859  QVariant value = index.data(Qt::BackgroundRole);
860  if (value.canConvert<QBrush>()) {
861  QPointF oldBO = painter->brushOrigin();
862  painter->setBrushOrigin(option.rect.topLeft());
863  painter->fillRect(option.rect, qvariant_cast<QBrush>(value));
864  painter->setBrushOrigin(oldBO);
865  }
866  }
867 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void setBrushOrigin(int x, int y)
Sets the brush&#39;s origin to point (x, y).
Definition: qpainter.h:825
ColorGroup
Definition: qpalette.h:92
QPoint brushOrigin() const
Returns the currently set brush origin.
Definition: qpainter.cpp:2168
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
bool showDecorationSelected
whether the decoration should be highlighted on selected items
Definition: qstyleoption.h:553
const QBrush & brush(ColorGroup cg, ColorRole cr) const
Returns the brush in the specified color group, used for the given color role.
Definition: qpalette.cpp:874
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288

◆ drawCheck()

void QItemDelegate::drawCheck ( QPainter painter,
const QStyleOptionViewItem option,
const QRect rect,
Qt::CheckState  state 
) const
protectedvirtual

Renders a check indicator within the rectangle specified by rect, using the given painter and style option, using the given state.

Definition at line 808 of file qitemdelegate.cpp.

Referenced by paint().

811 {
812  Q_D(const QItemDelegate);
813  if (!rect.isValid())
814  return;
815 
816  QStyleOptionViewItem opt(option);
817  opt.rect = rect;
818  opt.state = opt.state & ~QStyle::State_HasFocus;
819 
820  switch (state) {
821  case Qt::Unchecked:
822  opt.state |= QStyle::State_Off;
823  break;
825  opt.state |= QStyle::State_NoChange;
826  break;
827  case Qt::Checked:
828  opt.state |= QStyle::State_On;
829  break;
830  }
831 
832  const QWidget *widget = d->widget(option);
833  QStyle *style = widget ? widget->style() : QApplication::style();
834  style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
835 }
double d
Definition: qnumeric_p.h:62
QPointer< QWidget > widget
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QStyle * style()
Returns the application&#39;s style object.
#define Q_D(Class)
Definition: qglobal.h:2482
QStyle * style() const
Definition: qwidget.cpp:2742
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
QRect rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237

◆ drawDecoration()

void QItemDelegate::drawDecoration ( QPainter painter,
const QStyleOptionViewItem option,
const QRect rect,
const QPixmap pixmap 
) const
protectedvirtual

Renders the decoration pixmap within the rectangle specified by rect using the given painter and style option.

Definition at line 753 of file qitemdelegate.cpp.

Referenced by paint().

755 {
756  Q_D(const QItemDelegate);
757  // if we have an icon, we ignore the pixmap
758  if (!d->tmp.icon.isNull()) {
759  d->tmp.icon.paint(painter, rect, option.decorationAlignment,
760  d->tmp.mode, d->tmp.state);
761  return;
762  }
763 
764  if (pixmap.isNull() || !rect.isValid())
765  return;
767  pixmap.size(), rect).topLeft();
768  if (option.state & QStyle::State_Selected) {
769  QPixmap *pm = selected(pixmap, option.palette, option.state & QStyle::State_Enabled);
770  painter->drawPixmap(p, *pm);
771  } else {
772  painter->drawPixmap(p, pixmap);
773  }
774 }
double d
Definition: qnumeric_p.h:62
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition: qstyle.cpp:2120
Qt::Alignment decorationAlignment
the alignment of the decoration for the item
Definition: qstyleoption.h:548
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
#define Q_D(Class)
Definition: qglobal.h:2482
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
QRect rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPixmap * selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const
Returns the selected version of the given pixmap using the given palette.
Qt::LayoutDirection direction
the text layout direction that should be used when drawing text in the control
Definition: qstyleoption.h:89
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237

◆ drawDisplay()

void QItemDelegate::drawDisplay ( QPainter painter,
const QStyleOptionViewItem option,
const QRect rect,
const QString text 
) const
protectedvirtual

Renders the item view text within the rectangle specified by rect using the given painter and style option.

Definition at line 669 of file qitemdelegate.cpp.

Referenced by paint().

671 {
672  Q_D(const QItemDelegate);
673 
676  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
677  cg = QPalette::Inactive;
678  if (option.state & QStyle::State_Selected) {
679  painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight));
680  painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
681  } else {
682  painter->setPen(option.palette.color(cg, QPalette::Text));
683  }
684 
685  if (text.isEmpty())
686  return;
687 
688  if (option.state & QStyle::State_Editing) {
689  painter->save();
690  painter->setPen(option.palette.color(cg, QPalette::Text));
691  painter->drawRect(rect.adjusted(0, 0, -1, -1));
692  painter->restore();
693  }
694 
695  const QStyleOptionViewItemV4 opt = option;
696 
697  const QWidget *widget = d->widget(option);
698  QStyle *style = widget ? widget->style() : QApplication::style();
699  const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1;
700  QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
701  const bool wrapText = opt.features & QStyleOptionViewItemV2::WrapText;
702  d->textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
703  d->textOption.setTextDirection(option.direction);
704  d->textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment));
705  d->textLayout.setTextOption(d->textOption);
706  d->textLayout.setFont(option.font);
707  d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text));
708 
709  QSizeF textLayoutSize = d->doTextLayout(textRect.width());
710 
711  if (textRect.width() < textLayoutSize.width()
712  || textRect.height() < textLayoutSize.height()) {
713  QString elided;
714  int start = 0;
715  int end = text.indexOf(QChar::LineSeparator, start);
716  if (end == -1) {
717  elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
718  } else {
719  while (end != -1) {
720  elided += option.fontMetrics.elidedText(text.mid(start, end - start),
721  option.textElideMode, textRect.width());
722  elided += QChar::LineSeparator;
723  start = end + 1;
724  end = text.indexOf(QChar::LineSeparator, start);
725  }
726  //let's add the last line (after the last QChar::LineSeparator)
727  elided += option.fontMetrics.elidedText(text.mid(start),
728  option.textElideMode, textRect.width());
729  }
730  d->textLayout.setText(elided);
731  textLayoutSize = d->doTextLayout(textRect.width());
732  }
733 
734  const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));
735  const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment,
736  layoutSize, textRect);
737  // if we still overflow even after eliding the text, enable clipping
738  if (!hasClipping() && (textRect.width() < textLayoutSize.width()
739  || textRect.height() < textLayoutSize.height())) {
740  painter->save();
741  painter->setClipRect(layoutRect);
742  d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
743  painter->restore();
744  } else {
745  d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
746  }
747 }
double d
Definition: qnumeric_p.h:62
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition: qstyle.cpp:2120
QRect adjusted(int x1, int y1, int x2, int y2) const
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition: qrect.h:431
QFont font
the font used for the item
Definition: qstyleoption.h:552
QPointer< QWidget > widget
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
qreal width() const
Returns the width.
Definition: qsize.h:284
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
qreal height() const
Returns the height.
Definition: qsize.h:287
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QStyleOptionViewItemV4 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:609
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
Qt::TextElideMode textElideMode
where ellipsis should be added for text that is too long to fit into an item
Definition: qstyleoption.h:549
static QStyle * style()
Returns the application&#39;s style object.
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QString class provides a Unicode character string.
Definition: qstring.h:83
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags=0) const
If the string text is wider than width, returns an elided version of the string (i.
QStyle * style() const
Definition: qwidget.cpp:2742
ColorGroup
Definition: qpalette.h:92
ViewItemFeatures features
a bitwise OR of the features that describe this view item
Definition: qstyleoption.h:577
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
const QBrush & brush(ColorGroup cg, ColorRole cr) const
Returns the brush in the specified color group, used for the given color role.
Definition: qpalette.cpp:874
bool hasClipping() const
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition: qstyle.cpp:2149
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
Qt::Alignment displayAlignment
the alignment of the display value for the item
Definition: qstyleoption.h:547
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
Qt::LayoutDirection direction
the text layout direction that should be used when drawing text in the control
Definition: qstyleoption.h:89
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition: qpainter.h:650
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static QString replaceNewLine(QString text)
static const KeyPair *const end
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288

◆ drawFocus()

void QItemDelegate::drawFocus ( QPainter painter,
const QStyleOptionViewItem option,
const QRect rect 
) const
protectedvirtual

Renders the region within the rectangle specified by rect, indicating that it has the focus, using the given painter and style option.

Definition at line 781 of file qitemdelegate.cpp.

Referenced by paint().

784 {
785  Q_D(const QItemDelegate);
786  if ((option.state & QStyle::State_HasFocus) == 0 || !rect.isValid())
787  return;
789  o.QStyleOption::operator=(option);
790  o.rect = rect;
795  o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
797  const QWidget *widget = d->widget(option);
798  QStyle *style = widget ? widget->style() : QApplication::style();
799  style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, widget);
800 }
double d
Definition: qnumeric_p.h:62
QPointer< QWidget > widget
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QStyle * style()
Returns the application&#39;s style object.
#define Q_D(Class)
Definition: qglobal.h:2482
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
QStyle * style() const
Definition: qwidget.cpp:2742
ColorGroup
Definition: qpalette.h:92
QColor backgroundColor
the background color on which the focus rectangle is being drawn
Definition: qstyleoption.h:109
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
The QStyleOptionFocusRect class is used to describe the parameters for drawing a focus rectangle with...
Definition: qstyleoption.h:103
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
QRect rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90

◆ editorEvent()

bool QItemDelegate::editorEvent ( QEvent event,
QAbstractItemModel model,
const QStyleOptionViewItem option,
const QModelIndex index 
)
protectedvirtual

Reimplemented Function

Reimplemented from QAbstractItemDelegate.

Definition at line 1276 of file qitemdelegate.cpp.

1280 {
1281  Q_ASSERT(event);
1282  Q_ASSERT(model);
1283 
1284  // make sure that the item is checkable
1285  Qt::ItemFlags flags = model->flags(index);
1286  if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
1287  || !(flags & Qt::ItemIsEnabled))
1288  return false;
1289 
1290  // make sure that we have a check state
1291  QVariant value = index.data(Qt::CheckStateRole);
1292  if (!value.isValid())
1293  return false;
1294 
1295  // make sure that we have the right event type
1296  if ((event->type() == QEvent::MouseButtonRelease)
1297  || (event->type() == QEvent::MouseButtonDblClick)
1298  || (event->type() == QEvent::MouseButtonPress)) {
1299  QRect checkRect = check(option, option.rect, Qt::Checked);
1300  QRect emptyRect;
1301  doLayout(option, &checkRect, &emptyRect, &emptyRect, false);
1302  QMouseEvent *me = static_cast<QMouseEvent*>(event);
1303  if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
1304  return false;
1305 
1306  // eat the double click events inside the check rect
1307  if ((event->type() == QEvent::MouseButtonPress)
1308  || (event->type() == QEvent::MouseButtonDblClick))
1309  return true;
1310 
1311  } else if (event->type() == QEvent::KeyPress) {
1312  if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
1313  && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
1314  return false;
1315  } else {
1316  return false;
1317  }
1318 
1319  Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
1321  return model->setData(index, state, Qt::CheckStateRole);
1322 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
CheckState
Definition: qnamespace.h:1607
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
void doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
Qt::MouseButton button() const
Returns the button that caused the event.
Definition: qevent.h:101
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
int key
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90

◆ eventFilter()

bool QItemDelegate::eventFilter ( QObject editor,
QEvent event 
)
protectedvirtual

Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns false.

The following key press events are handled by default:

  • Tab
  • Backtab
  • Enter
  • Return
  • Esc

In the case of Tab, Backtab, Enter and Return key press events, the editor's data is comitted to the model and the editor is closed. If the event is a Tab key press the view will open an editor on the next item in the view. Likewise, if the event is a Backtab key press the view will open an editor on the previous item in the view.

If the event is a Esc key press event, the editor is closed without committing its data.

See also
commitData(), closeEditor()

Reimplemented from QObject.

Definition at line 1202 of file qitemdelegate.cpp.

1203 {
1204  QWidget *editor = qobject_cast<QWidget*>(object);
1205  if (!editor)
1206  return false;
1207  if (event->type() == QEvent::KeyPress) {
1208  switch (static_cast<QKeyEvent *>(event)->key()) {
1209  case Qt::Key_Tab:
1210  emit commitData(editor);
1212  return true;
1213  case Qt::Key_Backtab:
1214  emit commitData(editor);
1216  return true;
1217  case Qt::Key_Enter:
1218  case Qt::Key_Return:
1219 #ifndef QT_NO_TEXTEDIT
1220  if (qobject_cast<QTextEdit *>(editor) || qobject_cast<QPlainTextEdit *>(editor))
1221  return false; // don't filter enter key events for QTextEdit
1222  // We want the editor to be able to process the key press
1223  // before committing the data (e.g. so it can do
1224  // validation/fixup of the input).
1225 #endif // QT_NO_TEXTEDIT
1226 #ifndef QT_NO_LINEEDIT
1227  if (QLineEdit *e = qobject_cast<QLineEdit*>(editor))
1228  if (!e->hasAcceptableInput())
1229  return false;
1230 #endif // QT_NO_LINEEDIT
1231  QMetaObject::invokeMethod(this, "_q_commitDataAndCloseEditor",
1232  Qt::QueuedConnection, Q_ARG(QWidget*, editor));
1233  return false;
1234  case Qt::Key_Escape:
1235  // don't commit data
1237  break;
1238  default:
1239  return false;
1240  }
1241  if (editor->parentWidget())
1242  editor->parentWidget()->setFocus();
1243  return true;
1244  } else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
1245  //the Hide event will take care of he editors that are in fact complete dialogs
1246  if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
1248  while (w) { // don't worry about focus changes internally in the editor
1249  if (w == editor)
1250  return false;
1251  w = w->parentWidget();
1252  }
1253 #ifndef QT_NO_DRAGANDDROP
1254  // The window may lose focus during an drag operation.
1255  // i.e when dragging involves the taskbar on Windows.
1256  if (QDragManager::self() && QDragManager::self()->object != 0)
1257  return false;
1258 #endif
1259 
1260  emit commitData(editor);
1261  emit closeEditor(editor, NoHint);
1262  }
1263  } else if (event->type() == QEvent::ShortcutOverride) {
1264  if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
1265  event->accept();
1266  return true;
1267  }
1268  }
1269  return false;
1270 }
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
bool isActiveWindow
whether this widget&#39;s window is the active window
Definition: qwidget.h:186
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
T * qobject_cast(QObject *object)
Definition: qobject.h:375
#define emit
Definition: qobjectdefs.h:76
void commitData(QWidget *editor)
This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model.
static QDragManager * self()
Definition: qdnd.cpp:163
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint=NoHint)
This signal is emitted when the user has finished editing an item using the specified editor...
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...

◆ hasClipping()

bool QItemDelegate::hasClipping ( ) const

Definition at line 346 of file qitemdelegate.cpp.

Referenced by drawDisplay().

347 {
348  Q_D(const QItemDelegate);
349  return d->clipPainting;
350 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61

◆ itemEditorFactory()

QItemEditorFactory * QItemDelegate::itemEditorFactory ( ) const

Returns the editor factory used by the item delegate.

If no editor factory is set, the function will return null.

See also
setItemEditorFactory()

Definition at line 645 of file qitemdelegate.cpp.

646 {
647  Q_D(const QItemDelegate);
648  return d->f;
649 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61

◆ paint()

void QItemDelegate::paint ( QPainter painter,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Renders the delegate using the given painter and style option for the item specified by index.

When reimplementing this function in a subclass, you should update the area held by the option's rect variable, using the option's state variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly.

For example, a selected item may need to be displayed differently to unselected items, as shown in the following code:

if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
int size = qMin(option.rect.width(), option.rect.height());
int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
double radius = (size/2.0) - (brightness/255.0 * size/2.0);
if (radius == 0.0)
return;
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(Qt::NoPen);
if (option.state & QStyle::State_Selected)
painter->setBrush(option.palette.highlightedText());
else

...

After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, it may be useful to call QPainter::save() before painting and QPainter::restore() afterwards.

See also
QStyle::State

Implements QAbstractItemDelegate.

Reimplemented in QCalendarDelegate, QComboBoxDelegate, and QCompleterItemDelegate.

Definition at line 416 of file qitemdelegate.cpp.

Referenced by QColumnViewDelegate::paint(), QCompleterItemDelegate::paint(), QComboBoxDelegate::paint(), QCalendarDelegate::paint(), and QCalendarDelegate::paintCell().

419 {
420  Q_D(const QItemDelegate);
421  Q_ASSERT(index.isValid());
422 
423  QStyleOptionViewItemV4 opt = setOptions(index, option);
424 
426  opt.features = v2 ? v2->features
427  : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
429  opt.locale = v3 ? v3->locale : QLocale();
430  opt.widget = v3 ? v3->widget : 0;
431 
432  // prepare
433  painter->save();
434  if (d->clipPainting)
435  painter->setClipRect(opt.rect);
436 
437  // get the data and the rectangles
438 
439  QVariant value;
440 
441  QPixmap pixmap;
442  QRect decorationRect;
443  value = index.data(Qt::DecorationRole);
444  if (value.isValid()) {
445  // ### we need the pixmap to call the virtual function
446  pixmap = decoration(opt, value);
447  if (value.type() == QVariant::Icon) {
448  d->tmp.icon = qvariant_cast<QIcon>(value);
449  d->tmp.mode = d->iconMode(option.state);
450  d->tmp.state = d->iconState(option.state);
451  const QSize size = d->tmp.icon.actualSize(option.decorationSize,
452  d->tmp.mode, d->tmp.state);
453  decorationRect = QRect(QPoint(0, 0), size);
454  } else {
455  d->tmp.icon = QIcon();
456  decorationRect = QRect(QPoint(0, 0), pixmap.size());
457  }
458  } else {
459  d->tmp.icon = QIcon();
460  decorationRect = QRect();
461  }
462 
463  QString text;
464  QRect displayRect;
465  value = index.data(Qt::DisplayRole);
466  if (value.isValid() && !value.isNull()) {
467  text = QItemDelegatePrivate::valueToText(value, opt);
468  displayRect = textRectangle(painter, d->textLayoutBounds(opt), opt.font, text);
469  }
470 
471  QRect checkRect;
472  Qt::CheckState checkState = Qt::Unchecked;
473  value = index.data(Qt::CheckStateRole);
474  if (value.isValid()) {
475  checkState = static_cast<Qt::CheckState>(value.toInt());
476  checkRect = check(opt, opt.rect, value);
477  }
478 
479  // do the layout
480 
481  doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
482 
483  // draw the item
484 
485  drawBackground(painter, opt, index);
486  drawCheck(painter, opt, checkRect, checkState);
487  drawDecoration(painter, opt, decorationRect, pixmap);
488  drawDisplay(painter, opt, displayRect, text);
489  drawFocus(painter, opt, displayRect);
490 
491  // done
492  painter->restore();
493 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
CheckState
Definition: qnamespace.h:1607
QRect textRectangle(QPainter *painter, const QRect &rect, const QFont &font, const QString &text) const
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
Renders a check indicator within the rectangle specified by rect, using the given painter and style o...
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QStyleOptionViewItemV4 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:609
void doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
virtual void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
Renders the decoration pixmap within the rectangle specified by rect using the given painter and styl...
The QStyleOptionViewItemV2 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:562
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
The QStyleOptionViewItemV3 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:590
ViewItemFeatures features
a bitwise OR of the features that describe this view item
Definition: qstyleoption.h:577
QPixmap decoration(const QStyleOptionViewItem &option, const QVariant &variant) const
Returns the pixmap used to decorate the root of the item view.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QStyleOptionViewItem setOptions(const QModelIndex &index, const QStyleOptionViewItem &option) const
virtual void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
Renders the item view text within the rectangle specified by rect using the given painter and style o...
static QString valueToText(const QVariant &value, const QStyleOptionViewItemV4 &option)
const QWidget * widget
Definition: qstyleoption.h:596
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
T qstyleoption_cast(const QStyleOption *opt)
Definition: qstyleoption.h:885
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QSize decorationSize
the size of the decoration for the item
Definition: qstyleoption.h:551
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
Renders the region within the rectangle specified by rect, indicating that it has the focus...
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Renders the item background for the given index, using the given painter and style option...
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
#define text
Definition: qobjectdefs.h:80
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

◆ rect()

QRect QItemDelegate::rect ( const QStyleOptionViewItem option,
const QModelIndex index,
int  role 
) const
protected
Warning
This function is not part of the public interface.

Definition at line 1089 of file qitemdelegate.cpp.

Referenced by drawCheck(), drawDecoration(), drawFocus(), and sizeHint().

1091 {
1092  Q_D(const QItemDelegate);
1093  QVariant value = index.data(role);
1094  if (role == Qt::CheckStateRole)
1095  return check(option, option.rect, value);
1096  if (value.isValid() && !value.isNull()) {
1097  switch (value.type()) {
1098  case QVariant::Invalid:
1099  break;
1100  case QVariant::Pixmap:
1101  return QRect(QPoint(0, 0), qvariant_cast<QPixmap>(value).size());
1102  case QVariant::Image:
1103  return QRect(QPoint(0, 0), qvariant_cast<QImage>(value).size());
1104  case QVariant::Icon: {
1105  QIcon::Mode mode = d->iconMode(option.state);
1106  QIcon::State state = d->iconState(option.state);
1107  QIcon icon = qvariant_cast<QIcon>(value);
1108  QSize size = icon.actualSize(option.decorationSize, mode, state);
1109  return QRect(QPoint(0, 0), size); }
1110  case QVariant::Color:
1111  return QRect(QPoint(0, 0), option.decorationSize);
1112  case QVariant::String:
1113  default: {
1115  value = index.data(Qt::FontRole);
1116  QFont fnt = qvariant_cast<QFont>(value).resolve(option.font);
1117  return textRectangle(0, d->textLayoutBounds(option), fnt, text); }
1118  }
1119  }
1120  return QRect();
1121 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
QFont font
the font used for the item
Definition: qstyleoption.h:552
QRect textRectangle(QPainter *painter, const QRect &rect, const QFont &font, const QString &text) const
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
State
This enum describes the state for which a pixmap is intended to be used.
Definition: qicon.h:64
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition: qicon.h:63
static QString valueToText(const QVariant &value, const QStyleOptionViewItemV4 &option)
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QSize actualSize(const QSize &size, Mode mode=Normal, State state=Off) const
Returns the actual size of the icon for the requested size, mode, and state.
Definition: qicon.cpp:730
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QSize decorationSize
the size of the decoration for the item
Definition: qstyleoption.h:551
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
#define text
Definition: qobjectdefs.h:80
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

◆ selected()

QPixmap * QItemDelegate::selected ( const QPixmap pixmap,
const QPalette palette,
bool  enabled 
) const
protected

Returns the selected version of the given pixmap using the given palette.

Warning
This function is not part of the public interface.

The enabled argument decides whether the normal or disabled highlight color of the palette is used.

Definition at line 1058 of file qitemdelegate.cpp.

Referenced by drawDecoration().

1059 {
1061  QPixmap *pm = QPixmapCache::find(key);
1062  if (!pm) {
1064 
1067  color.setAlphaF((qreal)0.3);
1068 
1069  QPainter painter(&img);
1070  painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
1071  painter.fillRect(0, 0, img.width(), img.height(), color);
1072  painter.end();
1073 
1075  int n = (img.byteCount() >> 10) + 1;
1076  if (QPixmapCache::cacheLimit() < n)
1078 
1079  QPixmapCache::insert(key, selected);
1080  pm = QPixmapCache::find(key);
1081  }
1082  return pm;
1083 }
static int cacheLimit()
Returns the cache limit (in kilobytes).
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
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:2197
double qreal
Definition: qglobal.h:1193
Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
Definition: qpixmap.cpp:94
static QString qPixmapSerial(quint64 i, bool enabled)
int byteCount() const
Returns the number of bytes occupied by the image data.
Definition: qimage.cpp:1800
The QString class provides a Unicode character string.
Definition: qstring.h:83
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
static QPixmap * find(const QString &key)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
void setAlphaF(qreal alpha)
Sets the alpha of this color to alpha.
Definition: qcolor.cpp:1117
static bool insert(const QString &key, const QPixmap &pixmap)
Inserts a copy of the pixmap pixmap associated with the key into the cache.
int key
QPixmap * selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const
Returns the selected version of the given pixmap using the given palette.
static void setCacheLimit(int)
Sets the cache limit to n kilobytes.
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
#define enabled

◆ setClipping()

void QItemDelegate::setClipping ( bool  clip)

Definition at line 352 of file qitemdelegate.cpp.

353 {
355  d->clipPainting = clip;
356 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61

◆ setEditorData()

void QItemDelegate::setEditorData ( QWidget editor,
const QModelIndex index 
) const
virtual

Sets the data to be displayed and edited by the editor from the data model item specified by the model index.

The default implementation stores the data in the editor widget's Qt's Property System.

See also
QMetaProperty::isUser()

Reimplemented from QAbstractItemDelegate.

Definition at line 552 of file qitemdelegate.cpp.

553 {
554 #ifdef QT_NO_PROPERTIES
555  Q_UNUSED(editor);
556  Q_UNUSED(index);
557 #else
558  Q_D(const QItemDelegate);
559  QVariant v = index.data(Qt::EditRole);
560  QByteArray n = editor->metaObject()->userProperty().name();
561 
562  // ### Qt 5: remove
563  // A work-around for missing "USER true" in qdatetimeedit.h for
564  // QTimeEdit's time property and QDateEdit's date property.
565  // It only triggers if the default user property "dateTime" is
566  // reported for QTimeEdit and QDateEdit.
567  if (n == "dateTime") {
568  if (editor->inherits("QTimeEdit"))
569  n = "time";
570  else if (editor->inherits("QDateEdit"))
571  n = "date";
572  }
573 
574  // ### Qt 5: give QComboBox a USER property
575  if (n.isEmpty() && editor->inherits("QComboBox"))
576  n = d->editorFactory()->valuePropertyName(static_cast<QVariant::Type>(v.userType()));
577  if (!n.isEmpty()) {
578  if (!v.isValid())
579  v = QVariant(editor->property(n).userType(), (const void *)0);
580  editor->setProperty(n, v);
581  }
582 #endif
583 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object&#39;s name property to value.
Definition: qobject.cpp:3755
#define Q_D(Class)
Definition: qglobal.h:2482
bool inherits(const char *classname) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass t...
Definition: qobject.h:275
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
const char * name() const
Returns this property&#39;s name.
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
QMetaProperty userProperty() const
Returns the property that has the USER flag set to true.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.

◆ setItemEditorFactory()

void QItemDelegate::setItemEditorFactory ( QItemEditorFactory factory)

Sets the editor factory to be used by the item delegate to be the factory specified.

If no editor factory is set, the item delegate will use the default editor factory.

See also
itemEditorFactory()

Definition at line 658 of file qitemdelegate.cpp.

659 {
661  d->f = factory;
662 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61

◆ setModelData()

void QItemDelegate::setModelData ( QWidget editor,
QAbstractItemModel model,
const QModelIndex index 
) const
virtual

Gets data from the editor widget and stores it in the specified model at the item index.

The default implementation gets the value to be stored in the data model from the editor widget's Qt's Property System.

See also
QMetaProperty::isUser()

Reimplemented from QAbstractItemDelegate.

Definition at line 595 of file qitemdelegate.cpp.

598 {
599 #ifdef QT_NO_PROPERTIES
600  Q_UNUSED(model);
601  Q_UNUSED(editor);
602  Q_UNUSED(index);
603 #else
604  Q_D(const QItemDelegate);
605  Q_ASSERT(model);
606  Q_ASSERT(editor);
607  QByteArray n = editor->metaObject()->userProperty().name();
608  if (n.isEmpty())
609  n = d->editorFactory()->valuePropertyName(
610  static_cast<QVariant::Type>(model->data(index, Qt::EditRole).userType()));
611  if (!n.isEmpty())
612  model->setData(index, editor->property(n), Qt::EditRole);
613 #endif
614 }
double d
Definition: qnumeric_p.h:62
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
const char * name() const
Returns this property&#39;s name.
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
QMetaProperty userProperty() const
Returns the property that has the USER flag set to true.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.

◆ setOptions()

QStyleOptionViewItem QItemDelegate::setOptions ( const QModelIndex index,
const QStyleOptionViewItem option 
) const
protected
Warning
This function is not part of the public interface.

Definition at line 1328 of file qitemdelegate.cpp.

Referenced by paint().

1330 {
1331  QStyleOptionViewItem opt = option;
1332 
1333  // set font
1334  QVariant value = index.data(Qt::FontRole);
1335  if (value.isValid()){
1336  opt.font = qvariant_cast<QFont>(value).resolve(opt.font);
1337  opt.fontMetrics = QFontMetrics(opt.font);
1338  }
1339 
1340  // set text alignment
1341  value = index.data(Qt::TextAlignmentRole);
1342  if (value.isValid())
1343  opt.displayAlignment = Qt::Alignment(value.toInt());
1344 
1345  // set foreground brush
1346  value = index.data(Qt::ForegroundRole);
1347  if (value.canConvert<QBrush>())
1348  opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value));
1349 
1350  return opt;
1351 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QFont font
the font used for the item
Definition: qstyleoption.h:552
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
void setBrush(ColorRole cr, const QBrush &brush)
Sets the brush for the given color role to the specified brush for all groups in the palette...
Definition: qpalette.h:206
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
Qt::Alignment displayAlignment
the alignment of the display value for the item
Definition: qstyleoption.h:547
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485

◆ sizeHint()

QSize QItemDelegate::sizeHint ( const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Returns the size needed by the delegate to display the item specified by index, taking into account the style information provided by option.

When reimplementing this function, note that in case of text items, QItemDelegate adds a margin (i.e. 2 * QStyle::PM_FocusFrameHMargin) to the length of the text.

Implements QAbstractItemDelegate.

Reimplemented in QComboBoxDelegate.

Definition at line 505 of file qitemdelegate.cpp.

Referenced by QComboBoxDelegate::sizeHint().

507 {
508  QVariant value = index.data(Qt::SizeHintRole);
509  if (value.isValid())
510  return qvariant_cast<QSize>(value);
511  QRect decorationRect = rect(option, index, Qt::DecorationRole);
512  QRect displayRect = rect(option, index, Qt::DisplayRole);
513  QRect checkRect = rect(option, index, Qt::CheckStateRole);
514 
515  doLayout(option, &checkRect, &decorationRect, &displayRect, true);
516 
517  return (decorationRect|displayRect|checkRect).size();
518 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
void doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
QRect rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485

◆ textRectangle()

QRect QItemDelegate::textRectangle ( QPainter painter,
const QRect rect,
const QFont font,
const QString text 
) const
protected
Warning
This function is not part of the public interface.

Definition at line 1159 of file qitemdelegate.cpp.

Referenced by paint(), rect(), and updateEditorGeometry().

1161 {
1162  Q_D(const QItemDelegate);
1163  d->textOption.setWrapMode(QTextOption::WordWrap);
1164  d->textLayout.setTextOption(d->textOption);
1165  d->textLayout.setFont(font);
1166  d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text));
1167  QSizeF fpSize = d->doTextLayout(rect.width());
1168  const QSize size = QSize(qCeil(fpSize.width()), qCeil(fpSize.height()));
1169  // ###: textRectangle should take style option as argument
1170  const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
1171  return QRect(0, 0, size.width() + 2 * textMargin, size.height());
1172 }
double d
Definition: qnumeric_p.h:62
int qCeil(qreal v)
Definition: qmath.h:63
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
qreal width() const
Returns the width.
Definition: qsize.h:284
qreal height() const
Returns the height.
Definition: qsize.h:287
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
static QStyle * style()
Returns the application&#39;s style object.
#define Q_D(Class)
Definition: qglobal.h:2482
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
int width() const
Returns the width.
Definition: qsize.h:126
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
static QString replaceNewLine(QString text)

◆ updateEditorGeometry()

void QItemDelegate::updateEditorGeometry ( QWidget editor,
const QStyleOptionViewItem option,
const QModelIndex index 
) const
virtual

Updates the editor for the item specified by index according to the style option given.

Reimplemented from QAbstractItemDelegate.

Definition at line 621 of file qitemdelegate.cpp.

624 {
625  if (!editor)
626  return;
627  Q_ASSERT(index.isValid());
628  QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole));
630  QRect pixmapRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect());
631  QRect textRect = textRectangle(0, option.rect, option.font, text);
632  QRect checkRect = check(option, textRect, index.data(Qt::CheckStateRole));
633  QStyleOptionViewItem opt = option;
634  opt.showDecorationSelected = true; // let the editor take up all available space
635  doLayout(opt, &checkRect, &pixmapRect, &textRect, false);
636  editor->setGeometry(textRect);
637 }
QFont font
the font used for the item
Definition: qstyleoption.h:552
QRect textRectangle(QPainter *painter, const QRect &rect, const QFont &font, const QString &text) const
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QRect intersected(const QRect &other) const
Returns the intersection of this rectangle and the given rectangle.
Definition: qrect.h:481
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
void doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QPixmap decoration(const QStyleOptionViewItem &option, const QVariant &variant) const
Returns the pixmap used to decorate the root of the item view.
bool showDecorationSelected
whether the decoration should be highlighted on selected items
Definition: qstyleoption.h:553
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QSize decorationSize
the size of the decoration for the item
Definition: qstyleoption.h:551
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
static QString replaceNewLine(QString text)
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
#define text
Definition: qobjectdefs.h:80

Properties

◆ clipping

QItemDelegate::clipping
private

if the delegate should clip the paint events

Since
4.2

This property will set the paint clip to the size of the item. The default value is on. It is useful for cases such as when images are larger than the size of the item.

Definition at line 64 of file qitemdelegate.h.


The documentation for this class was generated from the following files: