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版本,如果用戶需求量很大的話。
,