Blinking Custom WIdget problem ...



  • I try to make a custom widget .. a blinking Led utilizing in multiple place of my guy .. all works .. so I add a timer and some void for make possible choice if blinking or not when I use it in my code .... works but these cause some malfunctions when multiple widget blink ... I not see my error ... Precisely: all works when only one widget blink, when blink more than one and other is set to not blinking there are some confusion in time event so certain blinking widget blink other blinking widget no. Other blinking widget set to work in non-blinking mode works correctly .... the code:

    #ifndef QRECTLEDY_H
    #define QRECTLEDY_H
    
    #include <QLabel>
    #include <QPixmap>
    #include <QTimer>
    
    class QRectLedy : public QLabel
    {
        Q_OBJECT
    public:
        QRectLedy(QWidget *parent = 0);
    
    public slots:
    
        void RLYexchange(bool Rly, bool RlyPass);
        void RLYsetFlashing(bool blink);
        bool blink();
        void RLYsetState(bool state);
        bool state();
        void RLYsetFlashRate(int blinkRate);
        int blinkRate();
    
    private slots:
    
        void RYon();
        void RYoff();
        void RLYexchangeBlink();
        //void RLYestinguish();
    
    
    private:
        QTimer* blinkTimer;
        QPixmap onPixmap, offPixmap;
        bool m_blink;
        bool m_state;
        int m_blinkRate;
    
    
    
    };
    
    #endif // QRECTLEDY_H
    
    
    
    
    /**** header end ****************************************************/
    
    
    #include "qrectledy.h"
    #include <QTimer>
    #include <QDebug>
    
    
    bool indexB = false;
    
    QRectLedy::QRectLedy(QWidget *parent)
        : QLabel(parent), onPixmap("/home/k/res/rect_led_yellow_on_130.png"), offPixmap("/home/k/rect_led_yellow_off_130.png")
    {
        setPixmap(offPixmap);
        blinkTimer = new QTimer();
        blinkTimer->start(800);
        connect(blinkTimer, SIGNAL(timeout()), this, SLOT(RLYexchangeBlink()));
    
    
    
    }
    
    void QRectLedy::RLYexchange(bool Rly, bool RlyPass)
    {
        if (RlyPass && Rly && !m_blink) {
            RYon();
        }
        else if(RlyPass && !Rly && !m_blink) {
            RYoff();
        }
        else if (!RlyPass && !Rly && m_blink && !indexB) {
            RYon();
            indexB = true;
        }
        else if (!RlyPass && !Rly && m_blink && indexB) {
            RYoff();
            indexB = false;
        }
        else{
            RYoff();
            indexB = false;
        }
    }
    
    void QRectLedy::RLYexchangeBlink()
    {
        if(m_blink){
           RLYexchange(false, false);
        }
        else{
           RLYexchange(m_state, true);
        }
    }
    
    void QRectLedy::RLYsetFlashRate(int blinkRate)
    {
        m_blinkRate = blinkRate;
    }
    
    void QRectLedy::RLYsetFlashing(bool blink)
    {
        m_blink = blink;
    }
    
    void QRectLedy::RLYsetState(bool state)
    {
        m_state = state;
    }
    
    bool QRectLedy::blink()
    {
        return m_blink;
    }
    
    bool QRectLedy::state()
    {
        return m_state;
    }
    
    int QRectLedy::blinkRate()
    {
        return m_blinkRate;
    }
    
    
    
    void QRectLedy::RYoff()
    {
        setPixmap(offPixmap);
    }
    
    void QRectLedy::RYon()
    {
        setPixmap(onPixmap);
    }
    
    

    giorgio


  • Qt Champions 2016

    Hi
    its due to your GLOBAL variable
    bool indexB = false;

    it should be part of the class as else each blinking will make problem for others as
    the states are mixed.
    Moved to the class and all should be good :)



  • Re: Blinking Custom WIdget problem ...

    Very thanks ... so I declare in header as :

    private:
    
    bool = indexB = false; /***** because C11++ ***********/
    

    All works fine.

    Giorgio


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.