Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do SIGNAL or SLOT know which object I am referring to in a connect function?



  • I'm coming from C++ area so may be a QT idiosyncrasy, but I noticed something strange to me and wanted to ask you about it.

    In this code:

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
            ui->progressBar, SLOT(setValue(int)));
    

    how do SIGNAL or SLOT know which object I am referring to? Is this some sort of a macro that looks up the previous argument for memory address? How does it inferr that valueChanged is a function of horizontalSlider, or that setValue is a function of progressBar?

    Shouldn't it look like this?

    connect(ui->horizontalSlider, SIGNAL(ui->horizontalSlider->valueChanged(int)),
            ui->progressBar, SLOT(ui->progressBar->setValue(int)));
    

    Thanks for any explanations :)


  • Moderators

    @TheTobruk said in How do SIGNAL or SLOT know which object I am referring to in a connect function?:

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
    ui->progressBar, SLOT(setValue(int)));

    SIGNAL() / SLOT() are macros which return a char*, which is then used by Qt (MOC) for static lookup in a list. So the (cleaned up) method name is searched in the meta-object of the object you passed in the parameter before.

    You may also checkout the new signa/slot syntax (introduced with Qt5), which also accepts function pointers.
    The old syntax is basically the same, but the "dereferencing" of the function is done each time during runtime and also string based.

    connect(ui->horizontalSlider, SIGNAL(ui->horizontalSlider->valueChanged(int)),
    ui->progressBar, SLOT(ui->progressBar->setValue(int)));

    following your thinking this would actually call the method, which isn't intented.


Log in to reply