[SOLVED] Access function in another class
-
wrote on 3 Mar 2015, 10:01 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);
}@ -
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 ?
-
wrote on 3 Mar 2015, 10:22 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)
appleI'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...
-
You can work with Signals/Slots of Qt to make it clean separation between Class A and B and still communicate each other.
-
wrote on 3 Mar 2015, 11:06 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 Bpublic 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);
}@ -
wrote on 3 Mar 2015, 11:07 last edited by
Could you give me an example?
EDIT: You already added it, thanks.
-
wrote on 3 Mar 2015, 13:29 last edited by
Ok,
Try it and give me updated for any further help.
-
wrote on 3 Mar 2015, 15:16 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_OBJECTpublic:
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_OBJECTpublic:
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;
}
@ -
wrote on 3 Mar 2015, 15:44 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");
}
@
7/9