Defer a function call in Qt
I have a function in Qt 4.8 (windows), which has 3 things to do, like this:
void f()
{
//1. Do initialization
//2. Defer g() for next event (on the same thread) so that it may execute after f() is over
//3. Call h() which is time consuming
}
Now, I am not sure how to implement the 2nd step i.e. how to ensure that g()
is called after the call to f()
is complete, on the same thread (but on the different stack). I tried using QTimer
but if I provide a short time say 10 ms, the function g()
is called even when f()
has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.
qt qt4
add a comment |
I have a function in Qt 4.8 (windows), which has 3 things to do, like this:
void f()
{
//1. Do initialization
//2. Defer g() for next event (on the same thread) so that it may execute after f() is over
//3. Call h() which is time consuming
}
Now, I am not sure how to implement the 2nd step i.e. how to ensure that g()
is called after the call to f()
is complete, on the same thread (but on the different stack). I tried using QTimer
but if I provide a short time say 10 ms, the function g()
is called even when f()
has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.
qt qt4
What does this meanon the same thread (but on the different stack)
?
– Alex F
Jun 18 '14 at 14:01
What prevents you to callg()
afterf()
and notg()
fromf()
?
– vahancho
Jun 18 '14 at 14:11
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32
add a comment |
I have a function in Qt 4.8 (windows), which has 3 things to do, like this:
void f()
{
//1. Do initialization
//2. Defer g() for next event (on the same thread) so that it may execute after f() is over
//3. Call h() which is time consuming
}
Now, I am not sure how to implement the 2nd step i.e. how to ensure that g()
is called after the call to f()
is complete, on the same thread (but on the different stack). I tried using QTimer
but if I provide a short time say 10 ms, the function g()
is called even when f()
has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.
qt qt4
I have a function in Qt 4.8 (windows), which has 3 things to do, like this:
void f()
{
//1. Do initialization
//2. Defer g() for next event (on the same thread) so that it may execute after f() is over
//3. Call h() which is time consuming
}
Now, I am not sure how to implement the 2nd step i.e. how to ensure that g()
is called after the call to f()
is complete, on the same thread (but on the different stack). I tried using QTimer
but if I provide a short time say 10 ms, the function g()
is called even when f()
has not completed execution. So, relying on timers does not seem to be a reliable idea. Please help, any ideas are much appreciated.
qt qt4
qt qt4
asked Jun 18 '14 at 13:58
AarkanAarkan
1,45342244
1,45342244
What does this meanon the same thread (but on the different stack)
?
– Alex F
Jun 18 '14 at 14:01
What prevents you to callg()
afterf()
and notg()
fromf()
?
– vahancho
Jun 18 '14 at 14:11
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32
add a comment |
What does this meanon the same thread (but on the different stack)
?
– Alex F
Jun 18 '14 at 14:01
What prevents you to callg()
afterf()
and notg()
fromf()
?
– vahancho
Jun 18 '14 at 14:11
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32
What does this mean
on the same thread (but on the different stack)
?– Alex F
Jun 18 '14 at 14:01
What does this mean
on the same thread (but on the different stack)
?– Alex F
Jun 18 '14 at 14:01
What prevents you to call
g()
after f()
and not g()
from f()
?– vahancho
Jun 18 '14 at 14:11
What prevents you to call
g()
after f()
and not g()
from f()
?– vahancho
Jun 18 '14 at 14:11
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32
add a comment |
2 Answers
2
active
oldest
votes
Make g()
a slot or a Q_INVOKABLE
. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection)
.
add a comment |
If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:
f();
QApplication::processEvents();
g();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24287562%2fdefer-a-function-call-in-qt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Make g()
a slot or a Q_INVOKABLE
. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection)
.
add a comment |
Make g()
a slot or a Q_INVOKABLE
. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection)
.
add a comment |
Make g()
a slot or a Q_INVOKABLE
. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection)
.
Make g()
a slot or a Q_INVOKABLE
. Then use QMetaObject::invokeMethod(this, "g", Qt::QueuedConnection)
.
answered Jun 18 '14 at 18:20
Kuba OberKuba Ober
69.7k982186
69.7k982186
add a comment |
add a comment |
If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:
f();
QApplication::processEvents();
g();
add a comment |
If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:
f();
QApplication::processEvents();
g();
add a comment |
If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:
f();
QApplication::processEvents();
g();
If I understand correctly, it's needed simply to call g() after f(), but on the next message loop cycle, after all Qt events issued from f() would be processed. In this case I suggest the following trick:
f();
QApplication::processEvents();
g();
answered Nov 20 '18 at 18:33
RedSoftRedSoft
111111
111111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24287562%2fdefer-a-function-call-in-qt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does this mean
on the same thread (but on the different stack)
?– Alex F
Jun 18 '14 at 14:01
What prevents you to call
g()
afterf()
and notg()
fromf()
?– vahancho
Jun 18 '14 at 14:11
So, I want g() to be called from the next event stack of message loop which means that instead of calling g() at this time, I just want to post an event in message loop and call that later when current event processing is complete. I hope it clarifies the things.
– Aarkan
Jun 18 '14 at 14:32