Solved Get an Instances
-
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 -
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. -
hi
@martial123hi 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 intuitiveedit: sry if i misunderstood the question
-
@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 ...
-
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 yourconnects
. Happy coding! -
@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.
-
@jsulm
Hi
its related to this post
https://forum.qt.io/topic/101245/refresh-qlcdnumber
So its not a singleton he is after. -
@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 -
@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 namedui
already which I have seen elsewhere and is your "GUI class"?? -
@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; -
i have done this topic with that :
https://forum.qt.io/topic/101245/refresh-qlcdnumber/34ty for all !
it does not completely answer the question of this topic but for my use it suits me
see
-
@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...
-
@jsulm
I have tried to work with return instance but i have not succeeded ...