Access variable from a slot
-
I can't believe that I have never done this in all the years ..
main: emits to another "tell me page"
other says "2"main dbus handler says
answered = true;
answer = 2;main loop:
never sees answered
never sees answermain.h
class
volatile bool answered
volatile int answer
;What am I missing
~ -
I can't believe that I have never done this in all the years ..
main: emits to another "tell me page"
other says "2"main dbus handler says
answered = true;
answer = 2;main loop:
never sees answered
never sees answermain.h
class
volatile bool answered
volatile int answer
;What am I missing
~ -
I was tring to be succient
record1 ()
{
...
if ((asbuiltEnabled)) {
answered = false;
emit action ("toolbar", "1whatpage");
QElapsedTimer timer;
for (timer.start (); !timer.hasExpired (2000); ) {
if (answered == true) {
*dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
answered = false;
break;
}
}
if (timer.elapsed () > 1990)
asbuiltEnabled = false;
}
*dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
emit action ("toolbar", QString ("1record %1").arg (page[1]));
page[1] = 0;
}
}void MainWindow::dbusAction (const QString &nickname, const QString &text)
{
..
else if (text.contains ("page")) {
*dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
page[text.left (1).toInt ()] = text.right (1).toInt ();
answered = true;
}
} -
I was tring to be succient
record1 ()
{
...
if ((asbuiltEnabled)) {
answered = false;
emit action ("toolbar", "1whatpage");
QElapsedTimer timer;
for (timer.start (); !timer.hasExpired (2000); ) {
if (answered == true) {
*dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
answered = false;
break;
}
}
if (timer.elapsed () > 1990)
asbuiltEnabled = false;
}
*dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
emit action ("toolbar", QString ("1record %1").arg (page[1]));
page[1] = 0;
}
}void MainWindow::dbusAction (const QString &nickname, const QString &text)
{
..
else if (text.contains ("page")) {
*dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
page[text.left (1).toInt ()] = text.right (1).toInt ();
answered = true;
}
} -
Without doubt the event loop is my (silly) problem: thanks
My code is formatted, pasting eats whitespace. How to paste? as :"?@jamat13 this button adds a code block for you:
alternatively can you also manually type ``` at the beginning of your code segment and at the end, if you don't want to use the button. In general the textinput supports markdown-esk syntax most of the time. Doesn't work for title and subtitle, but Codeblocks, Lists, Bold/Cursive, links etc are supported
-
I was tring to be succient
record1 ()
{
...
if ((asbuiltEnabled)) {
answered = false;
emit action ("toolbar", "1whatpage");
QElapsedTimer timer;
for (timer.start (); !timer.hasExpired (2000); ) {
if (answered == true) {
*dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
answered = false;
break;
}
}
if (timer.elapsed () > 1990)
asbuiltEnabled = false;
}
*dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
emit action ("toolbar", QString ("1record %1").arg (page[1]));
page[1] = 0;
}
}void MainWindow::dbusAction (const QString &nickname, const QString &text)
{
..
else if (text.contains ("page")) {
*dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
page[text.left (1).toInt ()] = text.right (1).toInt ();
answered = true;
}
}@jamat13
Do not use signals/slots for either receiving an immediate reply from any attached slot(s) nor for "waiting" for something to happen as a consequence. That is not what they are intended for --- they are a "notify and continue" paradigm.If you want a "response" instead either use a direct function call or in the signaller emit the signal, save whatever state is necessary and continue to the Qt event loop, while the slot emits another signal when it is finished which the originally signalling side acts on in its own slot when it arrives. Use a
QTimer
at the signalling side if you need to know that the slot has not "replied" within a period of time.answered = false; emit action ("toolbar", "1whatpage"); QElapsedTimer timer; for (timer.start (); !timer.hasExpired (2000); ) { if (answered == true) { *dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush (); answered = false; break; }
Your loop is very "busy": it burns your CPU and completely blocks the thread it is in. Since it does not enter the Qt event loop it does not allow normal processing to continue. Behaviour of your code probably depends on whether attached slot(s) run in the same thread or a different one.