天天看点

Qt 自定义按钮前言一、新建控件?二、码代码总结

文章目录

  • 前言
  • 一、新建控件?
  • 二、码代码
    • 1.重载
    • 2.示例控件
    • 3、然后编译Release模式
  • 总结

前言

就自定义控件,估计有的朋友工作需要,而我自己就是闲的……,主要就是自己把控件画(paintEvent)出来

一、新建控件?

Qt Creator打开,看下自己Creator的编译套件,这个很重要,控件的编译套件要 一致 才最终会在自定义控件里面看到。(不一致看这篇 Qt维护)

Qt 自定义按钮前言一、新建控件?二、码代码总结

新建选这个,一步一步往下走

Qt 自定义按钮前言一、新建控件?二、码代码总结

最主要是这个选择编译套件(一定要一致!!!)

Qt 自定义按钮前言一、新建控件?二、码代码总结

往下走

Qt 自定义按钮前言一、新建控件?二、码代码总结

一步一步往下走就结束了……

二、码代码

1.重载

可以看文档检索下有啥要重载的

Qt 自定义按钮前言一、新建控件?二、码代码总结

比如按钮

QAbstractButton

虽然写了一大堆啊,最主要的就是

paintEvent

,控件的绘制。额外的在

QWidget

里面的

sizeHint() minimumSizeHint()

控制控件初始大小一般也要

Qt 自定义按钮前言一、新建控件?二、码代码总结

根据自己需求重载相应的虚函数,或者重写对应的函数

2.示例控件

(自己觉得原本的按钮丑又自己画了一个,最后发现还不如原本的):

Qt 自定义按钮前言一、新建控件?二、码代码总结

.h

文件

#ifndef PLATANIMBUTTON_H
#define PLATANIMBUTTON_H

#include <QAbstractButton>

class PlatAnimButton : public QAbstractButton
{
    Q_OBJECT
	//会在右边面板出现属性可以直接调
    Q_PROPERTY(qreal roundRadius READ getRoundRadius WRITE setRoundRadius);
    Q_PROPERTY(QColor btnColor READ getBtnColor WRITE setBtnColor);
    Q_PROPERTY(int textSpace READ getTextSpace WRITE setTextSpace);
    Q_PROPERTY(QFont font READ getFont WRITE setFont);

public:
    PlatAnimButton(QWidget *parent = 0);
    ~PlatAnimButton();

    void paintEvent(QPaintEvent *e) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    void drawRoundedRect(QPainter *);
    void onTimerCallBack();
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

    qreal getRoundRadius() {return roundRadius;}
    void setRoundRadius(qreal r);

    QColor getBtnColor(){return btnColor;}
    void setBtnColor(QColor c){btnColor = c;}

    int getTextSpace(){return textSpace;}
    void setTextSpace(int ts);

    QFont getFont(){return this->font;}
    void setFont(QFont f);

private:
    double maxRadius = 255;
    double minRadius = 1;
    double radius = 1;
    qreal roundRadius = 8; //圆角
    QColor btnColor = QColor(225, 81, 81, 255);
    int textSpace = 2;
    QFont font = QFont("SimHei", 18);

    QTimer *timer = nullptr;
    bool isExpand = false;
};

#endif // PLATANIMBUTTON_H

           

.cpp

文件,放主要的函数……

//重中之重
void PlatAnimButton::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    this->drawRoundedRect(&painter);
}

void PlatAnimButton::drawRoundedRect(QPainter *painter)
{
    painter->setPen(Qt::PenStyle::NoPen);
    QBrush brush(Qt::BrushStyle::SolidPattern);
    brush.setColor(QColor(15, 15, 15, 255 * 0.1));
    painter->setBrush(brush);
    painter->drawRoundedRect(0, 2, width(), height() - 2, this->getRoundRadius(), this->getRoundRadius());
    // 设置渐变色
    QRadialGradient radial(width() * 0.5, height(), this->radius, width() * 0.5, 0);
    if(this->radius > 1)
    {
        radial.setColorAt(0, this->btnColor);
    }
    radial.setColorAt(1, Qt::white);

    // 设置显示模式
    radial.setSpread(QGradient::PadSpread);
    painter->setBrush(radial);
    painter->drawRoundedRect(2, 0, width() - 4, height() - 2, this->getRoundRadius(), this->getRoundRadius());
    QPen pen(Qt::PenStyle::SolidLine);
    pen.setJoinStyle(Qt::PenJoinStyle::RoundJoin);
    pen.setColor(QColor(0, 0, 0, 125));
    painter->setPen(pen);

    font.setLetterSpacing(QFont::SpacingType::AbsoluteSpacing, this->getTextSpace());
    painter->setFont(font);
    painter->drawText(2, 0, width() - 4, height() - 2, Qt::AlignCenter, this->text());
}

QSize PlatAnimButton::sizeHint() const
{
    QSize size(172, 53.0);
    return size;
}

QSize PlatAnimButton::minimumSizeHint() const
{
    QSize size(172 / 2, 53.0 / 2);
    return size;
}
           

3、然后编译Release模式

找到对应的文件生成的

Qt 自定义按钮前言一、新建控件?二、码代码总结

复制到Qt安装目录下的路径目录下,重启就可以在控件栏看到了

Qt 自定义按钮前言一、新建控件?二、码代码总结

应用的时候,还需要吧项目的这三个复制到对应的项目下,

添加现有项

Qt 自定义按钮前言一、新建控件?二、码代码总结

结果如下:(运行就成功了,不出Bug的话……)

Qt 自定义按钮前言一、新建控件?二、码代码总结

总结

就没了……代码贴这边 代码,不行就在我的资源里面找,虽然不一定有人要看……

继续阅读