-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
executable file
·217 lines (199 loc) · 6.16 KB
/
widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "widget.h"
#include "ui_widget.h"
#include <QBitmap>
#include <QBrush>
#include <QDebug>
#include <QImage>
#include <QMouseEvent>
#include <QPainter>
#include <QPalette>
#include <QPixmap>
#include <QRect>
#include <QSignalMapper>
Widget::Widget(QWidget* parent)
: QWidget(parent)
, ui(new Ui::Widget)
, m_currentDrawImageIndx(0)
, isFullScreen(false)
{
ui->setupUi(this);
// this->setPalette(QPalette(Qt::transparent));
// this->setAutoFillBackground(true);
//设置左右按钮
QPixmap pixmapLeft(":/images/left");
ui->btnLeft->setMask(QBitmap(pixmapLeft.mask()));
ui->btnLeft->resize(pixmapLeft.size());
QPixmap pixmapRight(":/images/right");
ui->btnRight->setMask(QBitmap(pixmapRight.mask()));
ui->btnRight->resize(pixmapRight.size());
ui->frame->installEventFilter(this);
// painter = nullptr;
//初始化图片名字列表
initImageFileNameList();
//初始化信号槽
// connect(ui->btnLeft, &QPushButton::clicked, this,
// &Widget::switchButtonClicked);
// connect(ui->btnRight, &QPushButton::clicked, this,
// &Widget::switchButtonClicked);
connect(ui->btnLeft, &QPushButton::clicked,
[=]() { switchButtonClicked(-1); });
connect(ui->btnRight, &QPushButton::clicked,
[=]() { switchButtonClicked(1); });
qDebug() << "fsdfdsfdsf";
}
Widget::~Widget()
{
delete ui;
}
/**
* @brief 初始化图片名字列表
*/
void Widget::initImageFileNameList()
{
//有15张图片
for (int i = 0; i < IMAGENUM; ++i) {
QString imageName = IMAGENAMEPATTERN.arg(i);
m_imageFileNameList.append(imageName);
}
}
void Widget::switchButtonClicked(int i)
{
m_currentDrawImageIndx += i;
if (m_currentDrawImageIndx < 0) {
m_currentDrawImageIndx = IMAGENUM - 1;
}
if (m_currentDrawImageIndx >= IMAGENUM) {
m_currentDrawImageIndx = 0;
}
// paintOnWidget(ui->frame);
ui->frame->update();
}
/*
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
//创建QPainter对象
QPainter painter(ui->frame);
// QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
// int W = this->width();
// int H = this->height();
int W = ui->frame->width();
int H = ui->frame->height();
const int width = 1500;
const int height = 1200;
int availableW = 0;
int availabelH = 0;
int side = qMin(W, H);
if (W > H) {
double xi = static_cast<double>(side) / static_cast<double>(height);
availabelH = side;
availableW = static_cast<int>(width * xi);
} else {
double xi = static_cast<double>(side) / static_cast<double>(width);
availabelH = static_cast<int>(height * xi);
availableW = side;
}
qDebug() << W << H;
qDebug() << availableW << availabelH;
qDebug() << "*********************************";
painter.setViewport((W - availableW) / 2, (H - availabelH) / 2, availableW,
availabelH);
painter.setWindow(-(width / 2), -(height) / 2, width, height);
// painter.setWindow(0, 0, width, height);
QRect rect(-(width / 2), -(height) / 2, width, height);
// QRect rect(0, 0, width, height);
//设置画笔
// QPen pen;
// pen.setWidth(1);
// pen.setColor(Qt::red);
// pen.setStyle(Qt::SolidLine);
// pen.setCapStyle(Qt::FlatCap);
// pen.setJoinStyle(Qt::BevelJoin);
// painter.setPen(pen);
//设置画刷
// QPixmap texturePixmap(":images/screen");
// QBrush brush;
// brush.setColor(Qt::yellow);
// brush.setStyle(Qt::TexturePattern);
// brush.setTexture(texturePixmap);
// painter.setBrush(brush);
//绘图
// painter.drawRect(rect);
// painter.drawRect(rect);
painter.drawImage(rect, QImage(":images/fruit_12.jpg"));
}
*/
bool Widget::eventFilter(QObject* watched, QEvent* e)
{
if (watched == ui->frame) {
if (e->type() == QEvent::Paint) {
paintOnWidget(ui->frame);
return true;
}
}
return QWidget::eventFilter(watched, e);
}
void Widget::paintOnWidget(QWidget* w)
{
qDebug() << "paintOnWidget";
//创建QPainter对象
QPainter painter(w);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
/*
if (painter == nullptr) {
painter = new QPainter(w);
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::TextAntialiasing);
}
*/
qDebug() << w->metaObject()->className();
qDebug() << w->styleSheet();
qDebug() << "================";
int W = w->width();
int H = w->height();
const int width = 1500;
const int height = 1200;
int availableW = 0;
int availabelH = 0;
int side = qMin(W, H);
if (W > H) {
double xi = static_cast<double>(side) / static_cast<double>(height);
availabelH = side;
availableW = static_cast<int>(width * xi);
} else {
double xi = static_cast<double>(side) / static_cast<double>(width);
availabelH = static_cast<int>(height * xi);
availableW = side;
}
qDebug() << W << H;
qDebug() << availableW << availabelH;
qDebug() << "*********************************";
painter.setViewport((W - availableW) / 2, (H - availabelH) / 2, availableW,
availabelH);
painter.setWindow(-(width / 2), -(height) / 2, width, height);
// painter.setWindow(0, 0, width, height);
QRect rect(-(width / 2), -(height) / 2, width, height);
painter.drawImage(rect,
QImage(m_imageFileNameList.at(m_currentDrawImageIndx)));
painter.end();
//要或不要都能运行,内存占用增长也均一样,都保持在同一个数值后不变了
// delete painter;
// painter = nullptr;
}
// void Widget::mousePressEvent(QMouseEvent *event)
//{
// qDebug() << "mousePressEvent";
//}
void Widget::mouseDoubleClickEvent(QMouseEvent* event)
{
isFullScreen = !isFullScreen;
if (isFullScreen) {
this->showFullScreen();
} else {
this->showNormal();
}
QWidget::mouseDoubleClickEvent(event);
}