I'm new to C++, and I'm learning Qt.
Consider this line:
connect(ui->horizontalSlider, &QSlider::valueChanged,
ui->progressBar, &QProgressBar::setValue);
What I don't understand is why you pass the address of a static (is it static?) method valueChanged ( &QSlider::valueChanged
) instead of the current object method address &ui->horizontalSlider->valueChanged
. Although I can use this second option that works too.
You pass the address of the member function which should be called. The member function is not static though, that means it needs an object to work on.
class MyClass
{
void aFunction();
}
here MyClass::aFunction
is a member function.
What the compiler creates is similar to this
class MyClass
{
static void aFunction(MyClass *this);
}
So whenever you call aFunction
like my_instance.aFunction()
the this
pointer will be handed over implicitly, so the call basically becomes MyClass::aFunction(&my_instance)
. As a result the address of aFunction
is the same for every instance of MyClass
. Yet to execute aFunction
you need an instance of MyClass
.
This is why in your case you have to provide connect
with both the instance ui->horizontalSlider
as well the function to be called on it &QSlider::valueChanged
.
What I described is an oversimplification so take it with a grain of salt. Moreover when you have virtual functions things change.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.