QT風格(QStyle):繪製一個自定義QComboBox
繪製QComboBox即把QComboBoxd組成元素繪製出來。
元素如下:
這三個都是子控制元件,可互動的,圖示元素預設是PE_IndicatorArrowDown,一個下箭頭。
繪製設計圖:
過程實在沒啥好說的,繪製套路和前面都一樣,無非是確定好子控制元件/子元素位置,然後在相應位置繪製需要繪製的內容。
完整程式碼:
.h檔案:
#ifndef MYCOMBOBOXSTYLE_H
#define MYCOMBOBOXSTYLE_H
#include <QProxyStyle>
class myComboBoxStyle : public QProxyStyle
{
public:
myComboBoxStyle();
void drawComplexControl(ComplexControl which,const QStyleOptionComplex *option,QPainter *painter,const QWidget *widget = nullptr) const override;
QRect subControlRect(ComplexControl whichControl,const QStyleOptionComplex *option,SubControl whichSubControl,const QWidget *widget = nullptr) const override;
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
};
#endif // MYCOMBOBOXSTYLE_H
.cpp檔案:
#include "mycomboboxstyle.h"
#include <QPainter>
#include <QStyleOptionComplex>
#include <QDebug>
myComboBoxStyle::myComboBoxStyle()
{
}
void myComboBoxStyle::drawComplexControl(ComplexControl which,const QStyleOptionComplex *option,QPainter *painter,const QWidget *widget) const
{
if (which == CC_ComboBox)
{
if(const QStyleOptionComboBox * cbOption = qstyleoption_cast<const QStyleOptionComboBox *>(option))
{
painter->save();
QRect rect = subControlRect(CC_ComboBox, option,SC_ComboBoxFrame).adjusted(+1, +1, -1, -1);
painter->setBrush(QColor("#128bf1"));
painter->drawRect(rect);
rect = subControlRect(CC_ComboBox, option,SC_ComboBoxEditField).adjusted(+2, +2, -2, -2);
QLinearGradient gradient(rect.topLeft(),rect.bottomRight());
gradient.setColorAt(0.0, QColor("#fa709a"));
gradient.setColorAt(1.0, QColor("#fee140"));
painter->setPen(Qt::NoPen);
painter->setBrush(gradient);
painter->drawRect(rect);
rect = subControlRect(CC_ComboBox, option,SC_ComboBoxArrow).adjusted(+1, +1, -1, -1);
painter->setPen(Qt::transparent);
painter->translate(option->rect.x(),option->rect.y());
QLinearGradient gradient2(rect.topLeft(),rect.bottomRight());
gradient2.setColorAt(0.0, QColor("#84fab0"));
gradient2.setColorAt(1.0, QColor("#8fd3f4"));
painter->setBrush(gradient2);
painter->drawRect(rect);
painter->restore();
QStyleOption arrowOpt(*cbOption);
arrowOpt.rect = rect.adjusted(+rect.width() * 0.3, +rect.height() * 0.3,
-rect.width() * 0.3, -rect.height() * 0.3);
drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, painter);
if(option->activeSubControls == SC_ComboBoxEditField ||
option->activeSubControls == SC_ComboBoxArrow)
{
painter->save();
QColor slightlyOpaqueBlack(0, 0, 0, 63);
painter->setBrush(slightlyOpaqueBlack);
painter->drawRect(widget->rect());
painter->restore();
}
}
}
else
{
return QProxyStyle::drawComplexControl(which, option, painter,widget);
}
}
void myComboBoxStyle::drawPrimitive(PrimitiveElement which, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
switch (which)
{
case PE_IndicatorArrowDown:
{
painter->save();
painter->translate(option->rect.x(),option->rect.y());
QPainterPath drawtriangle; //畫三角形
drawtriangle.moveTo(0,0);
drawtriangle.lineTo(option->rect.width()/2,option->rect.height());
drawtriangle.lineTo(option->rect.width(),0);
drawtriangle.lineTo(0,0);
painter->setPen(QPen(QColor("#128bf1"), 2));
painter->drawPath(drawtriangle); //繪製出圖形
painter->restore();
}
break;
default:
QProxyStyle::drawPrimitive(which, option, painter, widget);
}
}
QRect myComboBoxStyle::subControlRect(ComplexControl whichControl,const QStyleOptionComplex *option,SubControl whichSubControl,const QWidget *widget) const
{
if (whichControl == CC_ComboBox)
{
switch (whichSubControl)
{
case SC_ComboBoxEditField:
return QRect(0,0,option->rect.width() * 0.7,option->rect.height()).adjusted(+2, +2, -2, -2);
case SC_ComboBoxFrame:
return QRect(0,0,option->rect.width() * 0.7,option->rect.height());
case SC_ComboBoxArrow:
return QRect(option->rect.width() * 0.7, 0, option->rect.width() * 0.3, option->rect.height());
default:
return QProxyStyle::subControlRect(whichControl, option,whichSubControl, widget);
}
}
else
{
return QProxyStyle::subControlRect(whichControl, option,whichSubControl, widget);
}
}
效果:
相關文章
- Qt繪製自定義箭頭圖元QT
- Flutter自定義繪製(1)- 繪製基礎Flutter
- Flutter 自定義繪製 ViewFlutterView
- Flutter自定義繪製Widget初探Flutter
- 使用自定義 View 繪製一個懸浮式可拖拽按鈕View
- flutter 用 CustomPaint 繪製自定義圖案FlutterAI
- 使用canvas繪製dribble風格水波浪Canvas
- Flutter日曆,可以自定義風格UIFlutterUI
- Android自定義View之Paint繪製文字和線AndroidViewAI
- flutter 自定義view 繪製曲線統計圖FlutterView
- 自定義部落格園個人皮膚
- Qt Charts 自定義樣式QT
- 自定義繪製鐘錶控制元件,這一篇就夠了控制元件
- QT繪製簡易錶盤QT
- [-Flutter 自定義元件-] 蛛網圖+繪製+動畫實踐Flutter元件動畫
- 例項QT程式 —— Qt自繪製小時鐘QT
- Android自定義View之(一)View繪製流程詳解——向原始碼要答案AndroidView原始碼
- Qt QMessageBox::information 自定義按鈕QTORM
- qt之點的繪製示例demoQT
- 動畫函式的繪製及自定義動畫函式動畫函式
- [Android]多層波紋擴散動畫——自定義View繪製Android動畫View
- 使用joinjs繪製流程圖(六)-自定義節點成html元素JS流程圖HTML
- 自定義一個kaniko映象
- canvas繪製風車效果Canvas
- NCL繪製風向杆
- Qt QComboBox之setEditable和currentTextChanged及其原始碼分析QT原始碼
- JavaScript WebGL 繪製一個面JavaScriptWeb
- CSS 繪製一個時鐘CSS
- Qt實現自定義控制元件QT控制元件
- 自定義控制元件之kotlin繪製折線圖和曲線圖控制元件Kotlin
- 手寫一個自定義PromisePromise
- 自定義一個事件聚合器事件
- Qt 過載QComboBox,實現右側刪除鍵QT
- Android自定義view-自繪ViewAndroidView
- Qt自定義動畫插值函式QT動畫函式
- C/C++ Qt TableDelegate 自定義代理元件C++QT元件
- 自定義View之kotlin繪製手勢設定溫度控制元件ViewKotlin控制元件
- 不如你把我殺了吧 | 繪製自定義的 3D 地圖3D地圖