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. Unable to add items when function called from other class?
Forum Updated to NodeBB v4.3 + New Features

Unable to add items when function called from other class?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 1.5k Views 2 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.
  • A Offline
    A Offline
    adalovegirls
    wrote on 15 Jan 2021, 17:28 last edited by adalovegirls
    #1

    I have a function that adds some items to a ListWidget (filenames link to the source code in my GitHub repo):

    debugger.cpp

    void Debugger::addRegisterViewItem() {
        for (int i = 0; i < 16; i++) {
            ui->leftRegisterListWidget->addItem(QString("%1").arg(chip8->V[i], 4, 16, QChar{'0'}).toUpper());
        }
    }
    

    when I call this from within the Debugger class it works as expected. However, when I try calling it from another class:

    SDL2Widget.cpp:

    void SDL2Widget::mainLoop() {
    // snip
        debug->updateWidgets();
    // snip
    }
    

    the program panics with a segmentation fault, and upon inspection, the ui pointer in the Debugger class is empty when called from SDL2Widget.cpp.

    I feel like I just have a fundamental lack of knowledge of how classes interact, but I'm not even sure where to look to learn how I should be doing it... I'm sorry for this probably very beginner question, but if someone could point me in the right direction I would be very grateful. :)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 15 Jan 2021, 17:55 last edited by
      #2

      @adalovegirls said in Unable to add items when function called from other class?:

      debug

      Make sure this is a valid pointer.

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

      A 1 Reply Last reply 15 Jan 2021, 18:27
      0
      • C Christian Ehrlicher
        15 Jan 2021, 17:55

        @adalovegirls said in Unable to add items when function called from other class?:

        debug

        Make sure this is a valid pointer.

        A Offline
        A Offline
        adalovegirls
        wrote on 15 Jan 2021, 18:27 last edited by
        #3

        @Christian-Ehrlicher Ahhh... I hadn't realized getDebugContext() was getting called before the Debugger window was actually created. I moved debug = Debugger::getDebugContext(); into mainLoop to test it, and it works. The thing I'm not sure to do, is how could I make sure getDebugContext() is called after the Debugger window is created and shown? I have a feeling reassigning it in every iteration of the loop isn't the best practice.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 15 Jan 2021, 18:37 last edited by
          #4

          First you can use QPointer<QWidget> instead Widget* for your 'debug' member - then you will see if it's still valid when you call SDL2Widget::mainLoop()

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

          A 1 Reply Last reply 15 Jan 2021, 18:45
          0
          • C Christian Ehrlicher
            15 Jan 2021, 18:37

            First you can use QPointer<QWidget> instead Widget* for your 'debug' member - then you will see if it's still valid when you call SDL2Widget::mainLoop()

            A Offline
            A Offline
            adalovegirls
            wrote on 15 Jan 2021, 18:45 last edited by
            #5

            @Christian-Ehrlicher How can I do that exactly? I changed the definition to QPointer<QWidget> debug; and it gives the error implicit instantiation of undefined template 'QPointer<QWidget>'

            Thank you for the help so far!

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 15 Jan 2021, 18:54 last edited by
              #6

              First you should not use QWidget but your derived class (so 'Debugger' in your case) and than you should also include <QPointer>

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

              A 1 Reply Last reply 15 Jan 2021, 19:02
              0
              • C Christian Ehrlicher
                15 Jan 2021, 18:54

                First you should not use QWidget but your derived class (so 'Debugger' in your case) and than you should also include <QPointer>

                A Offline
                A Offline
                adalovegirls
                wrote on 15 Jan 2021, 19:02 last edited by
                #7

                @Christian-Ehrlicher Oh. It wasn't indicating I was missing QPointer but including it fixed it, along with the change from QWidget to Debugger. After doing that however, the pointer is null when the time comes to call updateWidgets().

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 15 Jan 2021, 19:03 last edited by
                  #8

                  So my assumption is correct - the pointer is invalid and therefore the app is crashing.

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

                  A 1 Reply Last reply 15 Jan 2021, 19:18
                  0
                  • C Christian Ehrlicher
                    15 Jan 2021, 19:03

                    So my assumption is correct - the pointer is invalid and therefore the app is crashing.

                    A Offline
                    A Offline
                    adalovegirls
                    wrote on 15 Jan 2021, 19:18 last edited by
                    #9

                    @Christian-Ehrlicher How can I make it valid when I need it, then?

                    K 1 Reply Last reply 15 Jan 2021, 19:37
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 15 Jan 2021, 19:24 last edited by
                      #10

                      @adalovegirls said in Unable to add items when function called from other class?:

                      How can I make it valid when I need it, then?

                      I don't know your code - make sure to create a proper widget, don't delete it afterwards.

                      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
                      • A adalovegirls
                        15 Jan 2021, 19:18

                        @Christian-Ehrlicher How can I make it valid when I need it, then?

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 15 Jan 2021, 19:37 last edited by
                        #11

                        @adalovegirls said in Unable to add items when function called from other class?:

                        How can I make it valid when I need it, then?

                        Out of curiosity, is there a reason that everybody and their mother is a global variable?
                        Anyway, I peeked, and you're almost certainly calling updateWidgets() when there's no Debugger object created.
                        Piece of advice - fix this code pronto. Globals are nasty, everything global is everything nasty ... you get the idea ...

                        Read and abide by the Qt Code of Conduct

                        A 1 Reply Last reply 15 Jan 2021, 19:42
                        2
                        • K kshegunov
                          15 Jan 2021, 19:37

                          @adalovegirls said in Unable to add items when function called from other class?:

                          How can I make it valid when I need it, then?

                          Out of curiosity, is there a reason that everybody and their mother is a global variable?
                          Anyway, I peeked, and you're almost certainly calling updateWidgets() when there's no Debugger object created.
                          Piece of advice - fix this code pronto. Globals are nasty, everything global is everything nasty ... you get the idea ...

                          A Offline
                          A Offline
                          adalovegirls
                          wrote on 15 Jan 2021, 19:42 last edited by
                          #12

                          @kshegunov No reason! I'm just a beginner who doesn't really know what they're doing. :)
                          And yes, I am aware now that I was calling updateWidgets() when Debugger had yet to be created, I'm asking how I can solve this in a more elegant way than calling it every mainLoop iteration:

                          void SDL2Widget::mainLoop() {
                              debug = Debugger::getDebugContext();
                          
                              if (debug)
                                  debug->updateWidgets();
                          }
                          

                          It works, but I'm sure it's not the best way to do it.

                          K 1 Reply Last reply 15 Jan 2021, 20:07
                          0
                          • A adalovegirls
                            15 Jan 2021, 19:42

                            @kshegunov No reason! I'm just a beginner who doesn't really know what they're doing. :)
                            And yes, I am aware now that I was calling updateWidgets() when Debugger had yet to be created, I'm asking how I can solve this in a more elegant way than calling it every mainLoop iteration:

                            void SDL2Widget::mainLoop() {
                                debug = Debugger::getDebugContext();
                            
                                if (debug)
                                    debug->updateWidgets();
                            }
                            

                            It works, but I'm sure it's not the best way to do it.

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 15 Jan 2021, 20:07 last edited by kshegunov
                            #13

                            @adalovegirls said in Unable to add items when function called from other class?:

                            And yes, I am aware now that I was calling updateWidgets() when Debugger had yet to be created, I'm asking how I can solve this in a more elegant way than calling it every mainLoop iteration:

                            If you keep the Debugger as a pseudo-singleton (i.e. a glorified global), this is exactly how you do it.
                            Alternatively, you can enforce that a debugger is created before whatever widget by passing its address through the widget's constructor (and saving it in a member variable). Then you can't create a widget without first having a debugger and you don't get/check anything in the loop. If you're in the loop then Debugger has been passed to you/exists already.

                            Read and abide by the Qt Code of Conduct

                            A 1 Reply Last reply 15 Jan 2021, 20:25
                            0
                            • K kshegunov
                              15 Jan 2021, 20:07

                              @adalovegirls said in Unable to add items when function called from other class?:

                              And yes, I am aware now that I was calling updateWidgets() when Debugger had yet to be created, I'm asking how I can solve this in a more elegant way than calling it every mainLoop iteration:

                              If you keep the Debugger as a pseudo-singleton (i.e. a glorified global), this is exactly how you do it.
                              Alternatively, you can enforce that a debugger is created before whatever widget by passing its address through the widget's constructor (and saving it in a member variable). Then you can't create a widget without first having a debugger and you don't get/check anything in the loop. If you're in the loop then Debugger has been passed to you/exists already.

                              A Offline
                              A Offline
                              adalovegirls
                              wrote on 15 Jan 2021, 20:25 last edited by
                              #14

                              @kshegunov Alright. I guess I'll stick with the current method then, because I don't want it to be a requirement that the debugger is created before the emulator runs.

                              K 1 Reply Last reply 15 Jan 2021, 20:41
                              0
                              • A adalovegirls
                                15 Jan 2021, 20:25

                                @kshegunov Alright. I guess I'll stick with the current method then, because I don't want it to be a requirement that the debugger is created before the emulator runs.

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 15 Jan 2021, 20:41 last edited by
                                #15

                                @adalovegirls said in Unable to add items when function called from other class?:

                                Alright. I guess I'll stick with the current method then, because I don't want it to be a requirement that the debugger is created before the emulator runs.

                                Then I suggest you put the pointer into the class and set it to the widget through a setter, instead of coupling the classes through the Debugger::getDebugContext or pass it through the constructor, but with a default value (of nullptr). As a rule of thumb you want to rely on as few application-global states (a.k.a. global variables) as possible.

                                Read and abide by the Qt Code of Conduct

                                A 1 Reply Last reply 15 Jan 2021, 20:45
                                1
                                • K kshegunov
                                  15 Jan 2021, 20:41

                                  @adalovegirls said in Unable to add items when function called from other class?:

                                  Alright. I guess I'll stick with the current method then, because I don't want it to be a requirement that the debugger is created before the emulator runs.

                                  Then I suggest you put the pointer into the class and set it to the widget through a setter, instead of coupling the classes through the Debugger::getDebugContext or pass it through the constructor, but with a default value (of nullptr). As a rule of thumb you want to rely on as few application-global states (a.k.a. global variables) as possible.

                                  A Offline
                                  A Offline
                                  adalovegirls
                                  wrote on 15 Jan 2021, 20:45 last edited by
                                  #16

                                  @kshegunov I don't know how to do that without an example...

                                  K 1 Reply Last reply 15 Jan 2021, 20:55
                                  0
                                  • A adalovegirls
                                    15 Jan 2021, 20:45

                                    @kshegunov I don't know how to do that without an example...

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 15 Jan 2021, 20:55 last edited by kshegunov
                                    #17

                                    https://www.geeksforgeeks.org/constructors-c/
                                    also
                                    https://www.w3schools.com/cpp/cpp_encapsulation.asp

                                    Read and abide by the Qt Code of Conduct

                                    A 2 Replies Last reply 15 Jan 2021, 21:17
                                    0
                                    • K kshegunov
                                      15 Jan 2021, 20:55

                                      https://www.geeksforgeeks.org/constructors-c/
                                      also
                                      https://www.w3schools.com/cpp/cpp_encapsulation.asp

                                      A Offline
                                      A Offline
                                      adalovegirls
                                      wrote on 15 Jan 2021, 21:17 last edited by
                                      #18
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • K kshegunov
                                        15 Jan 2021, 20:55

                                        https://www.geeksforgeeks.org/constructors-c/
                                        also
                                        https://www.w3schools.com/cpp/cpp_encapsulation.asp

                                        A Offline
                                        A Offline
                                        adalovegirls
                                        wrote on 15 Jan 2021, 21:32 last edited by
                                        #19

                                        @kshegunov Alright. I tried making a setter function in SDL2Widget, but in the header for it I'm getting the error Debugger has not been declared even though I have debugger.h included:

                                        SDL2Widget.h

                                        #ifndef SDL2WIDGET_H
                                        #define SDL2WIDGET_H
                                        
                                        #include "chip8.h"
                                        #include "SDL.h"
                                        #include "debugger.h"
                                        
                                        #include <QDebug>
                                        #include <QWidget>
                                        #include <QTimer>
                                        #include <vector>
                                        
                                        class SDL2Widget : public QWidget
                                        {
                                        	Q_OBJECT    
                                        public:
                                            SDL2Widget(QWidget* parent = nullptr);
                                        	~SDL2Widget();
                                            static void loadRom(std::vector<unsigned char> rom);
                                            static void recKey(int key, bool state);
                                            static Chip8* getC8Context();
                                            static SDL2Widget* getSDLContext();
                                            void setDebugContext(Debugger* dbg);
                                        };
                                        #endif
                                        

                                        SDL2Widget.cpp

                                        void SDL2Widget::setDebugContext(Debugger* dbg) {
                                            debug = dbg;
                                        }
                                        

                                        I'm also getting the error no matching function for call to SDL2Widget::setDebugContext(Debugger*) when calling the setter from the Debugger class:

                                        debugger.cpp

                                        sdl2->setDebugContext(this);
                                        
                                        K 1 Reply Last reply 18 Jan 2021, 18:05
                                        0
                                        • A adalovegirls
                                          15 Jan 2021, 21:32

                                          @kshegunov Alright. I tried making a setter function in SDL2Widget, but in the header for it I'm getting the error Debugger has not been declared even though I have debugger.h included:

                                          SDL2Widget.h

                                          #ifndef SDL2WIDGET_H
                                          #define SDL2WIDGET_H
                                          
                                          #include "chip8.h"
                                          #include "SDL.h"
                                          #include "debugger.h"
                                          
                                          #include <QDebug>
                                          #include <QWidget>
                                          #include <QTimer>
                                          #include <vector>
                                          
                                          class SDL2Widget : public QWidget
                                          {
                                          	Q_OBJECT    
                                          public:
                                              SDL2Widget(QWidget* parent = nullptr);
                                          	~SDL2Widget();
                                              static void loadRom(std::vector<unsigned char> rom);
                                              static void recKey(int key, bool state);
                                              static Chip8* getC8Context();
                                              static SDL2Widget* getSDLContext();
                                              void setDebugContext(Debugger* dbg);
                                          };
                                          #endif
                                          

                                          SDL2Widget.cpp

                                          void SDL2Widget::setDebugContext(Debugger* dbg) {
                                              debug = dbg;
                                          }
                                          

                                          I'm also getting the error no matching function for call to SDL2Widget::setDebugContext(Debugger*) when calling the setter from the Debugger class:

                                          debugger.cpp

                                          sdl2->setDebugContext(this);
                                          
                                          K Offline
                                          K Offline
                                          kshegunov
                                          Moderators
                                          wrote on 18 Jan 2021, 18:05 last edited by
                                          #20

                                          You probably have a cyclic dependency. Forward declare the class(es) and include the headers in the source files only is the solution.

                                          Read and abide by the Qt Code of Conduct

                                          1 Reply Last reply
                                          2

                                          1/20

                                          15 Jan 2021, 17:28

                                          • Login

                                          • Login or register to search.
                                          1 out of 20
                                          • First post
                                            1/20
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved