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. Why mousePressEvent() not virtual, but can override it?

Why mousePressEvent() not virtual, but can override it?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.1k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    i inherite a class from QGraphicsView, and simply write a function void mousePressEvent(...), and it works...
    BUT i find that in QGraphicsView, it's also not virtual, just void mousePressEvent(...), so i'm confused why and how it works...

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      It is virtual function coming from base class QWidget.

      @virtual void mousePressEvent(QMouseEvent * event)@

      So it is same everywhere. What is confusion ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • P Offline
        P Offline
        primem0ver
        wrote on last edited by
        #3

        It is possible to have a function work even if the base function is not virtual. It would help to see the function code. Does it call the original method? Are the arguments the same? More than likely it is simply hiding the old code and since the function exists with the same arguments, the new function takes the place of the inherited function.

        This can cause problems under certain conditions so you should be careful when doing this. For example, if there is a significant part of the original code left out and you don't call the original code (by casting a pointer to the object as the base object and calling the base objects function) then the function may not do everything it is supposed too... causing unwanted side effects.

        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