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 #include "MyView.h"
13 
14 // ======================================================================
15 class SimpleItem : public QGraphicsItem {
16 private:
17  enum {nPenWidth = 3};
18 
19 public:
20  virtual QRectF boundingRect() const
21  {
22  QPointF ptPosition(-10 - nPenWidth, -10 - nPenWidth);
23  QSizeF size(20 + nPenWidth * 2, 20 + nPenWidth * 2);
24  return QRectF(ptPosition, size);
25  }
26 
27  virtual void paint(QPainter* ppainter,
28  const QStyleOptionGraphicsItem*,
29  QWidget*
30  )
31  {
32  ppainter->save();
33  ppainter->setPen(QPen(Qt::blue, nPenWidth));
34  ppainter->drawEllipse(-10, -10, 20, 20);
35  ppainter->restore();
36  }
37 
38  virtual void mousePressEvent(QGraphicsSceneMouseEvent* pe)
39  {
40 
41  QApplication::setOverrideCursor(Qt::PointingHandCursor);
42  QGraphicsItem::mousePressEvent(pe);
43  }
44 
45  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* pe)
46  {
47  QApplication::restoreOverrideCursor();
48  QGraphicsItem::mouseReleaseEvent(pe);
49  }
50 };
51 
52 // ----------------------------------------------------------------------
53 int main(int argc, char** argv)
54 {
55  QApplication app(argc, argv);
56  QWidget wgt;
57  QGraphicsScene scene(QRectF(-100, -100, 640, 480));
58 
59  MyView* pView = new MyView(&scene);
60  QPushButton* pcmdZoomIn = new QPushButton("&Zoom In");
61  QPushButton* pcmdZoomOut = new QPushButton("Z&oom Out");
62  QPushButton* pcmdRotateLeft = new QPushButton("&Rotate Left");
63  QPushButton* pcmdRotateRight = new QPushButton("Ro&tate Right");
64 
65  pView->setRenderHint(QPainter::Antialiasing, true);
66 
67  SimpleItem* pSimpleItem = new SimpleItem;
68  scene.addItem(pSimpleItem);
69  pSimpleItem->setPos(0, 0);
70  pSimpleItem->setFlags(QGraphicsItem::ItemIsMovable);
71 
72  QGraphicsPixmapItem* pPixmapItem =
73  scene.addPixmap(QPixmap(":/haus2.jpg"));
74  pPixmapItem->setParentItem(pSimpleItem);
75  pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable);
76 
77  QObject::connect(pcmdZoomIn, SIGNAL(clicked()),
78  pView, SLOT(slotZoomIn())
79  );
80  QObject::connect(pcmdZoomOut, SIGNAL(clicked()),
81  pView, SLOT(slotZoomOut())
82  );
83  QObject::connect(pcmdRotateLeft, SIGNAL(clicked()),
84  pView, SLOT(slotRotateLeft())
85  );
86  QObject::connect(pcmdRotateRight, SIGNAL(clicked()),
87  pView, SLOT(slotRotateRight())
88  );
89 
90  //Layout setup
91  QVBoxLayout* pvbxLayout = new QVBoxLayout;
92  pvbxLayout->addWidget(pView);
93  pvbxLayout->addWidget(pcmdZoomIn);
94  pvbxLayout->addWidget(pcmdZoomOut);
95  pvbxLayout->addWidget(pcmdRotateLeft);
96  pvbxLayout->addWidget(pcmdRotateRight);
97  wgt.setLayout(pvbxLayout);
98 
99  wgt.show();
100 
101  return app.exec();
102 }
virtual QRectF boundingRect() const
Definition: main.cpp:20
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *pe)
Definition: main.cpp:45
int main(int argc, char **argv)
Definition: main.cpp:15
virtual void mousePressEvent(QGraphicsSceneMouseEvent *pe)
Definition: main.cpp:38
virtual void paint(QPainter *ppainter, const QStyleOptionGraphicsItem *, QWidget *)
Definition: main.cpp:27
Definition: MyView.h:16