Invoke a function after main
-
int main(int argc, char *argv[]) { uint32_t discovered = 0; QApplication a(argc, argv); MainWindow w; w.show(); //do some stuff return a.exec(); }
I want to invoke a function after return a.exec(); - automatically , not pressing any buttons or something.
How should I do it properly? -
@jenya7 said in Invoke a function after main:
How should I do it properly?
For example with QTimer::singleShot()
-
@jenya7 Really don't understand what is so difficult about it:
int main(int argc, char *argv[]) { uint32_t discovered = 0; QApplication a(argc, argv); MainWindow w; w.show(); //do some stuff auto result = a.exec(); someFunction(); return result; }
Also, your title is wrong: you can't call anything after main...
-
@Christian-Ehrlicher said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
How should I do it properly?
For example with QTimer::singleShot()
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTimer::singleShot(600000, &app, SLOT(quit())); ... return app.exec(); }
It's executed before return app.exec(); and I need after.
Wait a minute - 600000 - how do I know what time to set?
app.exec() - it's a thread? what 's going on after? can do something in exec() ? -
@jsulm said in Invoke a function after main:
@jenya7 Really don't understand what is so difficult about it:
int main(int argc, char *argv[]) { uint32_t discovered = 0; QApplication a(argc, argv); MainWindow w; w.show(); //do some stuff auto result = a.exec(); someFunction(); return result; }
Also, your title is wrong: you can't call anything after main...
It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();
-
@jenya7 said in Invoke a function after main:
It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();
How did you verify that?
As soon as exec() terminates the flow continues with next statement.
Maybe you app is crashing? -
@jsulm said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();
How did you verify that?
As soon as exec() terminates the flow continues with next statement.
Maybe you app is crashing?I set a break point on auto result = a.exec(); . The next step - break point vanished and I don't have the option to go on with break point. But I see GUI - it prints all the previous messages (till someFunction()) and all operates well. May be someFunction() was executed but it didn't print anything.
-
@J-Hilk said in Invoke a function after main:
question:
do you actually know what
a.exec()
is and does?and if you think you do, can you please post it here quickly ?
I want to make sure, we're on the same page here
That's what I'm tying to understand.
Any way I could connect to some event like a.running() or something. -
@jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?
"Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
If you simply want to execute something after exec() then what I posted is the way to go... -
@jsulm said in Invoke a function after main:
@jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?
"Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
If you simply want to execute something after exec() then what I posted is the way to go...Yes but someFunction() not printing important messages.
auto result = a.exec(); someFunction();
This way it does print but blocks GUI
someFunction(); auto result = a.exec();
-
@jenya7 said in Invoke a function after main:
This way it does print but blocks GUI
someFunction();So why does someFunction() block? What are you doing inside this function? What's your final goal? What should this function do?
-
@jenya7 Did you bother to read the link I gave you?
Probably not, so I will spend my time to explain: exec() is a BLOCKING call (it runs the Qt event loop). It blocks until you terminate the Qt event loop (this happens when you close the last window of your app). Then everything else after exec() is executed. So, expecting someFunction() to be executed after exec() WHILE your app is running is completely wrong! Also the break point after exec() will be hit when you close your app! -
@Christian-Ehrlicher said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
This way it does print but blocks GUI
someFunction();So why does your function block at all? What's your final goal? What should this function do?
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. It goes through a list of remote devices and tries to connect (to discover).
-
@jenya7 said in Invoke a function after main:
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I
When it blocks then it does not.
And when you would have read and used my first answer, all your problems would have been gone... but I give up here.
-
@Christian-Ehrlicher said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I
When it blocks then it does not.
And when you would have read and used my first answer, all your problems would have been gone... but I give up here.
OK. But I have two questions
QTimer::singleShot(600000, &app, SLOT(quit()));
First - What time do I set - 600000? How long it takes to a.exec()?
Second - How do I put thisdiscovered = m_sensor.DiscoverConcurrent(0, 0, SYS_DISC_MODE_FILE);
Into this
QTimer::singleShot(600000, &?, ???);
-
@jenya7 said in Invoke a function after main:
What time do I set
You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
"How do I put this" - you do that in the slot connected to the timer... -
@jsulm said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
What time do I set
You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
"How do I put this" - you do that in the slot connected to the timer...I see. QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().