Access method from class object in another class

  • 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?

  • 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

  • Lifetime Qt Champion

    I assume you are also just starting with c++ so here is a sample
    showing the wise words of @Rednemus as code
    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.

  • 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

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

  • add a signal in Keyboard

    void puttonPressed(const QString& val);

    then in Keyboard constructor put:


    now in Page add a slot:

    void setLineEdit(const QString& val){

    finally in Top constructor, after you create Page and Keyboard, call


  • 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,

    public slots:
    void slotName();

    void slotName()
    in this slot call the Class B methods.


    Class B


    Class C

    void signalName(); // emit the signal

  • Thank you!!!
    Those 2 examples helped me a ton in understanding how signals&slots work outside connecting simple button click to slot.

Log in to reply