I'm working on a QT project. I was wondering if is possible to create a connection
using two signals to execute a method.
I have three classes: A, B and C. Class A emit a signal when a button is pressed (connected in Class C), also in Class C a QProcess
is created (from an instance of class B).
In class C I have a connect
to get the output of the QProcess
. Also, I have another connect
to execute a method when a button is pressed (signal emitted in Class A).
So, I need to modify the current behavior, the doSomething()
method should be executed when the QProcess output is ready, similar to an if
statement:
if(signalA && signal B){
do something...
}
This is the current code:
//This signal will get the output of a Qprocess
connect(objectC,&QProcess::readyReadStandardOutput, [=] (){
QString out = objectB->readAllStandardOutput();
qDebug() << out;
});
//When a button in Class A is pressed doSomething() is executed
//but now I must wait until the above signal generates an output.
connect(objectA, ClassA::buttonPressed, [=] (){
doSomething();
});
objectC -> run(args); // This line execute an external process
any Ideas on how to achieve this?
I found on stackoverflow
that in QT we can have two signals in a single connect
, like this:
connect(this,SIGNAL(someSignal()),this,SIGNAL(anotherSignal()));
but I don't know how to adapt this to my problem.
The flow of my QT application is the following:
A
QProcess
is created to launch an external .exe fileThe
QProcess
return aQString
At any moment the user can press a button that will execute a specific method (
doSomething()
) but needs the output of theQProcess
.The big issue is that sometimes the user press the button before the QProcess ends, so I cannot execute the
doSomething()
correctly.The desired behavior is: if the user press the button to execute
doSomething()
, first I must wait until the output of the QProcess is ready. So if I press the button and if theQProcess
takes e.g 10s to finish, thedoSomething()
should be executed after this 10s.
CodePudding user response:
You can store the state of both events and check them both whenever one of them changes.
// These should probably be defined in the header of your class.
bool processFinished = false;
bool buttonClicked = false;
void checkState() {
if (buttonClicked && processFinished) {
doSomething();
}
}
connect(objectC,&QProcess::readyReadStandardOutput, [=] (){
...
processFinished = true;
checkState();
});
connect(objectA, ClassA::buttonPressed, [=] (){
...
buttonClicked = true;
checkState();
});