How connect customWiget Signals and Slots
-
My app has many custom widgets each has their own signals, but I need one of these customwidget control the other other ones, the tutorials always use qt creator with included widgets, when I use the Edit signal /slots editor, in the end the IDE seeks the signal function of the customwidget in the mainwindow, when this signal is defined in the own file of the customwidget, I dont know then of connect the customwidgets.
I use the customwiget in my project only by including the header in the mainwindow header file.
Honestly I dont understand very well of do this connections, the tutorials are ambiguos. Only the command connect is nedded?, of work in union with the ui Edit signal /slots editor, or both do the same??
In QT Creator while run the appa this message appear, Warnings(???)
QObject::connect: No such signal QCustomPlot::rangeChanged() in ./ui_mainwindow.h:439
QObject::connect: (sender name: 'rule')
QObject::connect: (receiver name: 'customPlot1')Where rangeChanged is the name of Signal f QCustomPlot widget send
-
Hi
When using Designer, to connect signals to slots its
often use an auto connect feature.When you add a slot a function is added that is in the format
On_WidgetName_SignalName()When we run the app, inside setupUI the function
QMetaObject::connectSlotsByName(MainWindow);
is called.
It will (try) find a widget and a matching slot and auto connect them.However, this can easy break if you rename the widget or the slot function name.
For that reason, its recommended to simply always connect the widget manually.
for that, you only need to use the connect command.
and it works too with custom widgets like custom plot. -
Hi
When using Designer, to connect signals to slots its
often use an auto connect feature.When you add a slot a function is added that is in the format
On_WidgetName_SignalName()When we run the app, inside setupUI the function
QMetaObject::connectSlotsByName(MainWindow);
is called.
It will (try) find a widget and a matching slot and auto connect them.However, this can easy break if you rename the widget or the slot function name.
For that reason, its recommended to simply always connect the widget manually.
for that, you only need to use the connect command.
and it works too with custom widgets like custom plot. -
@LCorona
Hi
There is 2 syntaxes.
Its a good idea to practice the new one as it has benefits.
https://wiki.qt.io/New_Signal_Slot_SyntaxIts normally not very hard
it's normally something likeconnect(ui->customplotName, &QCustomPlot::SignalName, this, &QMainWindow::slotName );
the & is important. Make sure it does not help you and add () to signalname / slot name
Also make sure the slot has the same parameters as the signal has.
-
@LCorona
Hi
There is 2 syntaxes.
Its a good idea to practice the new one as it has benefits.
https://wiki.qt.io/New_Signal_Slot_SyntaxIts normally not very hard
it's normally something likeconnect(ui->customplotName, &QCustomPlot::SignalName, this, &QMainWindow::slotName );
the & is important. Make sure it does not help you and add () to signalname / slot name
Also make sure the slot has the same parameters as the signal has.
-
@mrjj
Excellent!!, works really fine in both syntaxes, thank you, problem resolved.In the end the solution was very easy, only one line of code nedded.
@LCorona
Super.
The main difference between the SIGNAL() and SLOT() macros and the
new syntax is when errors are detected.
The macros will accept anything when you compile and first fail when app runs mostly
by you discovering that nothing happens.The new syntax, however, gets help from the compiler and will catch errors at compile time.
For a big app, its very helpful as if you rename some slot or similar, you get told at once by the compiler.
Also the new syntax allows to use lambdas which can be very handy.
A lambda is just a nameless in place function.
LikeQObject::connect(timer, &QTimer::timeout, [this](){ .... });
the part between the {} is like a slot but you dont need to define in .h etc.
This can sometimes be very handy :)
-
@LCorona
Super.
The main difference between the SIGNAL() and SLOT() macros and the
new syntax is when errors are detected.
The macros will accept anything when you compile and first fail when app runs mostly
by you discovering that nothing happens.The new syntax, however, gets help from the compiler and will catch errors at compile time.
For a big app, its very helpful as if you rename some slot or similar, you get told at once by the compiler.
Also the new syntax allows to use lambdas which can be very handy.
A lambda is just a nameless in place function.
LikeQObject::connect(timer, &QTimer::timeout, [this](){ .... });
the part between the {} is like a slot but you dont need to define in .h etc.
This can sometimes be very handy :)