這個控件一開始打算用樣式表來實現,通過初步的探索,後面發現仍是不夠智能以及不能徹底知足需求,好比要在此控件設置多個角標,這個用QSS就很難實現,後面才慢慢研究用QPainter來繪製,我記得當時接到這個定製控件任務的時候是2016年,那時候對QPainter的使用還不是很熟悉,也就是今後控件開始,逐步研究QPainter的繪製,把全部的內置函數都使用一遍,最終用的愈來愈熟悉,使得後來到了心中有座標,萬物皆painter的境界,可能就像武林中所說的打通了任督二脈吧。 本控件除了能夠設置常規的圓角角度,邊框寬度,邊框顏色,正常顏色,按下顏色之外,還能夠設置各個角標和正文文字內容/字體/對齊方式/顏色,同時還要提供三種顏色展現模式,鬆開按下兩種顏色,按下鬆開顏色上下交替,按下鬆開顏色漸變交替。QLinearGradient是個好東西,各類顏色交替效果全靠它來實現。linux
#ifndef COLORBUTTON_H #define COLORBUTTON_H /** * 多樣式超級按鈕控件 做者:feiyangqingyun(QQ:517216493) 2017-9-24 * 1:可設置圓角角度,邊框寬度 * 2:可設置角標和正文文字內容/字體/對齊方式/顏色 * 3:可設置邊框顏色,正常顏色,按下顏色 * 4:可設置背景圖片 * 5:可設置按鈕顏色模式 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT ColorButton : public QWidget #else class ColorButton : public QWidget #endif { Q_OBJECT Q_ENUMS(ColorMode) Q_ENUMS(TextAlign) Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius) Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth) Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor) Q_PROPERTY(bool showSuperText READ getShowSuperText WRITE setShowSuperText) Q_PROPERTY(QString superText READ getSuperText WRITE setSuperText) Q_PROPERTY(QFont superTextFont READ getSuperTextFont WRITE setSuperTextFont) Q_PROPERTY(TextAlign superTextAlign READ getSuperTextAlign WRITE setSuperTextAlign) Q_PROPERTY(QColor superTextColor READ getSuperTextColor WRITE setSuperTextColor) Q_PROPERTY(QString text READ getText WRITE setText) Q_PROPERTY(QFont textFont READ getTextFont WRITE setTextFont) Q_PROPERTY(TextAlign textAlign READ getTextAlign WRITE setTextAlign) Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) Q_PROPERTY(QColor normalColor READ getNormalColor WRITE setNormalColor) Q_PROPERTY(QColor pressedColor READ getPressedColor WRITE setPressedColor) Q_PROPERTY(bool canMove READ getCanMove WRITE setCanMove) Q_PROPERTY(QPixmap bgImage READ getBgImage WRITE setBgImage) Q_PROPERTY(ColorMode colorMode READ getColorMode WRITE setColorMode) public: enum ColorMode { ColorMode_Normal = 0, //鬆開按下兩種顏色 ColorMode_Replace = 1, //按下鬆開顏色上下交替 ColorMode_Shade = 2 //按下鬆開顏色漸變交替 }; enum TextAlign { TextAlign_Top_Left = 0, TextAlign_Top_Center = 1, TextAlign_Top_Right = 2, TextAlign_Center_Left = 3, TextAlign_Center_Center = 4, TextAlign_Center_Right = 5, TextAlign_Bottom_Left = 6, TextAlign_Bottom_Center = 7, TextAlign_Bottom_Right = 8 }; explicit ColorButton(QWidget *parent = 0); protected: void mousePressEvent(QMouseEvent *); protected: bool eventFilter(QObject *watched, QEvent *event); void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawText(QPainter *painter); private: int borderRadius; //圓角半徑 int borderWidth; //邊框寬度 QColor borderColor; //邊框顏色 bool showSuperText; //顯示角標 QString superText; //角標文字 QFont superTextFont; //角標文字字體 TextAlign superTextAlign; //角標文字對齊方式 QColor superTextColor; //角標文字顏色 QString text; //文字 QFont textFont; //文字字體 TextAlign textAlign; //文字對齊方式 QColor textColor; //文字顏色 QColor normalColor; //正常顏色 QColor pressedColor; //按下顏色 bool canMove; //是否能移動 QPixmap bgImage; //背景圖片 ColorMode colorMode; //背景色模式 bool isPressed; //是否按下 public: int getBorderRadius() const; int getBorderWidth() const; QColor getBorderColor() const; bool getShowSuperText() const; QString getSuperText() const; QFont getSuperTextFont() const; TextAlign getSuperTextAlign()const; QColor getSuperTextColor() const; QString getText() const; QFont getTextFont() const; TextAlign getTextAlign() const; QColor getTextColor() const; QColor getNormalColor() const; QColor getPressedColor() const; bool getCanMove() const; QPixmap getBgImage() const; ColorMode getColorMode() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設置邊框圓角角度 void setBorderRadius(int borderRadius); //設置邊框寬度 void setBorderWidth(int borderWidth); //設置邊框顏色 void setBorderColor(const QColor &borderColor); //設置是否顯示角標 void setShowSuperText(bool showSuperText); //設置角標文字 void setSuperText(const QString &superText); //設置角標文字字體 void setSuperTextFont(const QFont &superTextFont); //設置角標文字對齊方式 void setSuperTextAlign(const TextAlign &superTextAlign); //設置角標文字顏色 void setSuperTextColor(const QColor &superTextColor); //設置文字 void setText(const QString &text); //設置文字字體 void setTextFont(const QFont &textFont); //設置文字對齊方式 void setTextAlign(const TextAlign &textAlign); //設置文字顏色 void setTextColor(const QColor &textColor); //設置正常顏色 void setNormalColor(const QColor &normalColor); //設置按下顏色 void setPressedColor(const QColor &pressedColor); //設置是否能夠移動 void setCanMove(bool canMove); //設置背景圖片 void setBgImage(const QPixmap &bgImage); //設置顏色模式 void setColorMode(const ColorMode &colorMode); Q_SIGNALS: void clicked(); }; #endif // WKBUTTON_H
bool ColorButton::eventFilter(QObject *watched, QEvent *event) { if (!isEnabled()) { return QWidget::eventFilter(watched, event); } static QPoint lastPoint; if (event->type() == QEvent::MouseButtonPress) { QMouseEvent *e = static_cast<QMouseEvent *>(event); if (this->rect().contains(e->pos()) && (e->button() == Qt::LeftButton)) { lastPoint = e->pos(); isPressed = true; update(); } } else if (event->type() == QEvent::MouseMove && isPressed && canMove) { QMouseEvent *e = static_cast<QMouseEvent *>(event); int dx = e->pos().x() - lastPoint.x(); int dy = e->pos().y() - lastPoint.y(); this->move(this->x() + dx, this->y() + dy); return true; } else if (event->type() == QEvent::MouseButtonRelease && isPressed) { isPressed = false; update(); } return QWidget::eventFilter(watched, event); } void ColorButton::paintEvent(QPaintEvent *) { //繪製準備工做,啓用反鋸齒 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製背景 drawBg(&painter); //繪製文字 drawText(&painter); } void ColorButton::drawBg(QPainter *painter) { painter->save(); //設置邊框顏色及寬度 QPen pen; pen.setColor(borderColor); pen.setWidthF(borderWidth); painter->setPen(pen); //繪製區域要減去邊框寬度 QRect rect; rect.setX(borderWidth); rect.setY(borderWidth); rect.setWidth(width() - borderWidth * 2); rect.setHeight(height() - borderWidth * 2); //若是背景圖片存在則顯示背景圖片,不然顯示背景色 if (!bgImage.isNull()) { //等比例縮放繪製 QPixmap img = bgImage.scaled(rect.width(), rect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); painter->drawPixmap((this->rect().width() - img.width()) / 2, (this->rect().height() - img.height()) / 2, img); } else { if (colorMode == ColorMode_Normal) { if (isPressed) { painter->setBrush(QBrush(pressedColor)); } else { painter->setBrush(QBrush(normalColor)); } } else if (colorMode == ColorMode_Replace) { QLinearGradient gradient(QPoint(0, 0), QPoint(0, height())); if (isPressed) { gradient.setColorAt(0.0, pressedColor); gradient.setColorAt(0.49, pressedColor); gradient.setColorAt(0.50, normalColor); gradient.setColorAt(1.0, normalColor); } else { gradient.setColorAt(0.0, normalColor); gradient.setColorAt(0.49, normalColor); gradient.setColorAt(0.50, pressedColor); gradient.setColorAt(1.0, pressedColor); } painter->setBrush(gradient); } else if (colorMode == ColorMode_Shade) { QLinearGradient gradient(QPoint(0, 0), QPoint(0, height())); if (isPressed) { gradient.setColorAt(0.0, pressedColor); gradient.setColorAt(1.0, normalColor); } else { gradient.setColorAt(0.0, normalColor); gradient.setColorAt(1.0, pressedColor); } painter->setBrush(gradient); } painter->drawRoundedRect(rect, borderRadius, borderRadius); } painter->restore(); } void ColorButton::drawText(QPainter *painter) { if (!bgImage.isNull()) { return; } painter->save(); //若是要顯示角標,則從新計算顯示文字的區域 if (showSuperText) { int offset = 3; QRect rect; rect.setX(borderWidth * offset); rect.setY(borderWidth); rect.setWidth(width() - borderWidth * offset * 2); rect.setHeight(height() - borderWidth * 2); Qt::Alignment alignment = Qt::AlignCenter; if (superTextAlign == TextAlign_Top_Left) { alignment = Qt::AlignTop | Qt::AlignLeft; } else if (superTextAlign == TextAlign_Top_Center) { alignment = Qt::AlignTop | Qt::AlignHCenter; } else if (superTextAlign == TextAlign_Top_Right) { alignment = Qt::AlignTop | Qt::AlignRight; } else if (superTextAlign == TextAlign_Center_Left) { alignment = Qt::AlignLeft | Qt::AlignVCenter; } else if (superTextAlign == TextAlign_Center_Center) { alignment = Qt::AlignHCenter | Qt::AlignVCenter; } else if (superTextAlign == TextAlign_Center_Right) { alignment = Qt::AlignRight | Qt::AlignVCenter; } else if (superTextAlign == TextAlign_Bottom_Left) { alignment = Qt::AlignBottom | Qt::AlignLeft; } else if (superTextAlign == TextAlign_Bottom_Center) { alignment = Qt::AlignBottom | Qt::AlignHCenter; } else if (superTextAlign == TextAlign_Bottom_Right) { alignment = Qt::AlignBottom | Qt::AlignRight; } //繪製角標 painter->setPen(superTextColor); painter->setFont(superTextFont); painter->drawText(rect, alignment, superText); } int offset = 5; QRect rect; rect.setX(borderWidth * offset); rect.setY(borderWidth); rect.setWidth(width() - borderWidth * offset * 2); rect.setHeight(height() - borderWidth * 2); Qt::Alignment alignment = Qt::AlignCenter; if (textAlign == TextAlign_Top_Left) { alignment = Qt::AlignTop | Qt::AlignLeft; } else if (textAlign == TextAlign_Top_Center) { alignment = Qt::AlignTop | Qt::AlignHCenter; } else if (textAlign == TextAlign_Top_Right) { alignment = Qt::AlignTop | Qt::AlignRight; } else if (textAlign == TextAlign_Center_Left) { alignment = Qt::AlignLeft | Qt::AlignVCenter; } else if (textAlign == TextAlign_Center_Center) { alignment = Qt::AlignHCenter | Qt::AlignVCenter; } else if (textAlign == TextAlign_Center_Right) { alignment = Qt::AlignRight | Qt::AlignVCenter; } else if (textAlign == TextAlign_Bottom_Left) { alignment = Qt::AlignBottom | Qt::AlignLeft; } else if (textAlign == TextAlign_Bottom_Center) { alignment = Qt::AlignBottom | Qt::AlignHCenter; } else if (textAlign == TextAlign_Bottom_Right) { alignment = Qt::AlignBottom | Qt::AlignRight; } painter->setPen(textColor); painter->setFont(textFont); painter->drawText(rect, alignment, text); painter->restore(); }