Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //mylabelchat.h
- #ifndef MYLABELCHAT_H
- #define MYLABELCHAT_H
- #include <QWidget>
- #include <QLabel>
- #include <QObject>
- #include <QMouseEvent>
- #include <QPaintEvent>
- #include <QEvent>
- class QGraphicsView;
- class QGraphicsPixmapItem;
- class QGraphicsScene;
- class QPushButton;
- class MyLabelChat : public QWidget
- {
- Q_OBJECT
- public:
- MyLabelChat(QPixmap pixmap, QWidget *parent = nullptr);
- QPushButton *button_close;
- bool _flag_active = false;
- int _status = 0;
- int _num_row = 0;
- int _num_mess = 0;
- QLabel *_text;
- void my_repaint(QColor col, int status);
- void my_update();
- void set_text(QString);
- signals:
- void signal_chatuser_pressed(const QString &);
- void signal_delete_user(const QString &);
- protected:
- bool event( QEvent *event) override;
- void mousePressEvent(QMouseEvent *event) override;
- private slots:
- void delete_chat_user();
- private:
- QGraphicsView *foto;
- QGraphicsScene *scene;
- QColor color;
- QPixmap _pixmap;
- };
- #endif // MYLABELCHAT_H
- //mylabelchat.cpp
- #include "mylabelchat.h"
- #include <QHBoxLayout>
- #include <QPainter>
- #include <QDebug>
- #include <QGraphicsView>
- #include <QGraphicsPixmapItem>
- #include <QGraphicsScene>
- #include <QPushButton>
- #include "global.h"
- MyLabelChat::MyLabelChat(QPixmap pixmap, QWidget *parent) : QWidget(parent)
- {
- _pixmap = pixmap.scaled(QSize(80, 80), Qt::KeepAspectRatio, Qt::SmoothTransformation);
- QHBoxLayout *box = new QHBoxLayout(this);
- box->setSpacing(0);
- box->setMargin(2);
- scene = new QGraphicsScene(this);
- foto = new QGraphicsView(scene);
- foto->setSceneRect(QRect(0, 0, 80, 80));
- foto->setScene(scene);
- foto->setRenderHint(QPainter::Antialiasing, true);
- foto->setStyleSheet("border:none;");
- foto->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
- foto->setFixedSize(80, 80);
- _text = new QLabel(this);
- _text->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
- _text->setFixedHeight(80);
- button_close = new QPushButton(_text);
- button_close->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
- button_close->setFixedSize(16, 16);
- button_close->move(_text->width() - 12, 0);
- button_close->setStyleSheet("QPushButton{ border-image: url(:/image/delete2.png); }");
- button_close->setToolTip("Удалить");
- button_close->setVisible(false);
- connect(button_close, &QPushButton::clicked, this, &MyLabelChat::delete_chat_user);
- color = this->palette().color(QPalette::Background);
- this->setFixedHeight(80);
- box->addWidget(foto);
- box->addWidget(_text);
- my_update();
- }
- void MyLabelChat::set_text(QString str)
- {
- _text->setText(str);
- }
- bool MyLabelChat::event(QEvent *event)
- {
- if (event->type() == QEvent::Enter )
- {
- if(!_flag_active)
- my_repaint(QColor(186,226,255), _status);
- button_close->setVisible(true);
- }
- else
- if(event->type() == QEvent::Leave)
- {
- if(!_flag_active)
- my_repaint(color, _status);
- button_close->setVisible(false);
- }
- return QWidget::event(event);
- }
- void MyLabelChat::mousePressEvent(QMouseEvent *event)
- {
- if(event->button() == Qt::LeftButton)
- {
- my_repaint(QColor(166,206,255), _status);
- _flag_active = true;
- emit signal_chatuser_pressed(this->objectName());
- }
- }
- void MyLabelChat::delete_chat_user()
- {
- emit signal_delete_user(this->objectName());
- }
- void MyLabelChat::my_repaint(QColor col, int status)
- {
- scene->clear();
- QColor ell_color;
- switch (status)
- {
- case Enabled:
- ell_color = Qt::green;
- break;
- case Lunch:
- ell_color = Qt::blue;
- break;
- case Conference:
- ell_color = Qt::red;
- break;
- }
- QPixmap temp = _pixmap;
- int pen_widht = 40;
- int border = 8;
- scene->addPixmap(temp);
- QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem;
- ellipse->setRect(QRectF(-pen_widht/2 + border/2, -pen_widht/2 + border/2,
- temp.width()+pen_widht -border, temp.height()+pen_widht -border));
- ellipse->setBrush(QBrush(Qt::transparent));
- ellipse->setPen(QPen(col,pen_widht));
- scene->addItem(ellipse);//круглая рамка
- if(status > 0)
- {
- QGraphicsEllipseItem *ellipse2 = new QGraphicsEllipseItem;
- ellipse2->setRect(QRectF(56, 60, 16, 16));
- ellipse2->setBrush(ell_color);
- ellipse2->setPen(QPen(Qt::white,2));
- scene->addItem(ellipse2);//статус
- }
- if(_num_mess)
- {
- int size = 28;
- QPixmap pix(":/image/email_3.png");
- QPixmap new_pix = pix.scaledToWidth(size, Qt::SmoothTransformation);
- QPainter painter;//количество непрочитанных сообщений
- painter.begin(&new_pix);
- painter.setRenderHint(QPainter::Antialiasing);
- painter.setPen(Qt::red);
- painter.setFont(QFont("Arial", 15, QFont::Bold));
- painter.drawText(QRectF(0, -3, size, size), Qt::AlignCenter, QString::number(_num_mess));
- painter.end();
- QGraphicsPixmapItem *item = scene->addPixmap(new_pix);
- item->setPos(QPointF(50, 6));
- }
- _text->setStyleSheet(QString("background-color: "+ col.name()));
- }
- void MyLabelChat::my_update()
- {
- if(!_flag_active)
- my_repaint(color, _status);
- else
- my_repaint(QColor(166,206,255), _status);
- }
- //использование
- QPixmap pixmap("image.png");
- MyLabelChat *label = new MyLabelChat(pixmap, this);
- QVBoxLayout* layout = new QVBoxLayout(this);
- layout->addWidget(label);
- connect(label, &MyLabelChat::signal_chatuser_pressed, this, &MainWindow::slot_chatuser_pressed);
- connect(label, &MyLabelChat::signal_delete_user, this, &MainWindow::slot_delete_user);
- label->set_text("name");
- label->show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement