-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabelWithPbtn.cpp
More file actions
69 lines (58 loc) · 1.99 KB
/
Copy pathlabelWithPbtn.cpp
File metadata and controls
69 lines (58 loc) · 1.99 KB
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
#include "labelWithPbtn.h"
#include <QHBoxLayout>
#include <QSizePolicy>
#include <QImage>
#include <QPixmap>
#include <QDebug>
#include <QPalette>
#include <QBuffer>
#include <QFileDialog>
labelWithPbtn::labelWithPbtn(QWidget *parent) :QLabel(parent)
{
init();
}
void labelWithPbtn::init()
{
hlay->addWidget(setPhotoBtn);
this->setLayout(hlay);
setPhotoBtn->setObjectName("setPhotoBtn");
setPhotoBtn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); //尺寸固定
setPhotoBtn->resize(this->size());
setPhotoBtn->setText("修改头像");
setPhotoBtn->setStyleSheet("QPushButton#setPhotoBtn"
"{"
"background-color: transparent;" //鼠标未在按钮上时透明,要包括背景和字体透明
"color: transparent;"
"border: none;" //不设置border,前两个会失效
"font: 20pt \"微软雅黑\";"
"}"
"QPushButton#setPhotoBtn:hover"
"{"
"background-color: rgba(173, 173, 173, 75);"
"color: rgb(117, 117, 117);"
"border: none;"
"}");
connect(setPhotoBtn, &QPushButton::clicked, this, &labelWithPbtn::setImage);
}
void labelWithPbtn::setPixmap(const QPixmap & pix)
{
QLabel::setPixmap(pix);
//更新iamgeData
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
pix.save(&buffer,"png");
emit imageDataChanged(imageData);
}
void labelWithPbtn::setImage()
{
QString fileName = QFileDialog::getOpenFileName(this,"选择图片文件","","照片(*.jpg *.png)");
if (fileName.isEmpty())
return;
QPixmap pix;
pix.load(fileName);
this->setPixmap(pix);
}
QByteArray labelWithPbtn::getImageData()
{
return imageData;
}