-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymsgitem.cpp
79 lines (65 loc) · 2.49 KB
/
mymsgitem.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
#include "mymsgitem.h"
#include "ui_mymsgitem.h"
#include <QPainter>
#include <QLabel>
MyMsgItem::MyMsgItem(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyMsgItem)
{
ui->setupUi(this);
//设置边距
}
MyMsgItem::~MyMsgItem()
{
delete ui;
}
void MyMsgItem::setData(QString nickName, QString msg,QString curTime) {
ui ->nickName ->setText(nickName.mid(1,nickName.size()-1));
ui ->MsgBoard ->setText(msg);
ui ->timeShow ->setText(curTime);
int iconId = nickName.mid(0,1).toInt();
QString imgPath = ":/Icons/icon/img" +QString::number(iconId + 1) + ".jpg";
int myWidth = ui->msgIcon->width() - 6;
int myHeight = ui->msgIcon->height();
QPixmap originPath(imgPath);
QPixmap roundCircle(myWidth,myHeight);
roundCircle.fill(Qt::transparent);
QPainter painter(&roundCircle);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPainterPath path;
path.addEllipse(0,0,myWidth,myHeight); //绘制椭圆
painter.setClipPath(path);
painter.drawPixmap(0,0,myWidth,myHeight,originPath);
ui->msgIcon->setPixmap(roundCircle);
}
int MyMsgItem::getHeight() {
return ui -> MsgBoard ->height() + (ui ->nickName ->height());
}
#include <QDebug>
void MyMsgItem::setPosRight(bool isRight) {
ui ->MsgBoard ->setMargin(10);
ui ->MsgBoard ->setWordWrap(true);
ui ->MsgBoard ->adjustSize();
//设置一个最大宽度
ui ->MsgBoard ->setMaximumWidth(230);
//放右边
if(isRight == true) {
ui ->MsgBoard ->setGeometry(700 - (ui->MsgBoard->width()) - 50, ui->MsgBoard->y(),
ui->MsgBoard->width(),ui -> MsgBoard->height());
ui ->nickName ->setGeometry(700 - 70 -(ui ->nickName->width()),ui->nickName->y(),
ui->nickName->width(),ui -> nickName->height());
ui ->msgIcon -> setGeometry(700 - 50,ui ->MsgBoard ->y(),
ui->MsgBoard->width(),ui -> MsgBoard->height());
}
//放左边
else {
ui ->MsgBoard ->setGeometry(50, ui->MsgBoard->y(),
ui->MsgBoard->width(),ui -> MsgBoard->height());
ui ->nickName ->setGeometry(10, ui->nickName->y(),
ui->nickName->width(),ui -> nickName->height());
ui ->msgIcon -> setGeometry(10,ui ->MsgBoard ->y(),
ui->MsgBoard->width(),ui -> MsgBoard->height());
}
//修改当前自定义控件大小
this->setFixedSize(700,getHeight() + 20);
}