先展示一下 效果
以下是源码
// client
TestLabel* pTestLabel;
/* 显示一段文字 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("显示文字"));
pTestLabel->setGeometry(10, 10, 120, 120);
/* 显示一段文字,左对齐 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("左对齐"));
pTestLabel->setAlignment(Qt::AlignLeft);
pTestLabel->setGeometry(150, 10, 120, 120);
/* 显示一段文字,右对齐 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("右对齐"));
pTestLabel->setAlignment(Qt::AlignRight);
pTestLabel->setGeometry(290, 10, 120, 120);
/* 显示一段文字,上对齐 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("上对齐"));
pTestLabel->setAlignment(Qt::AlignTop);
pTestLabel->setGeometry(430, 10, 120, 120);
/* 显示一段文字,下对齐 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("下对齐"));
pTestLabel->setAlignment(Qt::AlignBottom);
pTestLabel->setGeometry(570, 10, 120, 120);
/* 显示一段文字,设置文字颜色 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("红色文字"));
pTestLabel->setGeometry(10, 150, 120, 120);
pTestLabel->setTextColor(Qt::red);
/* 显示一段文字,设置背景颜色 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("win红色背景"));
pTestLabel->setGeometry(150, 150, 120, 120);
pTestLabel->setWidgetBackColor(Qt::red);
/* 显示一段文字,设置背景颜色 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("label绿色背景(取消)"));
pTestLabel->setGeometry(290, 150, 120, 120);
pTestLabel->setLabelBackColor(Qt::green);
/* 显示一段文字,设置文字大小 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("20大小字体"));
pTestLabel->setGeometry(430, 150, 120, 120);
pTestLabel->setFontSize(20);
/* 显示一段文字,显示控件阴影 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("控件阴影"));
pTestLabel->setGeometry(570, 150, 120, 120);
pTestLabel->setWidgetEffect(Qt::blue);
/* 控件倒圆角 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("控件倒圆角"));
pTestLabel->setGeometry(10, 290, 120, 120);
pTestLabel->setWidgetRound(30);
/* 控件倒圆角+阴影 */
pTestLabel = new TestLabel(this);
pTestLabel->setText(tr("控件倒圆角+阴影"));
pTestLabel->setGeometry(150, 290, 120, 120);
pTestLabel->setWidgetRound(30);
pTestLabel->setWidgetEffect(Qt::gray);
// h文件
#ifndef TESTLABEL_H
#define TESTLABEL_H
#include <QWidget>
#include <QPalette>
#include <QFont>
#include <QGraphicsBlurEffect>
namespace Ui {
class TestLabel;
}
class TestLabel : public QWidget
{
Q_OBJECT
public:
explicit TestLabel(QWidget *parent = 0);
~TestLabel();
void setText(const QString& text);
void setAlignment(Qt::Alignment align);
void setTextColor(QColor color);
void setWidgetBackColor(QColor color);
void setWidgetRound(int r);
void setLabelBackColor(QColor color);
void setFontSize(int size);
void setWidgetEffect(QColor color);
protected:
void paintEvent(QPaintEvent* e);
private:
Ui::TestLabel *ui;
QPalette _palette; // 文字颜色
QFont _font;
QGraphicsDropShadowEffect _effect;
int _round; // 圆角半经
QColor _backColor; // 控件背景颜色
};
#endif // TESTLABEL_H
/*
cpp文件
*/
#include "testlabel.h"
#include "ui_testlabel.h"
#include <QGraphicsBlurEffect>
#include <QPainter>
#include <QVBoxLayout>
TestLabel::TestLabel(QWidget *parent) :
_round(0),
_backColor(217, 217, 217),
QWidget(parent),
ui(new Ui::TestLabel)
{
ui->setupUi(this);
}
TestLabel::~TestLabel()
{
delete ui;
}
void TestLabel::setText(const QString &text)
{
ui->label->setText(text);
}
void TestLabel::setAlignment(Qt::Alignment align)
{
ui->label->setAlignment(align);
}
void TestLabel::setTextColor(QColor color)
{
#if 1
QPalette p = ui->label->palette();
p.setColor(QPalette::WindowText, color);
ui->label->setPalette(p);
#endif
}
void TestLabel::setWidgetBackColor(QColor color)
{
#if 0
setAutoFillBackground(true);
QPalette p = palette();
p.setColor(QPalette::Window, color);
setPalette(p);
#endif
_backColor = color;
update();
}
void TestLabel::setWidgetRound(int r)
{
_round = r;
update();
}
// 这个方法取消
void TestLabel::setLabelBackColor(QColor color)
{
// 这种方式的涂色没有stylesheet方式优先级高
// 但是比paintEvent优先级要高
ui->label->setAutoFillBackground(true); //这里不加这个自动填充的话,背景色不会显示
QPalette p = ui->label->palette();
p.setColor(QPalette::Window, color);
ui->label->setPalette(p);
}
void TestLabel::setFontSize(int size)
{
_font.setPointSize(size);
ui->label->setFont(_font);
}
void TestLabel::setWidgetEffect(QColor color)
{
_effect.setOffset(-6, -6);
_effect.setColor(color);
_effect.setBlurRadius(30);
//ui->label->setGraphicsEffect(&_effect);
this->setGraphicsEffect(&_effect);
ui->verticalLayout->setMargin(20);
}
void TestLabel::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
p.setBrush(_backColor);
QRect rect = this->rect();
rect.setWidth(rect.width()-1);
rect.setHeight(rect.height()-1);
p.drawRoundedRect(rect, _round, _round);
}