What are the differences between qt's slot function and regular function during execution?
-
Hello everyone,
I have a function that controls the output of the collection card, which can be successfully executed in the constructor of the widget, but fails in the slot function of qpushButton. This function does not require any other parameters.//toZero(); //QtConcurrent::run(toZero); //QTimer::singleShot(100, toZero); //All three writing methods were successfully executed ... connect(zero, &QPushButton::clicked, this, []() { toZero(); });//All three writing methods failed to execute
-
There is no difference between a slot function and a 'normal' member function. Provide more code.
-
@Christian-Ehrlicher
The function is hardware related, so a more detailed process cannot be provided. I can verify that tozero was executed.
In addition,I found that the execution was successful in the slot function of QDdoubleSpinBox:: valueChanged, but failed in the slot function of QPushButton:: clicked. Are there any differences in the execution process between these two slot functions? -
@John-Van
Hardware or not, it remains the case that there is nothing special about a function used as a slot. It is just a plain, normal C++ function, which can be called from a signal connection or directly with no difference. If you find a difference there is something different about where or when you are calling it, not the fact that it is a slot. You may also omit theslots
declaration and still use it/any function for a signal connection.I don't know what you mean by "I found success in QDoubleSpinBox:: valueChanged and failure in QPushButton:: clicked". If you have some difference you want to highlight please supply some minimal example which illustrates it.
-
There is no difference between slot functions and regular functions (when used in connections). The only difference is that
moc
will enter slot functions into a list where you can look up all slots during runtime by using a string. This is related to the old way of Qt doing slots. It is not used anymore.However,
valueChanged
andclicked
are not slots, but signals. Signals are C++ functions which are auto-generated by Qt (usingmoc
). This is the actual implementation logic how to call different 'slots' (and here 'slot' means both functions declared as slots as well as regular functions used as slot).@John-Van said in What are the differences between qt's slot function and regular function during execution?:
In addition,I found that the execution was successful in the slot function of QDdoubleSpinBox:: valueChanged, but failed in the slot function of QPushButton:: clicked.
This most likely means that your
connect
is wrong. Please provide the code for the two different connects if you want help. -
this
connect(zero, &QPushButton::clicked, this, []() { toZero(); })
should fail to compile, is toZero a free floating function ?