Access method from class object in another class
-
wrote on 17 Nov 2016, 15:45 last edited by
Hey,
i'm having a hard time wrapping my head around this.
Class A has object of class B and class C. All classes are derived from QWidget.
I want to access method of class C from class B.
How do i do it? -
wrote on 17 Nov 2016, 15:49 last edited by
Hello,
First of all you need to include your class C in your class B.
Then if your method in class C is static you can call it with C::yourMethod()
if it isn't, you need to instantiate your C class in your B class -
Hi
I assume you are also just starting with c++ so here is a sample
showing the wise words of @Rednemus as code
https://www.dropbox.com/s/punlkkwekxeflgl/myotherclass.zip?dl=0
It has button and when clicked it calls a function in another class.Note, calling a function directly from another class makes them
really know each other. ( which is (often) bad , later on as changes affect more classes)
A (often) better way is to use Signals and slots.
http://doc.qt.io/qt-5/signalsandslots.html -
wrote on 17 Nov 2016, 16:12 last edited by VRonin
You don't.
B should not even know C exists and vice-versa. A (that's the only one knowing of the existence of both B and C) should take what's needed from C and pass it to B
-
wrote on 18 Nov 2016, 08:51 last edited by
Yep i'm starting with c++. As I didn't get to clear cut solution. I'll explain my problem in a more detailed manner.
I have stacked widget in class Top(derived from QWidget).
I have a Page class with object is created in Top and added to stacked widget.
And i have a class Keyboard which has a line edit and a button.
Page class also has line edit.Now i need to get text from Keyboard line edit when i press a button and pass it(copy) to Page line edit( it could just be QString passing when pressing a button for the sake of simplicity).
My first attempt was to make object of Keyboard in Top and add it to stacked widget. That's where my initial problem occurred there was no way to pass or call anything from Keyboard to Page or vice-versa as VRonin pointed it out.
For my second attempt i added object of Keyboard in Page and i now can access line edit text from Page easily by pressing a button(call method of Keyboard to get text).
But I want to get line edit text of Keyboard when i press button in Keyboard! and copy it to Page line edit.
project -
wrote on 18 Nov 2016, 09:30 last edited by VRonin
add a signal in Keyboard
void puttonPressed(const QString& val);
then in Keyboard constructor put:
connect(button,&QPushButton::clicked,[this]()->void{puttonPressed(lineEdit->text());});
now in Page add a slot:
void setLineEdit(const QString& val){ lineEdit->setText(val); }
finally in Top constructor, after you create Page and Keyboard, call
connect(keyboard,&Keyboard::puttonPressed,page,&Page::setLineEdit);
-
wrote on 18 Nov 2016, 10:58 last edited by
Hi,
Create the objects of Class C and Class B in Class A.
Later for the access of functions(), use can use signal and slot communication.
If u want Class C to communicate with Class B.Ex :
Class A
{
B objectB;
C objectC;// As you have created the object in this class. use connect statement, connect(objectC,SIGNAL(signalName),this,SLOT(slotName)));
public slots:
void slotName();void slotName()
{
in this slot call the Class B methods.
}};
Class B
{};
Class C
{signals:
void signalName(); // emit the signal
}; -
wrote on 18 Nov 2016, 13:30 last edited by
Thank you!!!
Those 2 examples helped me a ton in understanding how signals&slots work outside connecting simple button click to slot.
8/8