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] Subclassing QGraphicsView crash the app

[SOLVED] Subclassing QGraphicsView crash the app

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.9k 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.
  • guidupasG Offline
    guidupasG Offline
    guidupas
    wrote on last edited by
    #1

    Hello all!

    I am trying to subclass QGraphicsView, but its crashing the app.

    How can I do that.

    fluxocaixagraphicsview.h
    @
    #ifndef FLUXOCAIXAGRAPHICSVIEW_H
    #define FLUXOCAIXAGRAPHICSVIEW_H

    #include <QGraphicsView>
    #include <QGraphicsScene>
    //#include <QGraphicsLineItem>

    class fluxoCaixaGraphicsView : public QGraphicsView
    {
    public:
    explicit fluxoCaixaGraphicsView(QWidget *parent = 0);
    ~fluxoCaixaGraphicsView();

    private:
    QGraphicsScene *cenario;

    protected:
    void showEvent(QShowEvent *event);
    void resizeEvent(QResizeEvent *event);
    };

    #endif // FLUXOCAIXAGRAPHICSVIEW_H
    @

    fluxocaixagraphicsview.cpp
    @
    #include "fluxocaixagraphicsview.h"

    fluxoCaixaGraphicsView::fluxoCaixaGraphicsView(QWidget *parent) :
    QGraphicsView(parent)
    {
    QGraphicsScene *cenario = new QGraphicsScene(this);
    }

    fluxoCaixaGraphicsView::~fluxoCaixaGraphicsView()
    {

    }

    void fluxoCaixaGraphicsView::showEvent(QShowEvent *event)
    {

    }

    void fluxoCaixaGraphicsView::resizeEvent(QResizeEvent *event)
    {

    }
    @

    calling it
    @
    QWidget *MainWindow::criaEstruturaNovoIvestimento()
    {
    //code here

    fluxoCaixaGraphicsView *graphicsViewFluxoCaixa = new fluxoCaixaGraphicsView(boxInvestimento3);
    
    //code here
    

    }
    @

    Thanks a lot

    Att.
    Guilherme Cortada Dupas

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Soraltan
      wrote on last edited by
      #2

      Hi,

      you give only few information! Looking at the code given, I don't spot an error (which of course doesn't mean there isn't one ;-) ).

      So:

      • What does 'crash' mean? Compiler error, linker error, exception thrown at runtime, segfault?!?
      • Could you post any error messages given?
      • What is 'boxInvestimento3' given as the parent of your QGraphicsView subclass?

      Best

      Soraltan

      1 Reply Last reply
      0
      • guidupasG Offline
        guidupasG Offline
        guidupas
        wrote on last edited by
        #3

        This is the class where I call this view.
        About the crash, the app just close with the message: The program has unexpectedly finished.

        @
        QWidget *MainWindow::criaEstruturaNovoIvestimento()
        {
        QWidget *tabInvestimento = new QWidget(this->tabWidgetInvestimento);

        QGridLayout *gridLayoutInvestimento2 = new QGridLayout(tabInvestimento);
        gridLayoutInvestimento2->setMargin(0);
        QToolBox *toolboxInvestimento = new QToolBox(tabInvestimento);
        gridLayoutInvestimento2->addWidget(toolboxInvestimento);
        QWidget *boxInvestimento = new QWidget(toolboxInvestimento);
        QWidget *boxInvestimento2 = new QWidget(toolboxInvestimento);
        QWidget *boxInvestimento3 = new QWidget(toolboxInvestimento);
        toolboxInvestimento->addItem(boxInvestimento, "Informações sobre o investimento");
        toolboxInvestimento->addItem(boxInvestimento2, "Lançamento de valore do fluxo de caixa e indicadores");
        toolboxInvestimento->addItem(boxInvestimento3, "Representação gráfica do fluxo de caixa");
        
        //Tool Box 1
        //CODE
        
        //Tool Box 2
        //CODE
        
        //Tool Box 3 - HERE IS THE PROBLEM
        QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
        fluxoCaixaGraphicsView *graphicsViewFluxoCaixa = new fluxoCaixaGraphicsView(boxInvestimento3);
        boxInvestimento3->setLayout(gridLayoutInvestimento5);
        gridLayoutInvestimento5->addWidget(graphicsViewFluxoCaixa);
        
        
        //------------------------------------------------------------------------
        
        tabInvestimento->setLayout(gridLayoutInvestimento2);
        
        connect(modeloLancamentosInvestimento, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(alteraModeloLancamentoInvestimento(QModelIndex,QModelIndex)));
        
        connect(lineEditTMA, SIGNAL(textChanged(QString)), this, SLOT(alteraValorTMA(QString)));
        
        this->objectAuxiliarLancamentosInvestimento->append(tableViewLancamentosInvestimento);
        
        this->objectAuxiliarIndicadoresInvestimento->append(modeloIndicadoresInvestimento);
        
        this->objectAuxiliarTMA->append(lineEditTMA);
        
        //this->objectAuxiliarFluxoCaixaInvestimento->append(graphicsViewFluxoCaixa);
        
        return tabInvestimento;
        

        }
        @

        Att.
        Guilherme Cortada Dupas

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Soraltan
          wrote on last edited by
          #4

          Ok, this is much better. Thanks.

          I admit that I find the code somewhat hard to read. Anyhow, I still cannot spot anything that would cause a problem.

          So, how did you find you the problem is with your QGraphicsView subclass? Did you try (temporarily) adding a standard QGraphicsView and it worked?
          Did you use a debugger? In that case, in which line exactly does it stop. What are the values of pointers and variable involved in that line?

          Best

          Soraltan

          1 Reply Last reply
          0
          • guidupasG Offline
            guidupasG Offline
            guidupas
            wrote on last edited by
            #5

            First of all, thanks a lot for your help.

            Well, I try with a standard QGraphicsView and it works fine. The problem occurs when I declare a QGraphicsScene pointer in the fluxocaixagraphicsview.h file. I have a button to create tabs dynamically in my MainWindow. When I click this button the first time it works, but when I click to create the second tab it crashs. Its happens just when I declare some pointer (any type) in fluxocaixagraphicsview.h

            Att.
            Guilherme Cortada Dupas

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              What do you intend to do with this:
              @QGraphicsScene *cenario = new QGraphicsScene(this);@

              What it will not do is attach the view to that scene. I also didn't see any call to QGraphicsView::setScene

              Can you try to move the GraphicsScene outside of your GraphicsView subclass, and give it a different parent?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Soraltan
                wrote on last edited by
                #7

                I just created a small test project in which I copied your code for the fluxoCaixaGraphicsView and added a QPushButton and a QTabWidget. On each click an new widget was generated with a layout and a new fluxoCaixaGraphicsView instance added. This was then added to the QTabWidget. It compiles and runs no matter how many tabs I generate.

                However, a compiler warning brought my attention to line 6 of the fluxocaixagraphicsview.cpp.

                @
                QGraphicsScene *cenario = new QGraphicsScene(this);
                @
                Are you aware that you are using a local pointer there?!? This pointer is freed as soon as the constructor is finished and you loose your only reference to the QGraphicsScene. The pointer declared in line 15 of fluxocaixagraphicsview.h remains uninitialised. Accessing this pointer from outside will surely lead to a segfault. Better:

                @
                cenario = new QGraphicsScene(this);
                @

                Hope it helps.

                Soraltan

                Hope it

                1 Reply Last reply
                0
                • guidupasG Offline
                  guidupasG Offline
                  guidupas
                  wrote on last edited by
                  #8

                  I got it.

                  Soraltan

                  I am using the QGraphicsView inside a QGridLayout and it resizes fine. My problem is that I need to resize the itens inside a QGraphicsScene that is inside the view. I need the itens resizing when the view is resized. Could you help me in this?

                  Att.
                  Guilherme Cortada Dupas

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Soraltan
                    wrote on last edited by
                    #9

                    So, your crash-problem is unrelated to that resizing problem isn't it?!? Your crash problem is solved then?!? If so,glad I could help! Could you please mark the thread as solved by prepending a [solved].

                    Anyhow, we recently had a discussion about resizing items together with their view here in the forum. Have a look at "this":https://qt-project.org/forums/viewthread/42823!

                    Best

                    Soraltan

                    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