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. [Solved] segfault with simple GGraphicsView
Forum Updated to NodeBB v4.3 + New Features

[Solved] segfault with simple GGraphicsView

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k 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.
  • B Offline
    B Offline
    brandont
    wrote on last edited by
    #1

    '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!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vlad2048
      wrote on last edited by
      #2

      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(...
      //...
      }
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        brandont
        wrote on last edited by
        #3

        Thanks alot!

        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