聊天气泡的绘制(圆角矩形+三角形+黑色边框,关键学会QPainter的draw函数就行了),注意每个QLabel都有自己的独立坐标

头文件:

#ifndef GLABEL_H
#define GLABEL_H

#include <QLabel>
#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>

class GLabel : public QLabel
{
    Q_OBJECT
public:
    enum PEOPLE{She,Me};
    Q_PROPERTY(QString str_context READ getStrContext WRITE setContext)
    explicit GLabel(QWidget *parent = 0,GLabel::PEOPLE f=GLabel::She);
    void setContext(const QString s){str_context = s;}
    QString getStrContext()const{return str_context;}

    QString str_context;
    QLabel *label_text;
    QLabel *label_people;
    PEOPLE people;
public:
    int m_width;
    int m_height;
    int parent_maxwidth;

    void init();
    void initUI(GLabel::PEOPLE f);
    void setSize(QString&);
    void setOurText(QString);
    void setHeadpic(QString);
    void  paintEvent(QPaintEvent *e);
    void mousePressEvent(QMouseEvent *e);
//signals:

//public slots:

};

#endif // GLABEL_H

.cpp文件:

#include "glabel.h"
#include <QPen>
#include <QDebug>

GLabel::GLabel(QWidget *parent,GLabel::PEOPLE f) :
    QLabel(parent)
{
      people = f;
      parent_maxwidth = ((QWidget*)this->parent())->width();
      init();
}

void GLabel::init()
{
    label_text  = new QLabel(this);
    label_text->setWordWrap(true);
    label_text->setGeometry(20+30,20,this->width()-40-30,this->height()-40);
    label_text->setStyleSheet("QLabel{background:rgb(173,216,67)}");//rgb(173,216,67)
    label_text->show();

    label_people = new QLabel(this);
    if(people == GLabel::She)
    {
        label_people->setGeometry(0,18,30,30);
        label_text->setStyleSheet("QLabel{background:rgb(173,216,67)}");
        label_people->setPixmap(QPixmap(":/image/she.png"));
    }
    else
    {
       label_people->setGeometry(this->width()-30,18,30,30);
       label_text->setStyleSheet("QLabel{background:rgb(240,240,240)}");
       label_people->setPixmap(QPixmap(":/image/he.png"));
    }
}

void GLabel::initUI(GLabel::PEOPLE f)
{

}

void GLabel::setSize(QString &paramstr)
{
//    int all_word_width  = this->fontMetrics().width(paramstr);
//    int one_line_height = this->fontMetrics().lineSpacing();
    int max_width = parent_maxwidth-50;//-label_people->width();
    int all_word_width;

    label_text->setWordWrap(false);
    label_text->setText(paramstr);
    label_text->adjustSize();
    all_word_width = label_text->width();
    label_text->setWordWrap(true);

    float line_count;
    int line;

    if(all_word_width > max_width)
    {
//        line_count = (float)all_word_width/max_width;
//        line = (int)line_count;
//        if(line < line_count)line=line+2;
        label_text->setMaximumWidth(max_width-20-30);
        label_text->setText(paramstr);
        label_text->adjustSize();
        qDebug()<<"the size of text"<<label_text->size()<<((QWidget*)this->parent())->width()-15;

        m_height = label_text->height()/*line*one_line_height*/;
        m_width  = parent_maxwidth-15;
       // qDebug()<<"linr_count"<<line_count<<one_line_height<<max_width<<all_word_width;
        this->resize(m_width,m_height+40);
    }
    else
    {
        label_text->setWordWrap(false);
        m_height = 66;

        label_text->setText(paramstr);
        label_text->adjustSize();
        m_width = label_text->width()+40+30;
        //label_text->setMinimumWidth(all_word_width);
        this->resize(m_width+10,m_height);
    }

    if(people == GLabel::She)
        label_text->setGeometry(20+40,20,this->width()-40-30,this->height()-40);
    else
        label_text->setGeometry(20,20,this->width()-40-30,this->height()-40);

    update();
}

