QT控件大全 三十九 QVCursorQCircularBar

来源:互联网 发布:centos 开启snmp 编辑:程序博客网 时间:2024/06/08 11:06

效果如图:


核心代码:

#ifndef QCIRCULARBAR_H#define QCIRCULARBAR_H#include <Qt>#include <QWidget>#include <QLCDNumber>#include <QPropertyAnimation>class QColor;class QCircularBar : public QWidget{ Q_OBJECT    Q_ENUMS(errorCode);    Q_PROPERTY(double value READ value WRITE setValue);    Q_PROPERTY(double minValue READ minValue WRITE setMinValue);    Q_PROPERTY(double maxValue READ maxValue WRITE setMaxValue);    Q_PROPERTY(double threshold READ threshold WRITE setThreshold);    Q_PROPERTY(int precision READ precision WRITE setPrecision);    Q_PROPERTY(QString label READ label WRITE setLabel);    Q_PROPERTY(int steps READ steps WRITE setSteps);    Q_PROPERTY(int startAngle READ startAngle WRITE setStartAngle);    Q_PROPERTY(int endAngle READ endAngle WRITE setEndAngle);    Q_PROPERTY(QColor foreground READ foreground WRITE setForeground);    Q_PROPERTY(QColor background READ background WRITE setBackground);    Q_PROPERTY(bool enableThreshold READ thresholdEnabled WRITE setThresholdEnabled);    Q_PROPERTY(bool enableNumericIndicator READ enableNumericIndicator WRITE setNumericIndicatorEnabled);    Q_PROPERTY(bool circularBarEnabled READ circularBarEnabled WRITE setCircularBarEnabled);    Q_PROPERTY(bool coverGlassEnabled READ coverGlassEnabled WRITE setCoverGlassEnabled);    Q_PROPERTY(int digitCount READ digitCount WRITE setDigitCount)    Q_PROPERTY(int barSize READ barSize WRITE setBarSize)public:/*!   \brief ErrorCode : This enum represents the code returned by the error signal*/    enum ErrorCode {MaxValueError=1,MinValueError,ThresholdError,TargetError,PrecisionError,ColorError,UnitsEmpty,OutOfRange};/*!  \brief Constructor  \param parent Parent Widget*/    QCircularBar(QWidget *parent = 0);    /*!      \brief Destructor      \param none    */        ~QCircularBar();    /*!  \return The current displayed value*/    double value() const { return m_value; }/*!  \return The minimum widget scale value*/    double minValue() const { return m_minValue; }/*!  \return The maximum widget scale value*/    double maxValue() const { return m_maxValue; }/*!  \return The threshold*/    double threshold() const { return m_threshold; }/*!  \return The number of decimals displayed in the widget scale*/    int precision() const { return m_precision; }/*!  \return The Label*/    QString label()const { return m_label; }/*!  \return The number of steps in the widget scale*/    int steps() const { return m_steps; }/*!  \return The starting angle associated to the minimum value for the widget scale*/    int startAngle()  { return m_startAngle;}/*!  \return The end angle associated to the minimum value for the widget scale*/     int endAngle()  { return m_endAngle; }/*!  \return The foreground color used for the widget*/    QColor foreground() const { return m_foreground; }/*!  \return The background color used for the widget*/    QColor background() const { return m_background; }/*!  \return The enableThreshold flag*/    bool thresholdEnabled() const { return m_thresholdEnabled; }/*!  \return The enableNumericIndicator flag*/    bool enableNumericIndicator() const { return m_numericIndicatorEnabled; }/*!  \return The m_circularBarEnabled flag*/    bool circularBarEnabled() const {return m_circularBarEnabled; };/*!  \return The m_circularBarEnabled flag*/    bool coverGlassEnabled() const {return m_coverGlassEnabled;};    /*!      \return nnumber of digit for LCD indicator    */    int digitCount() const;    int barSize() const {return m_barSize;};signals:/*!  \signals This signal is emitted to report an error condition.*/    void errorSignal(int);/*!  \signals This signal reports a value true when the input value exceeds for the           first time the threshould limit and reports a value false when the input           value decreases under the threshold limit. That means errorSignal is emitted           only one time (with true argument) when the input passes from a value < threshold to a value > threshold           and only one time (with false argument) when the input decreases under the           threshold limit.*/    void thresholdAlarm(bool);public slots:/*!  \slots This slot is used to set QMeter value*/    void setValue(double);/*!  \slots This is an overloaded member function, provided for convenience.*/    void setValue(int);/*!  \slots This slot is used to set the lower limit of the QMeter scale.*/    void setMinValue(double);/*!  \slots This is an overloaded member function, provided for convenience.*/    void setMinValue(int);/*!  \slots This slot is used to set the upper limit of the QMeter scale.*/    void setMaxValue(double);/*!  \slots This is an overloaded member function, provided for convenience.*/    void setMaxValue(int);/*!  \slots This slot is used to set the threshold limit. See  \ref thresholdAlarm for more info.*/    void setThreshold(double);/*!  \slots This is an overloaded member function, provided for convenience.*/    void setThreshold(int);/*!  \slots This slot is used to set the number of decimal digits for the QMeter scale  and QMeter numerical value.*/    void setPrecision(int); /*!  \slots This slot is used to set the label in the QMeter widget. */    void setLabel(QString);/*!  \slots This slot is used to set the number of spets in the QMeter scale.*/    void setSteps(int);/*!  \slots This represents the starting angle correspondign to the lower limit for the QMeter scale.  Positive and negative angles can be used.*/    void setStartAngle(int);/*!  \slots This represents the end angle corresponding to the upper limit for the QMeter scale.  Positive and negative angles can be used.*/    void setEndAngle(int);/*!  \slots This slot is used to set the widget foreground color.*/    void setForeground(QColor);/*!  \slots This slot is used to set the widget background color.*/    void setBackground(QColor);/*!  \slots This slot allows to enable the threshold management. See  \ref thresholdAlarm for more info.*/    void setThresholdEnabled(bool);/*!  \slots This slot allows to enable the numerical value display*/    void setNumericIndicatorEnabled(bool);/*!      \slots This slot allows to enable the circular bar indicator*/    void setCircularBarEnabled(bool);/*!      \slots This slot allows to enable the circular bar indicator*/        void setCoverGlassEnabled(bool);/*!      \slots This slot sets LCD number of digits*/        void setDigitCount(int n_digits);/*!              \slots This slot sets the bar size in pixels*/        void setBarSize(int barSize);protected:        void paintEvent(QPaintEvent *event);        void resizeEvent(QResizeEvent *event);        void changeEvent(QEvent * event);private:    void thresholdManager();    int digits(int val);      void drawCrown(QPainter *painter);    void drawBackground(QPainter *painter);    void drawTicks(QPainter *painter);    void drawCircularBar(QPainter *painter);    void drawCoverGlass(QPainter *painter);    void drawLabel(QPainter *painter);    void drawThresholdLine(QPainter *painter);    double m_value;    double m_maxValue, m_minValue;    int m_precision;    QString m_label;    int m_steps;    int m_barSize;    int m_startAngle,m_endAngle;    double m_threshold;    bool m_autodigits;    bool m_thresholdEnabled;    bool m_numericIndicatorEnabled;    bool m_circularBarEnabled;    bool m_coverGlassEnabled;    QColor m_foreground;    QColor m_background;    QLCDNumber* m_lcd;};#endif // QCIRCULARBAR_H

