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. Hover event on QPushButton

Hover event on QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 26.4k 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.
  • ShodanS Offline
    ShodanS Offline
    Shodan
    wrote on last edited by
    #1

    Hi.

    I want to change the icons of my buttons when mouse goes over it.
    They are created like this:

        btn1 = new QPushButton("", this);
        btn1->setGeometry(0, 150, 100, 100);
        btn1->setIcon(QIcon("btnLogs/1.jpg"));
        btn1->setIconSize(QSize(100, 100));
        QObject::connect(btn1, SIGNAL(clicked()), this, SLOT(slotBtn1()));
    

    Found the QEvent::HoverEnter and QEvent::HoverLeave events in the Qt documentation, but I fear that I will not get them to work without some help.

    I tried something like this for example:

        btn1->QEvent::HoverEnter;
        {
        btn1->setIcon(QIcon("btnLogs/1_hover.jpg"));
        }
        btn1->QEvent::HoverLeave;
        {
        btn1->setIcon(QIcon("btnLogs/1.jpg"));
        }
    

    It tells me that 'QEvent' is not a base of 'QPushButton' btn1->QEvent::HoverEnter;

    I didn't find an easy example for this on google.

    Thanks for your help.

    C 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      HoverEnter and HoverLeave are two enumeration value.

      In your case, you should implement an event filter that you'll set on your QPushButton

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • ShodanS Offline
        ShodanS Offline
        Shodan
        wrote on last edited by Shodan
        #3

        I tried but I did not really understant everything.

        In the .h file I declared a function called filterBtn1();

        #ifndef TEST_H
        #define TEST_H
        
        #include <QApplication>
        #include <QWidget>
        #include <QPixmap>
        #include <QPushButton>
        #include <QObject>
        #include <QHoverEvent>
        
        class myMainWindow : public QWidget
        {
            public:
            myMainWindow();
        
            public slots:
            void filterBtn1();
        
            private:
            QPushButton *btn1;
        };
        

        The main.cpp

        #include "Test.h"
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            myMainWindow mainWindow;
            mainWindow.setFixedSize(1024, 768);
            mainWindow.show();
        
            return app.exec();
        }
        

        Then I added an event filter: "btn1->installEventFilter(filterBtn1);"
        And I understood that I have to create a class in the .cpp file and set boolean conditions:
        From this point, the example of the Qt documentation is a bit to complicate for me.

        #include "Test.h"
        
        myMainWindow::myMainWindow() : QWidget()
        {
            btn1 = new QPushButton("", this);
            btn1->setGeometry(0, 150, 104, 103);
            btn1->setIcon(QIcon("btnLogs/1.jpg"));
            btn1->setIconSize(QSize(104, 103));
            btn1->installEventFilter(filterBtn1);
        }
        
        class filterBtn1 : public QObject
        {
            Q_OBJECT
        protected:
            bool eventFilter(QObject ???*, QEvent *???);
        };
        
        bool filterBtn1::eventFilter(QObject *???, QEvent *???)
        {
            if (event->type() == QEvent::HoverEnter) {
        btn1->setIcon(QIcon("btnLogs/1_hover.jpg"));
            } else {
        ???
            }
        }
        
        1 Reply Last reply
        0
        • ShodanS Offline
          ShodanS Offline
          Shodan
          wrote on last edited by
          #4

          If someone could help me with that it would be great.

          1 Reply Last reply
          0
          • ShodanS Shodan

            Hi.

            I want to change the icons of my buttons when mouse goes over it.
            They are created like this:

                btn1 = new QPushButton("", this);
                btn1->setGeometry(0, 150, 100, 100);
                btn1->setIcon(QIcon("btnLogs/1.jpg"));
                btn1->setIconSize(QSize(100, 100));
                QObject::connect(btn1, SIGNAL(clicked()), this, SLOT(slotBtn1()));
            

            Found the QEvent::HoverEnter and QEvent::HoverLeave events in the Qt documentation, but I fear that I will not get them to work without some help.

            I tried something like this for example:

                btn1->QEvent::HoverEnter;
                {
                btn1->setIcon(QIcon("btnLogs/1_hover.jpg"));
                }
                btn1->QEvent::HoverLeave;
                {
                btn1->setIcon(QIcon("btnLogs/1.jpg"));
                }
            

            It tells me that 'QEvent' is not a base of 'QPushButton' btn1->QEvent::HoverEnter;

            I didn't find an easy example for this on google.

            Thanks for your help.

            C Offline
            C Offline
            c64zottel
            wrote on last edited by
            #5

            @Shodan said in Hover event on QPushButton:

            QPushButton

            As far as I see it, its quite similar as in Qml. You can use:
            void QWidget::enterEvent(QEvent *event)
            void QWidget::leaveEvent(QEvent *event)
            from here:
            http://doc.qt.io/qt-5/qpushbutton-members.html
            An event is sent to the widget when the mouse cursor enters the widget.
            A leave event is sent to the widget when the mouse cursor leaves the widget.

            1 Reply Last reply
            1
            • ShodanS Offline
              ShodanS Offline
              Shodan
              wrote on last edited by
              #6

              @c64zottel

              I tried with those members like this:
              .h file

              #ifndef TEST2_H
              #define TEST2_H
              
              #include <QApplication>
              #include <QWidget>
              #include <QPushButton>
              
              
              class myMainWindow : public QWidget
              {
                  Q_OBJECT
              
                  public:
                  myMainWindow();
                  QPushButton *btn1;
              
                  public slots:
                  void enterEvent(QPushButton *btn1);
                  void leaveEvent(QPushButton *btn1);
              
                  private:
              };
              
              #endif // TEST2_H
              

              .cpp file

              #include "test2.h"
              
              myMainWindow::myMainWindow() : QWidget()
              {
                  btn1 = new QPushButton("", this);
                  btn1->setGeometry(0, 150, 104, 103);
                  btn1->setIcon(QIcon("btnLogs/1.jpg"));
                  btn1->setIconSize(QSize(104, 103));
              }
              
              void myMainWindow::enterEvent(QPushButton *btn1)
              {
                  btn1->setIcon(QIcon("btnLogs/1_hover.jpg"));
              }
              
              void myMainWindow::leaveEvent(QPushButton *btn1)
              {
                  btn1->setIcon(QIcon("btnLogs/1.jpg"));
              }
              

              No errors come up when I run the programm. Nothing happens when mouse goes over the button.
              I belive there is a connection to do with the button but I can't figure out how.
              At least I hope this is the right way to use void QWidget::enterEvent(QEvent *event).

              Thanks for your answers

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                With all due respect, it seems you have a lack of C++ knowledge regarding virtual method re-implementation.

                The methods you re-implement must have matching signatures.

                Did you read the documentation chapter about handling events I linked to ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ShodanS Offline
                  ShodanS Offline
                  Shodan
                  wrote on last edited by
                  #8

                  Yes I know.
                  I began with Qt only a few days ago :-)

                  Of course I read id and tried to follow the examples.
                  Since I am not english, many things are hard to understand.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Well, in this case, it has nothing to do with Qt but basic C++. See polymorphism.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      c64zottel
                      wrote on last edited by
                      #10

                      Ok, to test myself I did this in about 10 min. Since I can't upload a tar file, here is a link:
                      [https://www.file-upload.net/download-12680886/TestEnterLeaveEvent.tar.gz.html](link url)

                      For the sake of discussion and future references, here is the main code:

                      class CustomButton : public QPushButton
                      {
                          // QWidget interface
                      protected:
                          virtual void enterEvent(QEvent *) {
                              qDebug() << "entering";
                              QPalette pal = palette();
                              pal.setColor(QPalette::Button, QColor(Qt::blue));
                              setAutoFillBackground(true);
                              setPalette(pal);
                              update();
                          }
                      
                          virtual void leaveEvent(QEvent *) {
                              qDebug() << "leaving";
                              QPalette pal = palette();
                              pal.setColor(QPalette::Button, QColor(Qt::red));
                              setAutoFillBackground(true);
                              setPalette(pal);
                              update();
                          }
                      };
                      

                      The MainWindow has a pointer to a CustomButton, which is initialized in the ctor:

                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                          cb = new CustomButton;
                      
                          cb->setText( "My funny button");
                          this->setCentralWidget( cb );
                      }
                      
                      J.HilkJ 1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @c64zottel There's no need to call setAutoFileBackground in every function. That's typically something you do once in the constructor.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • C c64zottel

                          Ok, to test myself I did this in about 10 min. Since I can't upload a tar file, here is a link:
                          [https://www.file-upload.net/download-12680886/TestEnterLeaveEvent.tar.gz.html](link url)

                          For the sake of discussion and future references, here is the main code:

                          class CustomButton : public QPushButton
                          {
                              // QWidget interface
                          protected:
                              virtual void enterEvent(QEvent *) {
                                  qDebug() << "entering";
                                  QPalette pal = palette();
                                  pal.setColor(QPalette::Button, QColor(Qt::blue));
                                  setAutoFillBackground(true);
                                  setPalette(pal);
                                  update();
                              }
                          
                              virtual void leaveEvent(QEvent *) {
                                  qDebug() << "leaving";
                                  QPalette pal = palette();
                                  pal.setColor(QPalette::Button, QColor(Qt::red));
                                  setAutoFillBackground(true);
                                  setPalette(pal);
                                  update();
                              }
                          };
                          

                          The MainWindow has a pointer to a CustomButton, which is initialized in the ctor:

                          MainWindow::MainWindow(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                              cb = new CustomButton;
                          
                              cb->setText( "My funny button");
                              this->setCentralWidget( cb );
                          }
                          
                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @c64zottel If it's just the background color you want to change, I would recomment the use of a StyleSheet:

                          QPushButton *btn = new QPushButton();
                          btn->setObjectName("btnName_1");
                          btn->show();
                          btn->setSteyleSheet(
                          "   QPushButton#btnName_1 {"
                          "     background-color: yellow;"
                          " }"
                          " QPushButton#btnName_1:pressed {"
                          "     background-color: rgb(224, 0, 0);     "
                          " }"
                          " QPushButton#btnName_1:hover {"
                          "     background-color: rgb(224, 255, 0);"
                          " }"
                          
                          "QPushButton#btnName_1:hover:pressed"
                          "{"
                          "    background-color:red;"
                          "}"
                          );
                          

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          6
                          • ShodanS Offline
                            ShodanS Offline
                            Shodan
                            wrote on last edited by
                            #13

                            I tried to change the button icon with a styleSheet like this:

                            #include "test2.h"
                            
                            myMainWindow::myMainWindow() : QWidget()
                            {
                                btn1 = new QPushButton("", this);
                                btn1->setGeometry(0, 150, 104, 103);
                                btn1->setObjectName("btn1_Name");
                                btn1->show();
                                btn1->setStyleSheet(
                                "   QPushButton#btn1_Name {"
                                "     background-image: url('btnLogs/1.jpg');"
                                " }"
                                " QPushButton#btn1_Name:pressed {"
                                "     background-image: url('btnLogs/1_pressed.jpg');"
                                " }"
                                " QPushButton#btn1_Name:hover {"
                                "     background-image: url('btnLogs/1_hover.jpg');"
                                " }"
                                "QPushButton#btn1_Name:hover:pressed"
                                "{"
                                "     background-image: url('btnLogs/1_pressed.jpg');"
                                "}"
                                );
                            }
                            

                            It seems to work, even better than expected since there are the "pressed" status within.
                            For what I want to do it's just great!

                            I'll try to do this using a event filter in my next programm, with a bit more experience in Qt.

                            Thanks all the people for your help with this.

                            Best regards :-}

                            1 Reply Last reply
                            3

                            • Login

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