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. QT program crashing when using the extern keyword on a global object
Forum Updated to NodeBB v4.3 + New Features

QT program crashing when using the extern keyword on a global object

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 7 Posters 1.2k Views 4 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #7

    Hi

    QGraphicsScene *scene_ = new QGraphicsScene();

    seems like you make a local variable.

    if you mean to use the global one, should it not be

    scene_ = new QGraphicsScene();

    1 Reply Last reply
    0
    • G GastonDjam

      I'm trying to use a global object for different classes using the extern keyword but this somehow doesn't work in QT.

      Basically I wanted to create a common GraphicsScene Object to draw rectangles in the same scene but in different classes. That's why I thought a global object "scene_" would be a good choice here.

      manwindow.cpp :

      QGraphicsScene *scene_ = new QGraphicsScene();
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          scene_->setSceneRect(-300,-300,600,600);
          ui->graphicsView->setScene(scene_);
      
          QGraphicsRectItem *rectItem = new QGraphicsRectItem();
          rectItem->setRect(0,0,200,200);
      
          scene_->addItem(rectItem);
      }
      
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      

      mainwindow.h:

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      #include <QGraphicsScene>
      
      #include <QMainWindow>
      
      extern QGraphicsScene *scene_;
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
          private:
              Ui::MainWindow *ui;
          };
          #endif // MAINWINDOW_H    
      

      Here I wanted to retrieve the same object pointer _scene from mainwindow.h and add a second rectangle to it.

      A.c :

          #include "a.h"
          #include "mainwindow.h"
          #include <QGraphicsRectItem>
          
          A::A()
          {
              QGraphicsRectItem *rectItem2 = new QGraphicsRectItem();
              rectItem2->setRect(0,0,200,200);
              scene_->addItem(rectItem2);
          }
      

      main.cpp :

      #include "mainwindow.h"
      #include <QApplication>
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      The program crashes immediately when running it. (Due to the keyword implementation)

      10:17:26: The program has unexpectedly finished.
      10:17:26: The process was ended forcefully.
      Desktop_Qt_6_2_1_MinGW_64_bit-Debug\debug\test.exe crashed.
      

      How do I fix this issue or is there another way to implement this?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #8

      @GastonDjam said in QT program crashing when using the extern keyword on a global object:

      QGraphicsScene *scene_ = new QGraphicsScene();

      Ignoring for a moment whether any of this is a good idea, when do you think that standalone, global, non-class statement, sitting in manwindow.cpp, will be executed? And don't forget, you must do any Qt things like widgets before QApplication has been created....

      If you really feel you must do this, think about where to put the initialization statement....

      1 Reply Last reply
      2
      • JonBJ JonB

        @GastonDjam said in QT program crashing when using the extern keyword on a global object:

        It doesn't show which line exactly was affected

        It certainly should. When you run this under a debugger and get the SIGSEGV the debugger should "jump in" and break. Look at the stack trace pane (or gcc bt command) to see where the crash is happening from.

        G Offline
        G Offline
        GastonDjam
        wrote on last edited by GastonDjam
        #9

        @JonB This is where the program stopped when running the debugger
        43db565e-6738-4036-a1d0-d41a9b30e09a-image.png

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #10

          Hi,

          As already stated by my fellows, static QObject are not to be used.

          Your current architecture is going to trigger a lot of other problems so you should really think about redoing it properly.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • G Offline
            G Offline
            GastonDjam
            wrote on last edited by GastonDjam
            #11

            So this is my design :
            I'm trying to implement a smart home simulation in qt. I basically have 2 windows, one for the app and the other for the home simulation. These 2 windows should communicate with each other
            53a28925-c7e4-45eb-bb18-9cdbe9e8c9ca-image.png

            The goal here is when I click on the first button for example, a dialog window pops up with "Add lights" when clicking on that button a light icon will add up to the home simulation window

            I want scene object to be global / unique so that I can add lights and other stuff to the same window

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #12

              If I understand your desing correctly
              The Simulation Window should have the QGraphicScene variable
              To add lights to the scene, just create a method like:

              addLight()
              {
              QGraphicsRectItem *rectItem2 = new QGraphicsRectItem();
              rectItem2->setRect(0,0,200,200);
               scene_->addItem(rectItem2);
              }
              

              That's just the idea, not working code.

              G 1 Reply Last reply
              1
              • M mpergand

                If I understand your desing correctly
                The Simulation Window should have the QGraphicScene variable
                To add lights to the scene, just create a method like:

                addLight()
                {
                QGraphicsRectItem *rectItem2 = new QGraphicsRectItem();
                rectItem2->setRect(0,0,200,200);
                 scene_->addItem(rectItem2);
                }
                

                That's just the idea, not working code.

                G Offline
                G Offline
                GastonDjam
                wrote on last edited by
                #13

                @mpergand That's what I thought as well but the pattern here is the following : When clicking on that first button of the app , a dialog window will pop up therefore a new class (called dialog for example). So I should create an object in that dialog class that calls addLight method from home simulation window/class. But that would instantiate a new Scene which is not the behavior that I want. I want to modify the same scene object that was instantiated at the beginning in home_simulation class

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #14

                  Use signals&slots between your App window and the dialog.

                  1 Reply Last reply
                  2
                  • G GastonDjam

                    @JonB This is where the program stopped when running the debugger
                    43db565e-6738-4036-a1d0-d41a9b30e09a-image.png

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #15

                    @GastonDjam
                    I will throw one more thing in. This may or may not be a good way to design your software --- and certainly as per @mpergand it sounds like you should be using signals & slots, maybe then this will not be necessary --- but, to answer your original question correctly: I did not say you cannot have a global QGraphicsScene object, I said the way/place you initialised it is unacceptable. The following is OK:

                    QGraphicsScene *g_scene;    // global declaration in .cpp file of pointer variable, *but not instantiated here*, value is `nullptr`
                    
                    // if this is declared in some other file and you want to be able to access it elsewhere you can have the .h file go:
                    extern QGraphicsScene *g_scene;
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);    // create QApplication *first*
                        g_scene = new QGraphicsScene();    // *now* it is OK to create a widget, or you can do this in `MainWindow` or wherever
                    }
                    
                    1 Reply Last reply
                    1
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @JonB said in QT program crashing when using the extern keyword on a global object:

                      This may or may not be a good way to design your software

                      This is no question - the design is bad (the nicest word I could find for it, sorry)

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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