Solved 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:
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
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
QComboBox
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
SIGNAL
andSLOT
in connect, likeconnect(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
SIGNAL
andSLOT
we 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 useindex
nottext
.About the convert syntax, like
static_cast<returnType (ClassName::*)(parameter1Type,parameter1Type)>(pointerOfFunction)
- 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*
-
@CynicalPenguin Hi, friend. Welcome.
First, this way is convert function type.
We will see signals in
QComboBox
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
SIGNAL
andSLOT
in connect, likeconnect(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
SIGNAL
andSLOT
we 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 useindex
nottext
.About the convert syntax, like
static_cast<returnType (ClassName::*)(parameter1Type,parameter1Type)>(pointerOfFunction)
- 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*
-
Gotcha. Thanks!
-
@joeq This was very helpful to me. Thanks for the complete and detailed explanation of the syntax involved. Your answer should be included in the Qt All Classes webpage for QComboBox::currentIndexChanged(int index) and QComboBox::currentIndexChanged(const QString &text).
Since QComboBox::currentIndexChanged(const QString &text) is overloaded, the previous syntax doesn't seem to work anymore.
-
@jjlee said in Can someone explain this syntax to me?:
the previous syntax doesn't seem to work anymore
It does, but you need to tell the compiler which version you want to use. For that you can use https://doc.qt.io/qt-5/qtglobal.html#qOverload macro.