#include "qcircularbar.h"#include <QtGlobal>#include <QtGui>#include <QPainter>#include <QFont>/*!  \mainpage QCircularBar - A custom widget plugin for Qt 4.x  The QCircularBar widget is a custom widget plugin for Qt Designer/Qt Creator.\n  It requires the Qt GUI library.  \section homepage Project page  The QCircularBar page is hosted at    <a href="http://www.open-gui.eu</a>  \section credits Credits:  \par Author:    P. Sereno*/QCircularBar::QCircularBar(QWidget *parent)    : QWidget(parent){   m_lcd = new QLCDNumber(this);   setPrecision(0);   setSteps(20);   setBarSize(5);   setMinimumSize(QSize(80,80));   setStartAngle(225);   setEndAngle(-45);   setForeground(QColor(0, 166, 8));   setBackground(Qt::black);   setThresholdEnabled(false);   setNumericIndicatorEnabled(true);   int r,g,b;   r=foreground().red();   g=foreground().green();   b=foreground().blue();   QString style=QString("background-color: transparent; color: rgb(%1,%2,%3);").arg(r).arg(g).arg(b);   m_lcd->setStyleSheet(style);   m_lcd->setSegmentStyle(QLCDNumber::Flat);   m_autodigits=false;   setMinValue(0);   setMaxValue(100);   setDigitCount(5);   setValue(0);   setLabel("Label");   setThreshold(80);   setCircularBarEnabled(true);   setCoverGlassEnabled(true);   setEnabled(true);}QCircularBar::~QCircularBar(){    delete m_lcd;}int QCircularBar::digits(int val){    int digits = 0;       if (val <= 0) digits = 1; // remove this line if '-' counts as a digit       while (val) {           val /= 10;           digits++;       }       return digits;}void QCircularBar::setBarSize(int barSize){    m_barSize=barSize;    update();}void QCircularBar::setValue(double value){    if(!isEnabled())        return;    if(value>m_maxValue)    {        m_value=m_maxValue;        emit errorSignal(OutOfRange);    }    else        if(value<m_minValue)        {            m_value=m_minValue;            emit errorSignal(OutOfRange);        }        else        {            m_value=value;            if(m_autodigits)                m_lcd->setDigitCount(digits(value));            if(m_lcd)            {                //QString val = QString( "%1" ).arg(m_value,0,'f',m_precision);                m_lcd->display(m_value);            }        }    if(thresholdEnabled())    {        thresholdManager();        if(value>=threshold())        {            QString style=QString("background-color: transparent; color: rgb(200,0,0);");            m_lcd->setStyleSheet(style);        }        else        {            int r,g,b;            r=foreground().red();            g=foreground().green();            b=foreground().blue();            QString style=QString("background-color: transparent; color: rgb(%1,%2,%3);").arg(r).arg(g).arg(b);            m_lcd->setStyleSheet(style);        }    }    else    {        int r,g,b;        r=foreground().red();        g=foreground().green();        b=foreground().blue();        QString style=QString("background-color: transparent; color: rgb(%1,%2,%3);").arg(r).arg(g).arg(b);        m_lcd->setStyleSheet(style);    }    update();}int QCircularBar::digitCount() const{    if(m_lcd)        return m_lcd->digitCount();    else        return 0;}void QCircularBar::setValue(int value){   setValue((double)value);   update();}void QCircularBar::setMinValue(double value){   m_minValue=value;   update();}void QCircularBar::setMinValue(int value){  setMinValue((double)value);}void QCircularBar::setMaxValue(double value){    if(value > m_minValue)    {        m_maxValue=value;        update();    }    else        emit errorSignal(MaxValueError);}void QCircularBar::setMaxValue(int value){  setMaxValue((double)value);}void QCircularBar::setThreshold(double value){    if(value > m_minValue && value < m_maxValue)    {        m_threshold=value;        m_thresholdEnabled=true;        update();    }    else        emit errorSignal(ThresholdError);}void QCircularBar::setThreshold(int value){  setThreshold((double)value);}void QCircularBar::setPrecision(int precision){   m_precision=precision;   update();}void QCircularBar::setLabel(QString label){    m_label=label;    update();}void QCircularBar::setDigitCount(int n_digits){    if(n_digits>0)    {        if (m_lcd)            m_lcd->setDigitCount(n_digits);    }    else            m_autodigits=true;}void QCircularBar::resizeEvent(QResizeEvent *event){    Q_UNUSED(event);    m_lcd->setGeometry(width()/2-width()/4,height()/2-height()/6,width()/2,height()/3);}void QCircularBar::changeEvent(QEvent *event){    if(!isEnabled())    {        QString style=QString("background-color: transparent; color: rgb(200,200,200);");        m_lcd->setStyleSheet(style);        update();    }    else        setValue(value());}void QCircularBar::paintEvent(QPaintEvent *event){    Q_UNUSED(event);    QPainter painter(this);    painter.setRenderHint(QPainter::Antialiasing, true);    int side = qMin(width(), height());    painter.setViewport((width() - side) / 2, (height() - side) / 2,side, side);    painter.setWindow(-50, -50, 100, 100);    drawBackground(&painter);    if(circularBarEnabled())        drawCircularBar(&painter);    drawTicks(&painter);    if(coverGlassEnabled())        drawCoverGlass(&painter);    drawLabel(&painter);    drawCrown(&painter);    if(thresholdEnabled())        drawThresholdLine(&painter);}void QCircularBar::setCoverGlassEnabled(bool enable){    m_coverGlassEnabled=enable;    update();}void QCircularBar::setSteps(int nSteps){    if(nSteps>1)    {        m_steps=nSteps;        update();    }    else        nSteps=1;}void QCircularBar::setStartAngle(int value){    m_startAngle=value;    update();}void QCircularBar::setEndAngle(int value){    m_endAngle=value;    update();}void QCircularBar::setForeground(QColor newForeColor){    m_foreground=newForeColor;    int r,g,b;    r=foreground().red();    g=foreground().green();    b=foreground().blue();    QString style=QString("background-color: transparent; color: rgb(%1,%2,%3);").arg(r).arg(g).arg(b);    m_lcd->setStyleSheet(style);    update();}void QCircularBar::setBackground(QColor newBackColor){    m_background=newBackColor;    int r,g,b;    r=foreground().red();    g=foreground().green();    b=foreground().blue();    QString style=QString("background-color: transparent; color: rgb(%1,%2,%3);").arg(r).arg(g).arg(b);    m_lcd->setStyleSheet(style);    update();}void QCircularBar::thresholdManager(){    // m_thresholdFlag is used to avoid signals at each setValue    if(m_value > m_threshold /*&& !m_thresholdFlag*/)    {//     m_thresholdFlag=true;     emit thresholdAlarm(true);    }    else     if(m_value < m_threshold /*&& m_thresholdFlag*/)     {//      m_thresholdFlag=false;      emit thresholdAlarm(false);     }}void QCircularBar::setThresholdEnabled(bool enable){  m_thresholdEnabled=enable;  update();}void QCircularBar::setNumericIndicatorEnabled(bool enable){  m_numericIndicatorEnabled=enable;  m_lcd->setVisible(enable);  update();}void QCircularBar::setCircularBarEnabled(bool enable){    m_circularBarEnabled=enable;    update();}


源码工程:QQ:609162385

原创粉丝点击