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 QLabel* lbl(const QPainter::CompositionMode& mode)
15 {
16  QLabel* plbl = new QLabel;
17  plbl->setFixedSize(100, 100);
18 
19  QRect rect(plbl->contentsRect());
20  QPainter painter;
21 
22  QImage sourceImage(rect.size(), QImage::Format_ARGB32_Premultiplied);
23  sourceImage.fill(QColor(0, 0, 0, 0));
24  painter.begin(&sourceImage);
25  painter.setRenderHint(QPainter::Antialiasing, true);
26  painter.setBrush(QBrush(QColor(0, 255, 0)));
27  painter.drawPolygon(QPolygon() << rect.bottomLeft()
28  << QPoint(rect.center().x(), 0)
29  << rect.bottomRight()
30  );
31  painter.end();
32 
33  QImage resultImage(rect.size(), QImage::Format_ARGB32_Premultiplied);
34  painter.begin(&resultImage);
35  painter.setRenderHint(QPainter::Antialiasing, true);
36  painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
37  painter.setPen(QPen(QColor(0, 255, 0), 4));
38  painter.setBrush(QBrush(QColor(255, 0, 0)));
39  painter.drawEllipse(rect);
40  painter.setCompositionMode(mode);
41  painter.drawImage(rect, sourceImage);
42  painter.end();
43 
44  plbl->setPixmap(QPixmap::fromImage(resultImage));
45  return plbl;
46 }
47 
48 
49 // ----------------------------------------------------------------------
50 int main(int argc, char** argv)
51 {
52  QApplication app(argc, argv);
53  QWidget wgt;
54 
55  //Layout Setup
56  QGridLayout* pgrd = new QGridLayout;
57  pgrd->addWidget(lbl(QPainter::CompositionMode_Source), 0, 0);
58  pgrd->addWidget(new QLabel("<CENTER>Source</CENTER>"), 1, 0);
59  pgrd->addWidget(lbl(QPainter::CompositionMode_SourceOver), 0, 1);
60  pgrd->addWidget(new QLabel("<CENTER>SourceOver</CENTER>"), 1, 1);
61  pgrd->addWidget(lbl(QPainter::CompositionMode_SourceIn), 0, 2);
62  pgrd->addWidget(new QLabel("<CENTER>SourceIn</CENTER>"), 1, 2);
63  pgrd->addWidget(lbl(QPainter::CompositionMode_SourceOut), 0, 3);
64  pgrd->addWidget(new QLabel("<CENTER>SourceOut</CENTER>"), 1, 3);
65  pgrd->addWidget(lbl(QPainter::CompositionMode_SourceAtop), 0, 4);
66  pgrd->addWidget(new QLabel("<CENTER>SourceAtop</CENTER>"), 1, 4);
67  pgrd->addWidget(lbl(QPainter::CompositionMode_Clear), 0, 5);
68  pgrd->addWidget(new QLabel("<CENTER>Clear</CENTER>"), 1, 5);
69  pgrd->addWidget(lbl(QPainter::CompositionMode_Destination), 2, 0);
70  pgrd->addWidget(new QLabel("<CENTER>Destination</CENTER>"), 3, 0);
71  pgrd->addWidget(lbl(QPainter::CompositionMode_DestinationOver), 2, 1);
72  pgrd->addWidget(new QLabel("<CENTER>DestinationOver</CENTER>"), 3, 1);
73  pgrd->addWidget(lbl(QPainter::CompositionMode_DestinationIn), 2, 2);
74  pgrd->addWidget(new QLabel("<CENTER>DestinationIn</CENTER>"), 3, 2);
75  pgrd->addWidget(lbl(QPainter::CompositionMode_DestinationOut), 2, 3);
76  pgrd->addWidget(new QLabel("<CENTER>DestinationOut</CENTER>"), 3, 3);
77  pgrd->addWidget(lbl(QPainter::CompositionMode_DestinationAtop), 2, 4);
78  pgrd->addWidget(new QLabel("<CENTER>DestinationAtop</CENTER>"), 3, 4);
79  pgrd->addWidget(lbl(QPainter::CompositionMode_Xor), 2, 5);
80  pgrd->addWidget(new QLabel("<CENTER>Xor</CENTER>"), 3, 5);
81  wgt.setLayout(pgrd);
82 
83  wgt.show();
84 
85  return app.exec();
86 }
QLabel * lbl(const QPainter::CompositionMode &mode)
Definition: main.cpp:14
int main(int argc, char **argv)
Definition: main.cpp:15