I have two buttons, every button has one slot function, i clike two buttons, i want the frist slot function to be executed?
-
i have two buttons, every button has one slot function, i clike two buttons, i want the frist slot function to be executed,when the frist button's slot function finish,the second button is
executed, how to do it ? -
Hi,
You don't have to do anything special. Qt already waits for the first slot to finish before starting the second slot.
-
can i control the second button not to execute slot function,if i clike the second button and i write the second button's slot function.
-
Sorry, I don't quite understand what you want to achieve. Can you rephrase your question? Or post some code to show us what you want?
-
You can simply disable your second button when the first slot is running. Or you can set a flag to let the second slot exit when entered.
-
i hope to do it by event or message.
-
[quote author="foxgod" date="1382602114"]i hope to do it by event or message.[/quote]Why? Event/message is more complicated.
-
i need to do it by event or message. i wrote just now is only a example,it's not my real project.my project need use event or message.please tell me.
-
if i have three button or more ,whether i need set many flags??
-
Try "QObject::blockSignals()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#blockSignals
-
Hi,
first of all, slots for UI are typically executed by the main thread. This means, If you have:
button1 --> slot1
button2 --> slot2
button3 --> slot3and the user clicks
button1 --> button3 --> button2
the slots are executed in this order
slot1 --> slot3 --> slot2if the slots are of objects from different threads, you have no control, which is executed first...
-
bq. if the slots are of objects from different threads, you have no control, which is executed first…
I think if signals are from diffrent thread than the receiving object by default with Qt::AutoConnection they are queued, meaning that they wait till the app returns to the receiver's thread but the execution is in the same order of their sending ... but with Qt::DirectConnection the slot is invoked imediatelly
-
That's why Gerolf wrote:
bq. if the slots are of objects from different threads
(emphasis mine)
If the objects' event queues run in different threads, you have absolutely no control over the order of execution.
-
bq. If the objects’ event queues run in different threads, you have absolutely no control over the order of execution.
I think you missed the part with Qt::DirectConnection: when this happens, the signals and slots mechanism is totally independent of any thread loop, and slot is executed immediately as any function call ... and anyway even the case when slots are in different threads and signals are queued and the slot function is executed later in an undetermined order, this is reduced to the case of a synchronization case between threads ...
:)
-
[quote author="foxgod" date="1382600959"]i have two buttons, every button has one slot function,the frist
button's slot function's execution needs some time,so i want to prevent the second buuton's slot function's execution,when i clike the second button quickly after the frist buttons cliked.[/quote]You want each button to operate completely on its own before another button gets clicked? That's what it sounds like you're wanting
-
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);
connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
i set a flag to prevent the second button to execute its slot function ,but it's no effect。
-
[quote author="Gerolf" date="1382615946"]Hi,
first of all, slots for UI are typically executed by the main thread. This means, If you have:
button1 --> slot1
button2 --> slot2
button3 --> slot3and the user clicks
button1 --> button3 --> button2
the slots are executed in this order
slot1 --> slot3 --> slot2if the slots are of objects from different threads, you have no control, which is executed first...[/quote]
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);
connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
i set a flag to prevent the second button to execute its slot function ,but it's no effect。
-
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()),Qt::DirectConnection);
connect(signalMapper,SIGNAL(mapped(QObject*)),this,SLOT(setBackground(QObject*)),Qt::DirectConnection);
i set a flag to prevent the second button to execute its slot function ,but it's no effect。
[quote author="SchwenLarson" date="1382641312"][quote author="foxgod" date="1382600959"]i have two buttons, every button has one slot function,the frist
button's slot function's execution needs some time,so i want to prevent the second buuton's slot function's execution,when i clike the second button quickly after the frist buttons cliked.[/quote]You want each button to operate completely on its own before another button gets clicked? That's what it sounds like you're wanting[/quote]
-
foxgod, how about you disable all the buttons when you enter your slot, and then re-enable them when your slot finishes? This also tells the user that the program is busy.
@
QList<QPushButton*> allButtons;void slot1() {
for (int i = 0; i < allButtons.size(); ++i)
allButtons[i]->setEnabled(false);// Do work for (int i = 0; i < allButtons.size(); ++i) allButtons[i]->setEnabled(true);
}
void slot2() {
for (int i = 0; i < allButtons.size(); ++i)
allButtons[i]->setEnabled(false);// Do work for (int i = 0; i < allButtons.size(); ++i) allButtons[i]->setEnabled(true);
}
@[quote author="NicuPopescu" date="1382630065"]I think you missed the part with Qt::DirectConnection: when this happens, the signals and slots mechanism is totally independent of any thread loop, and slot is executed immediately as any function call ... [/quote]...which usually means your slot is now executing in the wrong thread.