本系列主要使用Qt painter來實現一些基礎控件。主要是對平時自行編寫的一些自定義控件的總結。c++
爲了簡潔。低耦合,咱們儘可能不使用圖片,qrc,ui等文件,而只使用c++的.h和.cpp文件。git
因爲我的水平有限,學習Qt時間不長,因此代碼中出現BUG,不夠優雅以及有待改進部分,還請見諒和提出意見。github
本項目全部代碼在Qt5.10以上均測試經過,理論上Qt 5版本應該都支持。學習
本系列全部程序,已經放在了github的項目上,項目地址:https://github.com/LOMOoO/CustomControls-Qt測試
(一) 圓形進度條動畫
示例圖如上,該自定義控件主要特色有:ui
1.純QPaint繪製,不包括圖片等文件;this
2.多種自定義控制,很是靈活;spa
3.可以自適應大小,不須要手動調整;3d
4.支持動畫效果。
該圓形進度條值變化時,會有動畫效果,咱們還能夠使用Qt的動畫屬性,讓動畫效果更加優雅,譬如:
QPropertyAnimation* animation=new QPropertyAnimation(this,"_value"); animation->setDuration(500); animation->setStartValue(_value); animation->setEndValue(value); animation->setEasingCurve(QEasingCurve::OutQuad); animation->start();
實際上,咱們還能夠對顯示效果,進行多種自定義設置,例以下
//設置初始角度,順時針逆時針 void setdefault(int,bool); //設置外圈寬度 void setOutterBarWidth(float); //設置內圈寬度 void setInnerBarWidth(float); //設置範圍 void setRange(float, float); //設置當前值 void setValue(float); //設置外圈顏色 void setOutterColor(const QColor&); //設置內圈漸變色 void setInnerColor(const QColor&,const QColor&); void setInnerColor(const QColor&); //設置默認文字顏色 void setDefaultTextColor(const QColor&); //設置控制命令 void setControlFlags(int); //設置顯示數字精度 void setPrecision(int); //設置內圈默認文字樣式 inline void setInnerDefaultTextStyle(InnerDefaultTextStyle style){_innerDefaultTextStyle=style;}
若是還想進一步控制,你能夠選擇自行修改源代碼。更多部分,請參考github上的源代碼