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. [SOLVED] Access function in another class
QtWS25 Last Chance

[SOLVED] Access function in another class

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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
          1
          • T Offline
            T Offline
            theknight47
            wrote on last edited by
            #5

            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
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Could you give me an example?

              EDIT: You already added it, thanks.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                theknight47
                wrote on last edited by
                #7

                Ok,

                Try it and give me updated for any further help.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

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