Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    MousePressEvent is not getting in my Parent Widget

    General and Desktop
    3
    4
    1857
    Loading More Posts
    • 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.
    • A
      Abin last edited by

      Hi,

      I have two customized widgets of which one acts as a parent and another as child.

      Parent inherits QScrollArea, and child inherits QWidget.

      I am not getting mousepressevent in my parent widget when i click the child widget. :(

      // child widget
      //table.h
      @#ifndef TABLE_H
      #define TABLE_H

      #include <QWidget>
      #include <QGridLayout>
      #include <QLabel>
      #include <QPoint>
      #include <QMouseEvent>

      class Table : public QWidget
      {
      Q_OBJECT
      public:
      explicit Table(QWidget *parent = 0);

      private:
      QString m_id,
      m_type,
      m_image,
      m_sectionId;
      QPoint m_position;
      int m_seatCount;

      QGridLayout *m_pMainLayout;
      QLabel      *m_pTableImage,
                  *m_pBottomLabel,
                  *m_pSeatImage;
      

      public:
      ...

      //protected:
      // void mousePressEvent(QMouseEvent *event);
      // void mouseReleaseEvent(QMouseEvent *event);
      // void mouseMoveEvent(QMouseEvent *event);
      // void mouseDoubleClickEvent(QMouseEvent *event);

      signals:

      public slots:

      };

      #endif // TABLE_H@

      // table.cpp
      @#include "table.h"
      #include <QDebug>
      #include <QApplication>

      Table::Table(QWidget *parent) :
      QWidget(parent),
      m_seatCount(0)
      {
      qDebug() << Q_FUNC_INFO << "Invoked";

      m_pMainLayout   = new QGridLayout(this);
      m_pTableImage   = new QLabel(this);
      m_pBottomLabel  = new QLabel(this);
      m_pSeatImage    = new QLabel(this);
      
      if (NULL != m_pMainLayout && NULL != m_pTableImage &&
          NULL != m_pBottomLabel  && NULL != m_pSeatImage)
      {
          setLayout(m_pMainLayout);
          m_pMainLayout->addWidget(m_pTableImage, 0, 0, 2, 2);
          m_pMainLayout->addWidget(m_pBottomLabel, 2, 0);
          m_pMainLayout->addWidget(m_pSeatImage, 2, 1);
      
          m_pTableImage->setObjectName("tableImage");
          m_pBottomLabel->setObjectName("bottomLabel");
          m_pSeatImage->setObjectName("seatImage");
      }
      
      qDebug() << Q_FUNC_INFO << "Exits";
      

      }
      @

      //Parent widget
      //hearderfile
      @#ifndef TABLESOURCESCROLLAREA_H
      #define TABLESOURCESCROLLAREA_H

      #include <QScrollArea>
      #include <QVBoxLayout>
      #include <QMouseEvent>
      #include <QDataStream>
      #include <QMimeData>
      #include <QPoint>
      #include <QDrag>

      #include "tablesourcewidget.h"
      #include "table.h"

      class TableSourceScrollArea : public QScrollArea
      {
      Q_OBJECT
      public:
      explicit TableSourceScrollArea(QWidget *parent = 0);

      private:

      void createDislpay();
      
      //TableSourceWidget   *m_pTableSourceWidget;
      QVBoxLayout         *m_pMainVLayout;
      
      
      Table   *m_pSquareTable,
              *m_pCircleTable,
              *m_pOvalTable,
              *m_pRectangleTable;
      

      protected:
      void mousePressEvent(QMouseEvent *event);

      signals:

      public slots:

      };

      #endif // TABLESOURCESCROLLAREA_H
      @

      // source file
      @#include "tablesourcescrollarea.h"
      #include <QDebug>

      TableSourceScrollArea::TableSourceScrollArea(QWidget *parent) :
      QScrollArea(parent)
      {
      createDislpay();
      }

      void TableSourceScrollArea::createDislpay()
      {
      qDebug() << Q_FUNC_INFO << "Invoked";

      m_pMainVLayout      = new QVBoxLayout(this);
      
      m_pSquareTable      = new Table(this);
      m_pCircleTable      = new Table(this);
      m_pOvalTable        = new Table(this);
      m_pRectangleTable   = new Table(this);
      
      if (NULL != m_pSquareTable  && NULL != m_pCircleTable &&
          NULL != m_pOvalTable    && NULL != m_pRectangleTable &&
          NULL != m_pMainVLayout)
      {
          setLayout(m_pMainVLayout);
          // source tables
          m_pMainVLayout->addWidget(m_pSquareTable);
          m_pMainVLayout->addWidget(m_pCircleTable);
          m_pMainVLayout->addWidget(m_pOvalTable);
          m_pMainVLayout->addWidget(m_pRectangleTable);
      

      ...
      }

      qDebug() << Q_FUNC_INFO << "Exits";
      

      }

      void TableSourceScrollArea::mousePressEvent(QMouseEvent *event)
      {
      qDebug() << Q_FUNC_INFO << "Invoked";

      Table *table = static_cast<Table*>(childAt(event->pos()));
      if (!table)
      {
          qDebug() << Q_FUNC_INFO << "Exit NULL Param";
          return;
      }
      
      ...
      qDebug() << Q_FUNC_INFO << "Exits";
      

      }
      @

      as read from article : "mouse event propagation":http://blog.qt.digia.com/blog/2006/05/27/mouse-event-propagation/

      I get to know if child widget doesn't implement event handlers it will be pass to parent widget. But my parent widget handler is not getting is not getting invoked when i click on child.
      Can someone tell where am I going wrong?

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        Where are you setting the parent and child relation ship ? Did you check whether event is really received by the child widget ? You can just implement mousePress in child and see. I feel either parent->child relationship is not correct or event is consumed by some other widget.

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

        1 Reply Last reply Reply Quote 0
        • D
          dvb0222 last edited by

          The Scroll ares itself does not accept the events, rather the scroll area's viewport accepts those events. Rather than subclassing the scroll area to create the event handler, install the event handler on the scroll area's viewport().

          Here is a decent article on this issue

          http://www.setnode.com/blog/mouse-wheel-events-event-filters-and-qscrollarea/


          David Van Beveren
          Oak Park Technology Corp.
          Malibu, California
          vbdavid@gmail.com

          1 Reply Last reply Reply Quote 0
          • A
            Abin last edited by

            @ dvb0222

            Thanks. For time being I've changed my parent widget inheriting from QWidget and it works :)

            Ill try using viewport() and will update.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post