Can someone explain this syntax to me?
I'm using the terminal example to learn how to make a terminal in qt, and I don't understand this syntax:
connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &SettingsDialog::showPortInfo);
I understand that the serialPortInfoListBox (a combo box) is the sender. I don't understand this part:
This is my signal argument, but I don't get what it's saying. Is this like a really fancy pointer to a function? I would go to the documentation but idk where to look for this. The (QComboBox::*)(int) is especially confusing because it essentially points to nothing, right? Shouldn't a scope have to have something after the :: ?
@CynicalPenguin Hi, friend. Welcome.
First, this way is convert function type.
We will see signals in
void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)
There are two signals have the same function name, but parameters type is different.
In Qt4. we used
SLOTin connect, like
connect(cbox,SIGNAL(currentIndexChanged(int index)),this,SLOT(slot_function(int index)); connect(cbox,SIGNAL(currentIndexChanged(const QString &text)),this,SLOT(slot_function(const QString &text));
In compile time. The Qt use compare string ways to connect signal and slots. It is In Qt4, using the
SLOTwe do not need to distinguish these signal has same function and different parameters.
But in Qt5. we can use pointer of signal and slot function in connect. like
connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use index connect(cbox,&QComboBox::currentIndex,this,&Window::slot_function); /// I want use text
But, Qt don't know which function you want to use. so when we use pointer of function in connect, if signal or slot has overloaded. we should convert pointer type of function to tell Qt which function we want to connect.
so we used
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),tell Qt, I want to use
About the convert syntax, like
- static_cast c++ convert type keyword
- returnTypr means function return type, in signal and slot, most is void
- ClassName::* means which class of the function in.
- the paramters types in function, one, two, three...
- the function pointer.
Just like convert pointer type
int *pint； void* pVoid; pVoid = static_cast<void *>(pint); // convert int* => void* pint = static_cast<int *>(pVoid); // convert void* => int*