Signal and slot related issue?



  • If i am using signal and slot like this
    @Qobject::connect(b1, SIGNAL(clicked()),b2,SLOT(animate_button())@
    does the address of b2 will pass implicitly to animate_button function?



  • Your connect statement is missing a closing ).

    To answer your question: yes, b2 will have access to a pointer to b1 at the time the slot is invoked via QObject::sender(). However, I recommend you try to avoid it. Usually, QSignalMapper will do the trick. Note that you can not distinguish between different signals of the same object connected to a slot. You can only know what object fired an event that triggered your slot, but not what signal was fired to trigger it.



  • animate_button() is a method of your class. b2 is an instance of that class. So, in animate_button() you have the usual this pointer you can use.



  • animate_button() is supposed to be a slot, i.e. a member-function of a class. b2 is supposed to be [a pointer to] an object of that class. So, the pointer you're looking is... ‘this'.

    If you meant b1 rather than b2, call QObject::sender() to know who sent the signal.



  • i am actually want to stop timer of b2 by clicking b1? timer is stopping but it is stopping after sometime not exactly while clicking.



  • Of course there is some delay. The click creates an event, that is put into the event loop, the event loop is processed, the signal is emitted, all the connected slots are called, the slot is executed, slot stop of the timer is called...

    If your machine is busy, this all can last longer than a few microseconds...



  • @

    Button::Button (QWidget *parent,
    QPixmap &pixmap1,
    QString tool_tip_text,
    char *btn_txt,
    bool erase_bkground,
    QRect rect,
    int num_states
    ) : QPushButton( parent),
    m_hovered(false),
    m_pressed(false),
    is_animate(false)
    {
    pixmap = pixmap1;
    button_width = rect.width ();
    button_height = rect.height();
    num_of_states = num_states;
    num_of_images = pixmap.width()/button_width;
    press_count = 0;
    count = 0;
    link_flag = false;
    button_txt = btn_txt;
    tt_flag = true;
    tt_normal = tool_tip_text;
    timer = new QTimer(this);

    target = QRectF(0.0, 0.0, button_width, button_height);
    source = QRectF(0.0, 0.0, button_width, button_height);
    

    }

    Button::~Button ()
    {}

    void Button::draw_button_normal()

    {

    this->m_pressed = false;
    m_hovered = false;
    //source = QRectF(0.0, 0.0, button_width, button_height);
    qDebug ()<<"normal";
    
    if (this->is_animate) {
        timer->singleShot (5000, this, SLOT(mousePress ()));
    } else {
        timer->stop();
        qDebug()<<"stop timer";
    
       // source = QRectF(0.0, 0.0, button_width, button_height);
    
    
    }
    qDebug()<<"mouse press"<<this->m_pressed<<m_hovered;
    qDebug()<<this;
    this->repaint();
    qDebug()<<this;
    

    }

    void Button::animate_button (bool is_animate)
    {
    this->is_animate = is_animate;
    if (this->is_animate) {
    timer->start ();
    mousePress ();
    qDebug ()<<"timer called";
    } else {
    is_animate = false;

        draw_button_normal();
    }
    qDebug()<<this<< "animate button";
    

    // draw_button_normal ();
    }

    void Button::mousePress ()
    {
    this->m_pressed = true;
    m_hovered = false;

    if(num_of_states == 2) {
        press_count++;
    }
    
    if (this->isEnabled ()) {
        source = QRectF((button_width) * 2.0, 0.0, button_width, button_height);
    }
    if(num_of_images == 4) {
        count++;
        source = QRectF(0.0, 0.0, button_width, button_height);
    }
    
    
    if (this->is_animate) {
        timer->singleShot (5000, this,SLOT(draw_button_normal()));
        qDebug ()<<"mousePress";
    }
    this->repaint();
    

    }

    void Button::paintEvent (QPaintEvent *e)
    {
    qDebug()<<"mpressed1"<<this->m_pressed<<m_hovered;
    Q_UNUSED(e);

    QPainter painter(this);
    painter.setRenderHint(QPainter::HighQualityAntialiasing);
    
    if (!pixmap.isNull ()) {
    
        this->resize(pixmap.width(), pixmap.height());
    
        QRegion region(0,0,0,0);
    
        QImage image = pixmap.toImage();
        QRgb rgb;
    

    ......................
    .................................

        if (this->isEnabled()) {
    
            if(num_of_images == 4) {
    
                painter.drawPixmap(target, pixmap, source);
                painter.drawText  (target, Qt::AlignCenter , tr(button_txt));
    
            } else {
    
                qDebug()<<this;
                if(!this->m_pressed) {
                    qDebug()<<"not this->m_pressed paint event";
                    source = QRectF(0.0, 0.0, button_width, button_height);
                }
    
                painter.drawPixmap(target, pixmap, source);
                painter.drawText  (target, Qt::AlignCenter , button_txt);
                qDebug()<<m_hovered;
    
                if (m_hovered) {
    
                    qDebug()<<"m_hovered paint event";
                    source = QRectF(0.0, 0.0, button_width, button_height);
                    painter.drawPixmap(target, pixmap, source);
                    source = QRectF(button_width, 0.0, button_width, button_height);
                    painter.drawPixmap(target, pixmap, source);
                    //source = QRectF(0.0, 0.0, button_width, button_height);
                    //painter.drawPixmap(target, pixmap, source);
                    painter.drawText  (target, Qt::AlignCenter ,  tr(button_txt));
                }
    
          
            }
    
    
    
            painter.drawPixmap(target, pixmap, source);
            painter.drawText  (target, Qt::AlignCenter ,  tr(button_txt));
        }
        break;
    
    
    case 2:
    


    @

    here actually i am getting a unexpected output
    when i am stopping a timer of button2 from button1 by
    QObject::connect(button1, SIGNAL(clicked()),button2,SLOT(animate_button()));
    when the timer stop the value of m_pressed and m_hovered in draw_button_normal() button is false and false but after this when repaint is called the value of m_pressed and m_hovered is changing to false and true.
    I am not able to detect why this value changed in paint event even if i am not changing.


Log in to reply
 

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