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. Parented QGraphicsView won't receive events

Parented QGraphicsView won't receive events

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.2k 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.
  • R Offline
    R Offline
    RedWark
    wrote on last edited by
    #1

    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.

    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