ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义QLabel 控件

2022-02-24 21:32:56  阅读:183  来源: 互联网

标签:控件 Qt 自定义 contex width pen QString painter QLabel


话不多说,直接效果,这一贯是自己的风格,再多的话语,不如直接干货。

上图为最终效果:继承QLabel 类,进行重绘,适配各种分辨率。本实例只是抛砖引玉,烦请大神让行。

#ifndef PICTURELABELWIDGET_H
#define PICTURELABELWIDGET_H


#include <QWidget>
#include <QPainter>
#include <QDebug>
#include <QLabel>

//图标 显示

class PictureLabelWidget : public QLabel
{
    Q_OBJECT
public:
    explicit PictureLabelWidget(QWidget *parent = nullptr);

signals:

public slots:

public:
   void picture_label_init(QString color,QString name,QString contex,QString ico);//color:主背景色 name:显示的标题 contex:设置内容  ico:图标路径
   void label_contex(QString contex);//显示内容设置
private:

    QString  nameText; //题目
    QString  contexText; //内容

    QString  backLightColor; //背景颜色
    QString  icoPath; //显示图标路径
protected:
    virtual void paintEvent(QPaintEvent *event);




};

#endif // PICTURELABELWIDGET_H

#include "picturelabelwidget.h"

PictureLabelWidget::PictureLabelWidget(QWidget *parent) : QLabel(parent)
{

    this->setAttribute(Qt::WA_DeleteOnClose,true); //析构后完成内存的释放
}


void PictureLabelWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QPen pen;

    //pen.setColor(QColor(92,114,137));
    pen.setWidth(1);
    pen.setStyle(Qt::NoPen);

    painter.setPen(pen); //设置画笔形式
    painter.setBrush(QBrush(QColor(backLightColor),Qt::SolidPattern)); //设置画刷形式
    painter.drawRect(0,0,this->width(),this->height());

    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(0, 0, 0));

    QFont font;
    font.setPointSize(12);
    font.setBold(false);//粗体
    painter.setFont(font);
    painter.setPen(pen);
    painter.drawText(this->width()*0.05,0,this->width(),this->height(),Qt::AlignVCenter |Qt::AlignLeft,nameText);
    font.setBold(true);//粗体
    font.setPointSize(14);
    painter.setFont(font);
    painter.setPen(pen);
    painter.drawText(this->width()*0.5,0,this->width(),this->height(),Qt::AlignVCenter |Qt::AlignLeft,contexText);


    QPixmap pix;
    pix.load(icoPath);
    //qDebugInfo() <<this->height()<<pix.height() <<hScale;
    pix = pix.scaled(this->size()*0.8,Qt::KeepAspectRatio, Qt::SmoothTransformation);

    painter.drawPixmap(this->width()-pix.width()-this->width()*0.05,this->height()*0.15,pix.width(),pix.height(),pix);

}
void PictureLabelWidget::label_contex(QString contex)
{
    contexText = contex;
    this->update();
}
void PictureLabelWidget::picture_label_init(QString color,QString name,QString contex,QString ico)
{
    backLightColor = color;
    nameText = name;
    contexText = contex;
    icoPath = ico;
    this->update();
}

 

父类应用如下:

 

 

标签:控件,Qt,自定义,contex,width,pen,QString,painter,QLabel
来源: https://blog.csdn.net/qq84395064/article/details/123121341

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有