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. When I have to call base class in event functions?
QtWS25 Last Chance

When I have to call base class in event functions?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 978 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @qwe3 said in When I have to call base class in event functions?:

    So why he called QWidget::keyPressEvent(e)

    Because the base class might do something with this event. So it depends on what you want to achieve.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    2
    • Q Offline
      Q Offline
      qwe3
      wrote on last edited by
      #3

      @Christian-Ehrlicher Thank you for reply. But what with Chris Kawa's example? Here I have to call QWidget::keyPressEvent(e);?

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @qwe3 said in When I have to call base class in event functions?:

        But what with Chris Kawa's example? Here I have to call QWidget::keyPressEvent(e);?

        As I said - it depends. When you want that the base class can handle key press events then yes. If you don't then no.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qwe3
          wrote on last edited by
          #5

          @Christian-Ehrlicher And I don't undestand it. For example I have myWidget class and his base class is QWidget. I don' see any reason, why QWidget will be need QMouseEvent - only myWidget need that event, In mousePressEvent of myWidget I will do everything what I need.

          And in Chris Kawa's example I don't know why he called base class. What this base class do with this event?

          Christian EhrlicherC 1 Reply Last reply
          0
          • Q qwe3

            @Christian-Ehrlicher And I don't undestand it. For example I have myWidget class and his base class is QWidget. I don' see any reason, why QWidget will be need QMouseEvent - only myWidget need that event, In mousePressEvent of myWidget I will do everything what I need.

            And in Chris Kawa's example I don't know why he called base class. What this base class do with this event?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @qwe3 said in When I have to call base class in event functions?:

            What this base class do with this event?

            https://doc.qt.io/qt-5/qwidget.html#keyPressEvent

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • Q Offline
              Q Offline
              qwe3
              wrote on last edited by qwe3
              #7

              @Christian-Ehrlicher Hmmm... I still don't undestand it very well.

              If you reimplement this handler, it is very important that you call the base class implementation if you do not act upon the key.
              

              What means "act upon the key"?

              Chris Kawa did:

              keys[e->key()] = true; 
              

              So I think, he act upon the key.

              EDIT:
              Can you write small example, where I will se what means "act upon the key"?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                It's clearly documented what the base class does and therefore what's not going to work when you don't call it:

                "The default implementation closes popup widgets if the user presses the key sequence for QKeySequence::Cancel (typically the Escape key). Otherwise the event is ignored, so that the widget's parent can interpret it."

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • Q Offline
                  Q Offline
                  qwe3
                  wrote on last edited by qwe3
                  #9

                  @Christian-Ehrlicher And only this? This escape sequence? So when my program doesn't use popup widgets there is no matter if I call base class or no?

                  EDIT What with paintEvent? in docs https://doc.qt.io/qt-5/qwidget.html#paintEvent I don't see any information about things, which will be different, when I don't call base class.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    As already said more than once - if you want to paint by yourself, don't call the base class. If you want to add something, call the base class and then do your painting. I don't see anything Qt specific here - it's the nature of virtual c++ functions.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    3
                    • Q Offline
                      Q Offline
                      qwe3
                      wrote on last edited by qwe3
                      #11

                      @Christian-Ehrlicher Now I understand call base class in paintEvent:

                      void myRadio::paintEvent(QPaintEvent *event)
                      {
                          QRadioButton::paintEvent(event); // this is important to draw radio button circle
                          QPainter painter(this);
                          painter.drawLine(0,0,20,20);
                      }
                      

                      Perfect. Thank you.

                      But I still don't understand this keyPressEvent. Is there any difference in simple myWidget, which inherits QWidget and when this class don't use popup widgets?

                      1 Reply Last reply
                      1
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @qwe3 said in When I have to call base class in event functions?:

                        which inherits QWidget and when this class don't use popup widgets?

                        According the documentation - no.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1

                        • Login

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