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. Get an Instances
Qt 6.11 is out! See what's new in the release blog

Get an Instances

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 4.4k 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    What are you trying to achieve?

    Not sure but you might want to build a singleton.

    Or, if you want to get such instance by other means... well then some code somewhere (in another class or in main.cpp) will be calling your class constructor - meaning it will have access to your object pointer. That code can then provide the pointer to other objects which need it to connect to.

    (Z(:^

    1 Reply Last reply
    2
    • martial123M martial123

      Hi,
      In this exemple :

      Exemple :

      class::class()
      {
          INSTANCE *instanceOfClass = new INSTANCE();
          instanceOfClass->show()
      }
      

      Is it possible to recover this instance : "instanceOfClass " by other class to connect signal for exemple ?

      If it's possible, how ?
      Thank you

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #3

      hi
      @martial123

      hi you can move your instanceOfClass to member of your class called class
      so in the header you can add private member

      //myclass.h 
      private:
      INSTANCE *instanceOfClass;
      
      
      Myclass::MyClass()
      {
          instanceOfClass = new INSTANCE();
          QObject::connect(instanceOfClass,&INSTANCE::signalName,this,&Myclass::slotName);
          instanceOfClass->show()
      }
      

      note :
      your instanceOfClass is an instance of a class called INSTANCE which is not very intuitive

      edit: sry if i misunderstood the question

      1 Reply Last reply
      1
      • martial123M Offline
        martial123M Offline
        martial123
        wrote on last edited by
        #4

        @martial123 said in Get an Instances:

        INSTANCE

        i think its the same like singleton ..
        I just would like 1 instance and be able to acces at this instance by the class what i want ...
        I have never implemented singleton ...

        leLev i have try this ... but without result ...

        jsulmJ 1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #5

          @martial123

          Hello! Take a look at my example of the singleton.

          testclass.h

          class TestClass : public QObject
          {
              Q_OBJECT
          
          public:
              static TestClass &instance();
          }
          

          testclass.cpp

          TestClass &TestClass::instance()
          {
              static TestClass testInstance;
              return testInstance;
          }
          

          Then, you can use TestClass::instance() in your connects. Happy coding!

          1 Reply Last reply
          0
          • martial123M martial123

            @martial123 said in Get an Instances:

            INSTANCE

            i think its the same like singleton ..
            I just would like 1 instance and be able to acces at this instance by the class what i want ...
            I have never implemented singleton ...

            leLev i have try this ... but without result ...

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @martial123 Then simply implement a getter:

            // You should really use more meaningful names in your code examples...
            class class
            {
            public:
                INSTANCE* getInstance();
            private:
                INSTANCE *instance;
            };
            
            class::class()
            {
                instanceOfClass = new INSTANCE();
                instanceOfClass->show()
            }
            
            INSTANCE* getInstance()
            {
                return instance;
            }
            

            Also, from your description it is not clear whether INSTANCE is actually same as class? If it is not then this is not a singleton.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            mrjjM 1 Reply Last reply
            1
            • jsulmJ jsulm

              @martial123 Then simply implement a getter:

              // You should really use more meaningful names in your code examples...
              class class
              {
              public:
                  INSTANCE* getInstance();
              private:
                  INSTANCE *instance;
              };
              
              class::class()
              {
                  instanceOfClass = new INSTANCE();
                  instanceOfClass->show()
              }
              
              INSTANCE* getInstance()
              {
                  return instance;
              }
              

              Also, from your description it is not clear whether INSTANCE is actually same as class? If it is not then this is not a singleton.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              @jsulm
              Hi
              its related to this post
              https://forum.qt.io/topic/101245/refresh-qlcdnumber
              So its not a singleton he is after.

              martial123M 1 Reply Last reply
              1
              • mrjjM mrjj

                @jsulm
                Hi
                its related to this post
                https://forum.qt.io/topic/101245/refresh-qlcdnumber
                So its not a singleton he is after.

                martial123M Offline
                martial123M Offline
                martial123
                wrote on last edited by
                #8

                @mrjj said in Get an Instances:

                So its not a singleton he is after.

                in fact I just need 1 instance of my gui class .. and this instance it serve for connect many signal by other class .. so my use looks like the singleton not ??
                Ty for all of you for your response

                JonBJ 1 Reply Last reply
                0
                • martial123M martial123

                  @mrjj said in Get an Instances:

                  So its not a singleton he is after.

                  in fact I just need 1 instance of my gui class .. and this instance it serve for connect many signal by other class .. so my use looks like the singleton not ??
                  Ty for all of you for your response

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @martial123 , @ others
                  Still not sure what "1 instance of my gui class" means. I don't do Qt from C++/Qt Creator, so my usage is a bit different, but don't you have some sort of "global" variable named ui already which I have seen elsewhere and is your "GUI class"??

                  martial123M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @martial123 , @ others
                    Still not sure what "1 instance of my gui class" means. I don't do Qt from C++/Qt Creator, so my usage is a bit different, but don't you have some sort of "global" variable named ui already which I have seen elsewhere and is your "GUI class"??

                    martial123M Offline
                    martial123M Offline
                    martial123
                    wrote on last edited by
                    #10

                    @JonB
                    yes i have !
                    all my classes are design by QT Design so he created variable ui like this in all classes :
                    private:
                    Ui::Window018703 *ui;

                    1 Reply Last reply
                    0
                    • martial123M Offline
                      martial123M Offline
                      martial123
                      wrote on last edited by
                      #11

                      i have done this topic with that :
                      https://forum.qt.io/topic/101245/refresh-qlcdnumber/34

                      ty for all !

                      it does not completely answer the question of this topic but for my use it suits me

                      see

                      jsulmJ 1 Reply Last reply
                      0
                      • martial123M martial123

                        i have done this topic with that :
                        https://forum.qt.io/topic/101245/refresh-qlcdnumber/34

                        ty for all !

                        it does not completely answer the question of this topic but for my use it suits me

                        see

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @martial123 said in Get an Instances:

                        it does not completely answer the question of this topic

                        Did you check my suggestion? If I understand your question correctly it should give you the solution...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        martial123M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @martial123 said in Get an Instances:

                          it does not completely answer the question of this topic

                          Did you check my suggestion? If I understand your question correctly it should give you the solution...

                          martial123M Offline
                          martial123M Offline
                          martial123
                          wrote on last edited by
                          #13

                          @jsulm
                          I have tried to work with return instance but i have not succeeded ...

                          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