void GLabel::setOurText(QString paramstr)
{
    setSize(paramstr);
    qDebug()<<"setSize"<<this->width()<<this->height();
    //label_text->setWordWrap(true);
   // label_text->setText(paramstr);
}

void GLabel::mousePressEvent(QMouseEvent *e)
{
    qDebug()<<"this pos is"<<e->pos();
}

void GLabel::setHeadpic(QString dir)
{
    label_people->setPixmap(QPixmap(dir));
}

void GLabel::paintEvent(QPaintEvent *e)
{
    //QPainter painter(this);
    if(people == GLabel::She)
    {
        QRectF rectangle(50.0, 10.0, this->width()-20-40, this->height()-20);

        QPainter painter(this);
        painter.setRenderHint( QPainter::Antialiasing, true );
        painter.setBrush(QBrush(QColor(173,216,67)));
        painter.drawRoundedRect(rectangle,10,10);

        QPointF points[3] = {
           QPointF(40, 33),
           QPointF(51, 28),
           QPointF(51, 38),
       };

       QPen pen;
       pen.setColor(QColor(173,216,67));
       painter.setPen(pen);
       painter.drawPolygon(points, 3);

       QPen pen1;
       pen.setColor(Qt::black);
       painter.setPen(pen1);
       painter.drawLine(points[0],points[1]);
       painter.drawLine(points[0],points[2]);
    }
    else if(people == GLabel::Me)
    {
        QRectF rectangle(10.0, 10.0, this->width()-20-40, this->height()-20);

        QPainter painter(this);
        painter.setRenderHint( QPainter::Antialiasing, true );
        painter.setBrush(QBrush(QColor(240,240,240)));
        painter.drawRoundedRect(rectangle,10,10);

        QPointF points[3] = {
           QPointF(this->width()-40, 33),
           QPointF(this->width()-51, 28),
           QPointF(this->width()-51,38),
       };

       QPen pen;
       pen.setColor(QColor(240,240,240));
       painter.setPen(pen);
       painter.drawPolygon(points, 3);

       QPen pen1;
       pen.setColor(Qt::black);
       painter.setPen(pen1);
       painter.drawLine(points[0],points[1]);
       painter.drawLine(points[0],points[2]);

      // label_text->setGeometry(20,20,this->width()-40-30,this->height()-40);
       label_people->setGeometry(this->width()-30,18,30,30);
       this->setGeometry(((QWidget*)this->parent())->width()-15-this->width(),this->y(),this->width(),this->height());
    }
   // qDebug()<<"this size of pix"<<label_people->size();
   // drawShe();
}

实际调用:

#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    label[0] = new GLabel(this,GLabel::Me);
    label[0]->setGeometry(05,10,100,66);
    label[0]->setOurText(tr("你好!"));
    label[0]->show();

    label[1] = new GLabel(this,GLabel::She);
    label[1]->setGeometry(5,label[0]->y()+label[0]->height(),100,66);
    label[1]->setOurText(tr("你好!"));
    label[1]->show();

    label[2] = new GLabel(this,GLabel::Me);
    label[2]->setGeometry(5,label[1]->y()+label[1]->height(),100,66);
    label[2]->setOurText(tr("我们好像在哪里见过?"));
    label[2]->show();

    label[3] = new GLabel(this,GLabel::She);
    label[3]->setGeometry(5,label[2]->y()+label[2]->height(),100,66);
    label[3]->setOurText(tr("?"));
    label[3]->show();

    label[4] = new GLabel(this,GLabel::Me);
    label[4]->setGeometry(5,label[3]->y()+label[3]->height(),100,66);
    label[4]->setOurText(tr("我们好像在哪见过你记得吗?好像那是一个秋天夕阳西下,你美得让我不敢和你说话,你经过我时风起浮动我的发"));
    label[4]->show();

    label[5] = new GLabel(this,GLabel::She);
    label[5]->setGeometry(5,label[4]->y()+label[4]->height(),100,66);
    label[5]->setOurText(tr("银杏还是会黄的,雪也会再次染白整个世界。安好"));
    label[5]->show();
}

Dialog::~Dialog()
{
    delete ui;
}

下载:http://download.csdn.net/download/yangkping123/8620775

