Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Access function in another class

    General and Desktop
    4
    9
    3278
    Loading More Posts
    • 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.
    • A
      Anonymous last edited by

      Hello, I am trying to access function from another class. I've been googling quite a lot but have not found any useful tips... I know my question concerns C++ more than a Qt, but anyway...

      I have two classes... Class A is reading some data from files and should pass data to class B, and B should write that data to textEdit... How can I call function writeText in class B, from class A? I can not inherit class B or initialize it again, because that would cause mess with db connections in class B... Passing data to class B was successful using static function, but with static I can't access non-static objects, so can't write to textEdit...

      @class A
      {
      void readData();
      }

      void A::readData()
      {
      QString s;
      writeText(s);
      }

      class B
      {
      public:
      void writeText(QString s);
      }

      void B::writeText(QString s)
      {
      ui->textEdit->writePlainHTML(s);
      }@

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Since you are modifying a widget, it depends more on how you are triggering the read of class A. What is the use case ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • A
          Anonymous last edited by

          readData in class A is constantly reading file data (every second). When it gets certain data, it should trigger function writeText() from B class...

          For example:
          Reading some text...
          orange
          pineaple
          lemon // When readData reaches that line it should call writeText(lemon)
          apple

          I'd need vice versa "communication" as well... When user click button in class B, it should call getData() in class A...

          @A::QString getData()
          {
          return string;
          }@

          I will have several instances of class A (25-30) and each will process different file...

          1 Reply Last reply Reply Quote 0
          • dheerendra
            dheerendra Qt Champions 2022 last edited by

            You can work with Signals/Slots of Qt to make it clean separation between Class A and B and still communicate each other.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply Reply Quote 1
            • T
              theknight47 last edited by

              Hi,

              In order to realize this, you have to use a signal/slot mechanism like this :

              @class A
              {

              void readData();

              //add this
              signals:
              writeText( QString );
              }

              void A::readData()
              {
              QString s;
              //writeText(s); replacing this line by :
              emit writeText ( s );
              }@

              and in class B :

              @class B
              {
              A a; // creating an object of A in B

              public slots : // this is a slot, not a ordinary method
              void writeTextB(QString s);

              QObject::connect( &a, SIGNAL (writeText( QString) ), this, SLOT( writeTextB ( QString)) );
              }

              void B::writeTextB(QString s)
              {
              ui->textEdit->writePlainHTML(s);
              }@

              1 Reply Last reply Reply Quote 0
              • A
                Anonymous last edited by

                Could you give me an example?

                EDIT: You already added it, thanks.

                1 Reply Last reply Reply Quote 0
                • T
                  theknight47 last edited by

                  Ok,

                  Try it and give me updated for any further help.

                  1 Reply Last reply Reply Quote 0
                  • A
                    Anonymous last edited by

                    It works only one way...

                    @
                    // This one works... When I emit signal from class B (mainwindow) it trigger function (writeTextA) in class A (character)
                    character.h
                    class Character : public QObject
                    {
                    Q_OBJECT
                    public:
                    Character(QString str);
                    ~Character();

                    public slots:
                    void writeTextA(QString s);
                    };

                    character.cpp
                    void Character::writeTextA(QString s)
                    {
                    qDebug() << s;
                    }

                    mainwindow.h
                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();

                    signals:
                    void test(QString s);
                    };

                    mainwindow.cpp in constructor
                    Character ch("test");
                    connect(this, SIGNAL(test(QString)), &ch, SLOT(writeTextA(QString)));
                    emit test("abce");
                    @

                    @
                    // Does not work... It compile but nothing happens...
                    character.h
                    class Character : public QObject
                    {
                    Q_OBJECT
                    public:
                    Character(QString str);
                    ~Character();

                    signals:
                    void testF(QString s);
                    };

                    character.cpp in constructor
                    emit testF("test");

                    mainwindow.h
                    class MainWindow;
                    }

                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();

                    public slots:
                    void write(QString s);
                    };

                    mainwindow.cpp in constructor
                    Character ch("test");
                    connect(&cw, SIGNAL(testF(QString)), this, SLOT(write(QString)));

                    void MainWindow::write(QString string)
                    {
                    qDebug() << string;
                    }
                    @

                    1 Reply Last reply Reply Quote 0
                    • A
                      Anonymous last edited by

                      Added timer to class A and now works... I guess signal in A was emitted before class B initialized connection...

                      @QTimer::singleShot(1100, this, SLOT(update()));

                      void Character::update()
                      {
                      emit testF("test");
                      }
                      @

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post