新四季網

qt控制項使用說明(Qt編寫自定義控制項30-顏色多態按鈕)

2023-05-28 14:16:42 1

一、前言

這個控制項一開始打算用樣式表來實現,經過初步的探索,後面發現還是不夠智能以及不能完全滿足需求,比如要在此控制項設置多個角標,這個用QSS就很難實現,後面才慢慢研究用QPainter來繪製,我記得當時接到這個定製控制項任務的時候是2016年,那時候對QPainter的使用還不是很熟悉,也就是從此控制項開始,逐步研究QPainter的繪製,把所有的內置函數都使用一遍,最終用的越來越熟悉,使得後來到了心中有坐標,萬物皆painter的境界,可能就像武林中所說的打通了任督二脈吧。

本控制項除了可以設置常規的圓角角度,邊框寬度,邊框顏色,正常顏色,按下顏色以外,還可以設置各個角標和正文文字內容/字體/對齊方式/顏色,同時還要提供三種顏色展示模式,鬆開按下兩種顏色,按下鬆開顏色上下交替,按下鬆開顏色漸變交替。QLinearGradient是個好東西,各種顏色交替效果全靠它來實現。

二、實現的功能

* 1:可設置圓角角度,邊框寬度

* 2:可設置角標和正文文字內容/字體/對齊方式/顏色

* 3:可設置邊框顏色,正常顏色,按下顏色

* 4:可設置背景圖片

* 5:可設置按鈕顏色模式

三、效果圖

四、核心代碼

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(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(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;}

六、控制項介紹

1. 超過149個精美控制項,涵蓋了各種儀錶盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農曆等。遠超qwt集成的控制項數量。

2. 每個類都可以獨立成一個單獨的控制項,零耦合,每個控制項一個頭文件和一個實現文件,不依賴其他文件,方便單個控制項以源碼形式集成到項目中,較少代碼量。qwt的控制項類環環相扣,高度耦合,想要使用其中一個控制項,必須包含所有的代碼。

3. 全部純Qt編寫,QWidget QPainter繪製,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,支持任意作業系統比如windows linux mac 嵌入式linux等,不亂碼,可直接集成到Qt Creator中,和自帶的控制項一樣使用,大部分效果只要設置幾個屬性即可,極為方便。

4. 每個控制項都有一個對應的單獨的包含該控制項源碼的demo,方便參考使用。同時還提供一個所有控制項使用的集成的DEMO。

5. 每個控制項的原始碼都有詳細中文注釋,都按照統一設計規範編寫,方便學習自定義控制項的編寫。

6. 每個控制項默認配色和demo對應的配色都非常精美。

7. 超過130個可見控制項,6個不可見控制項。

8. 部分控制項提供多種樣式風格選擇,多種指示器樣式選擇。

9. 所有控制項自適應窗體拉伸變化。

10. 集成自定義控制項屬性設計器,支持拖曳設計,所見即所得,支持導入導出xml格式。

11. 自帶activex控制項demo,所有控制項可以直接運行在ie瀏覽器中。

12. 集成fontawesome圖形字體 阿里巴巴iconfont收藏的幾百個圖形字體,享受圖形字體帶來的樂趣。

13. 所有控制項最後生成一個dll動態庫文件,可以直接集成到qtcreator中拖曳設計使用。

14. 目前已經有qml版本,後期會考慮出pyqt版本,如果用戶需求量很大的話。

,
同类文章
葬禮的夢想

葬禮的夢想

夢見葬禮,我得到了這個夢想,五個要素的五個要素,水火只好,主要名字在外面,職業生涯良好,一切都應該對待他人治療誠意,由於小,吉利的冬天夢想,秋天的夢是不吉利的
找到手機是什麼意思?

找到手機是什麼意思?

找到手機是什麼意思?五次選舉的五個要素是兩名士兵的跡象。與他溝通很好。這是非常財富,它擅長運作,職業是仙人的標誌。單身男人有這個夢想,主要生活可以有人幫忙
我不怎麼想?

我不怎麼想?

我做了什麼意味著看到米飯烹飪?我得到了這個夢想,五線的主要土壤,但是Tu Ke水是錢的跡象,職業生涯更加真誠。他真誠地誠實。這是豐富的,這是夏瑞的巨星
夢想你的意思是什麼?

夢想你的意思是什麼?

你是什​​麼意思夢想的夢想?夢想,主要木材的五個要素,水的跡象,主營業務,主營業務,案子應該抓住魅力,不能疏忽,春天夢想的吉利夢想夏天的夢想不幸。詢問學者夢想
拯救夢想

拯救夢想

拯救夢想什麼意思?你夢想著拯救人嗎?拯救人們的夢想有一個現實,也有夢想的主觀想像力,請參閱週宮官方網站拯救人民夢想的詳細解釋。夢想著敵人被拯救出來
2022愛方向和生日是在[質量個性]中

2022愛方向和生日是在[質量個性]中

[救生員]有人說,在出生88天之前,胎兒已經知道哪天的出生,如何有優質的個性,將走在什麼樣的愛情之旅,將與生活生活有什么生活。今天
夢想切割剪裁

夢想切割剪裁

夢想切割剪裁什麼意思?你夢想切你的手是好的嗎?夢想切割手工切割手有一個真正的影響和反應,也有夢想的主觀想像力。請參閱官方網站夢想的細節,以削減手
夢想著親人死了

夢想著親人死了

夢想著親人死了什麼意思?你夢想夢想你的親人死嗎?夢想有一個現實的影響和反應,還有夢想的主觀想像力,請參閱夢想世界夢想死亡的親屬的詳細解釋
夢想搶劫

夢想搶劫

夢想搶劫什麼意思?你夢想搶劫嗎?夢想著搶劫有一個現實的影響和反應,也有夢想的主觀想像力,請參閱週恭吉夢官方網站的詳細解釋。夢想搶劫
夢想缺乏缺乏紊亂

夢想缺乏缺乏紊亂

夢想缺乏缺乏紊亂什麼意思?你夢想缺乏異常藥物嗎?夢想缺乏現實世界的影響和現實,還有夢想的主觀想像,請看官方網站的夢想組織缺乏異常藥物。我覺得有些東西缺失了