Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue connecting member function to signal
Forum Updated to NodeBB v4.3 + New Features

Issue connecting member function to signal

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 635 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mortbopet
    wrote on last edited by
    #1

    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?

    M 1 Reply Last reply
    0
    • M mortbopet

      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?

      M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      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).

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mortbopet
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved