using another class object for signal and slots inside qt
-
using another class object for signal and slots inside qt?
-
Sorry, but I've no idea what you're asking.
You might want to try and rephrase the question or provide a bit more details on what you want to do. -
Sorry, but I've no idea what you're asking.
You might want to try and rephrase the question or provide a bit more details on what you want to do.@Chris-Kawa how to access a class from another class?
-
Well first of all I think you mean to access an instance of a class from a method of another class. Class is a type, like an int, so instances of them can access each other, not their classes (well, unless you want to go into funky metaprogramming, but let's not complicate it).
Anyway, to "access" an instance of a class you have to have a pointer or a reference to it. For example:
class A { public: int x; }; class B { public: void doSomethingWithA(A& a) { a.x = 42; } }; A instanceOfA; B instanceOfB; instanceofB.doSomethingWithA(instanceofA);
Another example is when a class has a member of another class:
class A { public int x; }; class B { A a; public: void doSomethingWithAMember() { a.x = 42; } }; B instanceOfB; instanceofB.doSomethingWithAMember();
Or you can store a pointer to instance of one class in an instance of another class. for example through a constructor:
class A { public int x; }; class B { A* a; public: B(A* ptr) : a(ptr) {} void doSomethingWithAPointer() { a->x = 42; } }; A instanceOfA; B instanceOfB(&instanceofA); instanceofB.doSomethingWithAPointer();
Or you can create an instance in place:
class A { public: int x; }; class B { public: void doSomethingWithLocalA() { A a; a.x = 42; } }; B instanceOfB; instanceofB.doSomethingWithLocalA();
Qt has also a signal/slot mechanism. You can connect two instances so that when one signals something a slot of another is called:
QPushButton button("Press me"); QCheckBox checkbox("Hello"); QObject::connect(&button, &QPushButton::clicked, &checkbox, &QCheckBox::toggle);
And there's million other ways to make instances interact. You should start with what you want to do, not how, and then seek the right approach to accomplish that.