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. Access method from class object in another class
QtWS25 Last Chance

Access method from class object in another class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 4.9k Views
  • 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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rednemus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        4
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • E Offline
            E Offline
            Eligijus
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              5
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7

                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
                };

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                5
                • E Offline
                  E Offline
                  Eligijus
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved