mousePressEvent没有进入我的Parent Widget

时间:2014-09-16 15:51:01

标签: qt

我有两个自定义的小部件,其中一个作为父级,另一个作为孩子。

Parent继承QScrollArea,子继承QWidget。

当我点击子窗口小部件时,我没有在我的父窗口小部件中获取mousepressevent。

// 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";
    }

从文章中读到:mouse event propagation

我知道子窗口小部件是否未实现事件处理程序,它将传递给父窗口小部件。但是,当我点击子项时,我的父窗口小部件处理程序没有被调用。 有人能告诉我哪里出错吗?

0 个答案:

没有答案