[Solved] segfault with simple GGraphicsView



  • 'ello everyone.
    I'm just doing a few tests with QGraphicsView, and came across this strange segfult. This program was just suppose to display red dot (which I intended on animating).

    main.cc
    @
    #include <QApplication>
    #include "QtDots.h"

    int main(int argc, char *argv[]) {
    QApplication q_application(argc, argv);

    QtDots dots;
    dots.show();
    
    return q_application.exec&#40;&#41;;
    

    }
    @

    QtDots.h
    @
    #ifndef QTDOTS_H
    #define QTDOTS_H

    #include <QObject>
    #include <QWidget>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsEllipseItem>

    class QtDots : public QGraphicsView {
    Q_OBJECT
    private:
    QGraphicsScene scene;
    QGraphicsEllipseItem dot_1;
    int y;
    public:
    explicit QtDots(QWidget *parent = 0);
    };

    #endif // QTDOTS_H
    @

    QtDots.cc
    @
    #include <Qt>
    #include <QWidget>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QBrush>
    #include <QGraphicsEllipseItem>
    #include <QPainter>
    #include "QtDots.h"

    QtDots::QtDots(QWidget *parent)
    : QGraphicsView(parent), dot_1(0, &scene) {
    scene.setBackgroundBrush(QBrush(Qt::black));
    scene.setSceneRect(0, 0, 400, 400);

    y = 0;
    dot_1.setBrush(QBrush(Qt::red));
    dot_1.setPen(QPen(Qt::red));
    dot_1.setRect(175, 0, 50, 50);
    
    setScene(&scene);
    resize(410, 410);
    setRenderHints(QPainter::Antialiasing);
    

    }
    @

    The segfault occurs at 'setScene(&scene)'. Whats strange is if you comment out 'y = 0', the segfault disapears. Can someone explain to me where my problem is?
    Thanks!



  • Hi

    Try this:
    @
    privite:
    QGraphicsScene *scene;
    QGraphicsEllipseItem *dot_1;
    //...
    QtDots::QtDots(QWidget *parent)
    : QGraphicsView(parent)
    {
    scene = new QGraphicsScene;
    //...
    dot_1 = scene->addEllipse(150, -25, 50, 50);
    dot_1->setBrush(...
    //...
    }
    @



  • Thanks alot!


Log in to reply
 

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