时间: 2024-10-03 23:02:45

聊天气泡的绘制(圆角矩形+三角形+黑色边框,关键学会QPainter的draw函数就行了),注意每个QLabel都有自己的独立坐标的相关文章

【VB6 GDI+进阶】通过拼接圆弧和线绘制圆角矩形

GDI+中没有直接绘制圆角矩形的函数.本篇将详细介绍如何通过拼接圆弧和线绘制圆角矩形,结尾附封装好的函数,可以直接调用. 1.GdipDrawArcI(绘制圆弧) 函数声明如下: Public Declare Function GdipDrawArcI _ Lib "gdiplus" (ByVal graphics As Long, _ ByVal Pen As Long, _ ByVal X As Long, _ ByVal Y As Long, _ ByVal Width As L

canvas.drawRoundRect方法,绘制圆角矩形

public void drawRoundRect (RectF rect, float rx, float ry, Paint paint) Draw the specified round-rect using the specified paint. The roundrect will be filled or framed based on the Style in the paint. Parameters rect The rectangular bounds of the rou

章鱼哥出品—VB.NET 如何绘制圆角矩形,并适应窗体大小

Public Class Form1 '********************************************************************* '作者:章鱼哥,QQ:3107073263 群:309816713 '如有疑问或好的建议请联系我,大家一起进步 '********************************************************************* '绘制圆角矩形函数 Private Function GetRou

drawRoundRect方法:绘制圆角矩形

[功能说明]该方法用于在画布上绘制圆角矩形,通过指定RectF对象以及圆角半径来实现.该方法是绘制圆角矩形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆角矩形. [基本语法]public void drawRoundRect (RectF rect, float rx, float ry, Paint paint) 参数说明 rect:RectF对象. rx:x方向上的圆角半径. ry:y方向上的圆角半径. paint:绘制时所使用的画笔. [实例演示]下面通过代码来演示如何在画布上

详述Canvas(五)/绘制圆角矩形

Canvas并没有提供绘制圆角矩形的方法,但是通过观察,我们可以发现,其实我们可以将圆角矩形分为四段,可以通过使用arcTo来实现. 我们假设起点为x,y.绘制的矩形宽高为w,h.圆角的半径为r;所以将起点设置在(x+r,y)处,然后acrTo(x+w,y,x+w,y+h,r),对于终点,其实只要y值大于绿色点的都是可以的(这部分在绘制曲线部分已经详述).此处我们将终点设为(x+w,y+h);这就是第一段曲线.第一段曲线绘制完毕之后,画笔落在了下图绿色点的位置. 现在再看下第二段曲线: 因此我们

canva绘制圆角矩形

在做组态的时候,需要支持矩形圆角格式,但是因为canvas本身不带有圆角矩形,需要自行算出坐标进行绘制 方案一.统一圆角 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>canvas制作圆角矩形(包括填充矩形的功能)</title> </head> <body> <canvas id="myCanvas&

Android中绘制圆角矩形图片及任意形状图片

圆角矩形图片在苹果的产品中很流行,相比于普通的矩形,很多人都喜欢圆角矩形的图片,因为它避开了直角的生硬,带来更好的用户体验,下面是几个设计的例子: 下面在Android中实现将普通的矩形图片绘制成圆角矩形.首先看最终效果: 代码清单: package com.example.phototest; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android

CSS3绘制圆角矩形的简单示例

随着网络的发展,CSS 也在不断的完善,充分吸取多年来 Web 发展的需求,提出了很多新颖的 CSS 特性,例如很受欢迎的圆角矩形 border-radius 属性,但很可惜,此属性目前没有得到任何浏览器的支持. 对于一些浏览器,它们有其私有的圆角属性.如 FF 的 -moz-border-radius ,Safari 和 Chrome 的 -webkit-border-radius .效果见下图: FF 的圆角Safari 和 Chrome 的圆角(Safari 和 Chrome 使用的是同一

canvas 绘制圆角矩形

<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</title> <style type="text/css"> #canvas{border:1px solid #eee ; display:block; background-color: #B36666; margin: 20px auto; } </style