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 does Overriding QWidget::mousePressEvent fail to pass mouse events to the parent widget?
Qt 6.11 is out! See what's new in the release blog

Why does Overriding QWidget::mousePressEvent fail to pass mouse events to the parent widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 756 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1
    class cusQWidget:public QPushButton{
    Q_OBJECT
    public:
         cusQWidget(QWidget* parent = nullptr):QPushButton(parent){}
         virtual ~cusQWidget(){}
    protected:
         void mousePressEvent(QMouseEvent* p)
         {
                 if (this->parentWidget())
                     {
                 
                         //this->setAttribute(Qt::WA_TransparentForMouseEvents, true);
                 
                         QPoint pt = this->mapTo(this->parentWidget(), e->pos());
                 
                         QWidget *w = this->parentWidget()->childAt(pt);
                 
                         if (w)   
                         {
                 
                             pt = w->mapFrom(this->parentWidget(), pt);
                 
                             QMouseEvent *event = new QMouseEvent(e->type(), pt, e->button(), e->buttons(), e->modifiers());
                 
                             QApplication::postEvent(w, event);
                 
                         }
                         //this->setAttribute(Qt::WA_TransparentForMouseEvents, false);
                 
                     }
          }
    };
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      .. because you don't call the base class' implementation.

      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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Please don't post the same question in multiple sub forum.

        One is enough.

        Duplicate

        Closing this one.

        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

        • Login

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