文章目錄
- 前言
- 一、建立控件?
- 二、碼代碼
-
- 1.重載
- 2.示例控件
- 3、然後編譯Release模式
- 總結
前言
就自定義控件,估計有的朋友工作需要,而我自己就是閑的……,主要就是自己把控件畫(paintEvent)出來
一、建立控件?
Qt Creator打開,看下自己Creator的編譯套件,這個很重要,控件的編譯套件要 一緻 才最終會在自定義控件裡面看到。(不一緻看這篇 Qt維護)

建立選這個,一步一步往下走
最主要是這個選擇編譯套件(一定要一緻!!!)
往下走
一步一步往下走就結束了……
二、碼代碼
1.重載
可以看文檔檢索下有啥要重載的
比如按鈕
QAbstractButton
雖然寫了一大堆啊,最主要的就是
paintEvent
,控件的繪制。額外的在
QWidget
裡面的
sizeHint() minimumSizeHint()
控制控件初始大小一般也要
根據自己需求重載相應的虛函數,或者重寫對應的函數
2.示例控件
(自己覺得原本的按鈕醜又自己畫了一個,最後發現還不如原本的):
.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安裝目錄下的路徑目錄下,重新開機就可以在控件欄看到了
應用的時候,還需要吧項目的這三個複制到對應的項目下,
添加現有項
結果如下:(運作就成功了,不出Bug的話……)
總結
就沒了……代碼貼這邊 代碼,不行就在我的資源裡面找,雖然不一定有人要看……