fork download
  1. // ======================================================================
  2. // main.cpp
  3. // ======================================================================
  4. // This file is a part of the book
  5. // "Qt 5.10 Professional programming with C++"
  6. // http://q...content-available-to-author-only...k.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. }
  61.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:21: fatal error: QtWidgets: No such file or directory
 #include <QtWidgets>
                     ^
compilation terminated.
stdout
Standard output is empty