0
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;


    QPushButton *trankil = new QPushButton("Clique&&Release", &w);
    QVBoxLayout *layoutrkl = new QVBoxLayout;
    layoutrkl->addWidget(trankil);
    //trankil->move(10,10);
    int resultat = 2;
    QLCDNumber *lcd = new QLCDNumber(&w);
    QLabel *lelabel = new QLabel("bonsoir");
    QPushButton *trankil2 = new QPushButton("Clique&&Release2", &w);
    layoutrkl->addWidget(trankil2);
    layoutrkl->addWidget(lelabel);
    layoutrkl->addWidget(lcd);

    QObject::connect(trankil, SIGNAL(clicked()),lcd, SLOT(display(resultat)));

    w.setLayout(layoutrkl);


    w.show();
    return a.exec();
}

The connect doesn't work and i don't understand why at all ! There is no problem about how it appear, but if i click the QPushbutton, the QLCD won't display resultat

Thanks for your help

PS : There is my includes :

#include <QApplication>
#include <QVBoxLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QLabel>
#include <QObject>
Pikate
  • 31
  • 5
  • 1
    The problem is here: `...SLOT(display(resultat))`. You should use rather a slot signature for the `SLOT` macro, i.e `SLOT(display(int))` instead. From the other hand, such connection will not work, as your signal has not parameters, but slot has. Please read https://doc.qt.io/qt-5/signalsandslots.html . – vahancho Jan 08 '21 at 08:01
  • Ok thanks you @vahancho ! But the signal "clicked" handle a bool ? if it's clicked or not ? So it should active the display slot ? As `resultat` is a int, it shouldn't works ? Thanks you ! – Pikate Jan 08 '21 at 08:13

1 Answers1

3

If you debug it.

QObject::connect: No such slot QLCDNumber::display(resultat)

display(resultat) is not a slot function.

You can try the following:

QObject::connect(trankil, &QPushButton::clicked, lcd, [&](){
            lcd->display(QString::number(resultat));
        });
  • It works ! Thanks you ! Now i would like to understand why it's working, and why it wasn't, could you help me ? :) @Derek – Pikate Jan 08 '21 at 08:13
  • @Pikate Derek uses a [lambda](https://en.cppreference.com/w/cpp/language/lambda) as adapter for the slot (to make the slot compatible to the signature of the signal). Please, note that the signature of the lambda is `()` (like the one of the signal). In its body, the lambda calls the actual "slot" with the additional required argument. (FYI: [SO: What is a lambda expression in C++11?](https://stackoverflow.com/q/7627098/7478597)) – Scheff's Cat Jan 08 '21 at 08:28
  • You are right, because the argument of slot "display" is "double", if you want to use SIGNAL(XXX), SLOT(xxx), the arguments of slot should be same as signal – Derek Zheng Jan 08 '21 at 08:35
  • @Pikate I'm not quite sure whether `[&]() { }` has really the matching signature for [QPushButton::clicked(bool)](https://doc.qt.io/qt-5/qabstractbutton.html#clicked). In the case that the compiler complaints, just change this to `[&](bool) { /* ... */ }`. – Scheff's Cat Jan 08 '21 at 08:36
  • @Scheff Ok perfect thanks you imma understand my mistake now ! :) thanks you ! – Pikate Jan 08 '21 at 08:38
  • @Pikate If this answer was helpful, please, don't forget to check it as [accepted answer](https://stackoverflow.com/help/someone-answers). ;-) – Scheff's Cat Jan 08 '21 at 08:40
  • @Scheff The signal is clicked(bool checked = false); so you can define the slot without argument – Derek Zheng Jan 08 '21 at 08:41
  • @DerekZheng I noticed the default argument but I'm always uncertain how precise the signature of the slot has to match. As this is something the compiler will just tell you if done wrong I did not yet manage to remember this correctly... ;-) – Scheff's Cat Jan 08 '21 at 08:46
  • If you read the source code supplied by qt. The SLOT SIGNAL, QT_PROPERTY.. is processed like serialization ,the compiler solve them as plain text.The compiler can't check it unless you use the IDE Qt creator. I use visual studio, Clion , there are no warning if signalName or slotName is error or not existing. – Derek Zheng Jan 11 '21 at 01:49