Qt 5.10 Book Examples
main.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // main.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 
13 // ======================================================================
14 class SimpleDelegate : public QStyledItemDelegate {
15 public:
16  SimpleDelegate(QObject* pobj = 0) : QStyledItemDelegate(pobj)
17  {
18  }
19 
20  void paint(QPainter* pPainter,
21  const QStyleOptionViewItem& option,
22  const QModelIndex& index
23  ) const
24  {
25  if (option.state & QStyle::State_MouseOver) {
26  QRect rect = option.rect;
27  QLinearGradient gradient(0, 0, rect.width(), rect.height());
28 
29  gradient.setColorAt(0, Qt::white);
30  gradient.setColorAt(0.5, Qt::blue);
31  gradient.setColorAt(1, Qt::green);
32  pPainter->setBrush(gradient);
33  pPainter->drawRect(rect);
34  }
35  QStyledItemDelegate::paint(pPainter, option, index);
36  }
37 
38  QSize sizeHint(const QStyleOptionViewItem& option,
39  const QModelIndex&/*index*/
40  ) const
41  {
42  return QSize(option.rect.width(), 55);
43  }
44 };
45 
46 // ----------------------------------------------------------------------
47 int main(int argc, char *argv[])
48 {
49  QApplication app(argc, argv);
50  QStringListModel model;
51  model.setStringList(QStringList() << "Item1" << "Item2" << "Item3");
52 
53  QListView listView;
54  listView.setModel(&model);
55  listView.setItemDelegate(new SimpleDelegate(&listView));
56  listView.viewport()->setAttribute(Qt::WA_Hover);
57  listView.show();
58 
59  return app.exec();
60 }
SimpleDelegate(QObject *pobj=0)
Definition: main.cpp:16
void paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: main.cpp:20
int main(int argc, char **argv)
Definition: main.cpp:15
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const
Definition: main.cpp:38