Parented QGraphicsView won't receive events



  • I've made some tests on Graphics and stumble upon an issue that I can't figure out. To sum up, I have a different behavior for the same QGraphicsView when it's parented or not.

    Here is the problem, I have a simple subclass of a QGraphicsView which is like this:

    @#ifndef GRAPHICS_VIEW_H
    #define GRAPHICS_VIEW_H

    #include <QGraphicsView>

    class QTimer;

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

    protected:
    virtual void paintEvent(QPaintEvent* event);

    private:
    QTimer* a_timer;

    };

    #endif // GRAPHICS_VIEW_H
    @

    And the implementation:

    @#include "graphics_view.h"

    #include <QDebug>
    #include <QTimer>

    GraphicsView::GraphicsView(QWidget* parent)
    : QGraphicsView(parent)
    {
    setFrameStyle(QFrame::NoFrame);
    setLineWidth(0);
    a_timer = new QTimer(this);
    connect(a_timer, SIGNAL(timeout()), this, SLOT(update()));
    a_timer->start(50);
    }

    void GraphicsView::paintEvent(QPaintEvent* event)
    {
    qDebug() << "CustomWidget::paintEvent";
    QGraphicsView::paintEvent(event);
    }
    @

    If I instantiate it directly in a main like this:

    @int main(int argv, char *args)
    {
    QApplication app(argv, args);
    GraphicsView
    widget = new GraphicsView();
    widget->show();
    return app.exec();
    }@

    Everything works fine and I've the following output:

    @CustomWidget::paintEvent
    CustomWidget::paintEvent
    CustomWidget::paintEvent
    @

    Repeated every 50 ms.

    But, if I embed it in a parent widget like this one:

    @#ifndef PARENT_WIDGET_H
    #define PARENT_WIDGET_H

    #include <QWidget>

    class GraphicsView;

    class ParentWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit ParentWidget(QWidget *parent = 0);
    void showMe();

    private:
    GraphicsView* a_widget;

    };

    #endif // PARENT_WIDGET_H
    @

    And the implementation:

    @#include "parent_widget.h"
    #include "graphics_view.h"

    #include <QDebug>

    ParentWidget::ParentWidget(QWidget *parent): QWidget(parent)
    {
    a_widget = new GraphicsView(this);
    }

    void ParentWidget::showMe()
    {
    show();
    a_widget->show();
    }
    @

    And I try to use it via this main:
    @
    int main(int argv, char **args)
    {
    QApplication app(argv, args);
    ParentWidget papaWidget;
    papaWidget.showMe();
    return app.exec();
    }@

    Then I have no output. Also changing the parent of ParentWidget::a_widget from this to NULL in ParentWidget constructor solves the issue but then I have two windows open (which is normal).

    I also verified that if I derived GraphicsView from QWidget rather than QGraphicsView then GraphicsView::paintEvent() gest called.

    So how can I parent a QGraphicsView and still update it via the event loop? It must be obvious but I just can't see what's wrong here.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.