Issue connecting member function to signal



  • I'm having a problem with connecting a custom button class, which inherits from QPushButton.

    I want to connect the clicked() signal, (which should be emitted on click, since the parent is QPushButton) to a member function of the class. I've had success with implementing the mousePressEvent function, inside the class.

    #include "looperbutton.h"
    #include <QtGui>
    #include <QApplication>
    
    LooperButton::LooperButton(QWidget *parent)
        : QPushButton(parent)
    {
        this->state = 0;
        connect(this, SIGNAL(clicked()), this, SLOT(clickedButton()));
        setFixedSize(100,100);
    }
    
    void LooperButton::clickedButton()
    {
        QApplication::beep();
        setText("clicked");
    }
    
    void LooperButton::mousePressEvent(QMouseEvent *e)
    {
        //This works
    }
    

    for completion, here is the header:

    #ifndef LOOPERBUTTON_H
    #define LOOPERBUTTON_H
    
    #include <QPushButton>
    
    class LooperButton : public QPushButton
    {
        Q_OBJECT
    protected:
        void enterEvent(QEvent *event);
        void leaveEvent(QEvent *event);
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
    
    
    private slots:
        void clickedButton();
    
    private:
        int state;
    
    public:
        LooperButton(QWidget *parent = 0);
        ~LooperButton();
    };
    
    #endif // LOOPERBUTTON_H
    
    
    

    Any suggestions?



  • Are you completely overriding mousePressEvent()? You probably have to include

    QPushButton::mousePressEvent(event);
    

    in your mousePressEvent to get the standard QPushButton behavior (e.g., emitting the clicked() signal).



  • Perfect, that did the trick.
    Thank you very much.


Log in to reply
 

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