Lets say I have a main window with a slider and a widget inside that window with a method called setValue(int)
. I'd like to call this method every time the value of the slider has changed.
Is there any practical difference between the two following ways of achieving it:
1
void MainWindow::on_slider_valueChanged(int value)
{
ui->widget->setValue(value);
}
2
// somewhere in constructor
connect(ui->slider, SIGNAL(valueChanged(int)), ui->widget, SLOT(setValue(int)));
For me the first approach looks better, because it possibly avoids some overhead related to signals and slots mechanism and also, allows me to process the value
before sending it to widget
, if there's a need for it.
Are there any scenarios where the second solution is better?
MainWindow::on_slider_valueChanged
called? Thats the point. Signals provide an abstraction of events and their handlers. Dont care about performance (atleast if it affects your design) if you dont have a bottleneck, especially not with GUI code. GUI code is absolutely not performance relevant. – KenwrickMainWindow::on_slider_valueChanged
is called becauseQMetaObject::connectSlotsByName()
automatically generates this connection. Performance is not my concern here, but the design is. I'm trying to find out why one is better than the other, if it makes any difference at all. – Manufacturer