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.6k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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

    adalovegirlsA 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      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()

      adalovegirlsA Offline
      adalovegirlsA Offline
      adalovegirls
      wrote on 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
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        adalovegirlsA 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

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

          adalovegirlsA Offline
          adalovegirlsA Offline
          adalovegirls
          wrote on 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            adalovegirlsA 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

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

              adalovegirlsA Offline
              adalovegirlsA Offline
              adalovegirls
              wrote on last edited by
              #9

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

              kshegunovK 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                • adalovegirlsA adalovegirls

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  adalovegirlsA 1 Reply Last reply
                  2
                  • kshegunovK kshegunov

                    @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 ...

                    adalovegirlsA Offline
                    adalovegirlsA Offline
                    adalovegirls
                    wrote on 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.

                    kshegunovK 1 Reply Last reply
                    0
                    • adalovegirlsA adalovegirls

                      @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.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 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

                      adalovegirlsA 1 Reply Last reply
                      0
                      • kshegunovK kshegunov

                        @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.

                        adalovegirlsA Offline
                        adalovegirlsA Offline
                        adalovegirls
                        wrote on 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.

                        kshegunovK 1 Reply Last reply
                        0
                        • adalovegirlsA adalovegirls

                          @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.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 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

                          adalovegirlsA 1 Reply Last reply
                          1
                          • kshegunovK kshegunov

                            @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.

                            adalovegirlsA Offline
                            adalovegirlsA Offline
                            adalovegirls
                            wrote on last edited by
                            #16

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

                            kshegunovK 1 Reply Last reply
                            0
                            • adalovegirlsA adalovegirls

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

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on 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

                              adalovegirlsA 2 Replies Last reply
                              0
                              • kshegunovK kshegunov

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

                                adalovegirlsA Offline
                                adalovegirlsA Offline
                                adalovegirls
                                wrote on last edited by
                                #18
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • kshegunovK kshegunov

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

                                  adalovegirlsA Offline
                                  adalovegirlsA Offline
                                  adalovegirls
                                  wrote on 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);
                                  
                                  kshegunovK 1 Reply Last reply
                                  0
                                  • adalovegirlsA adalovegirls

                                    @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);
                                    
                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on 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

                                    • Login

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