Questions about slots

  • Hi ! I'm sorry, in advance, for my bad english.

    I'm trying to run a program which use slots. Compilation works but when I run the program, he wait and don't do anything.

    Here we have the function :

    void Fenetre::Commencer()
        main_display->setText("Bienvenue dans jhqqjhfqjf, le nouveau jeu video de la mort qui tue !\n");
        boutons[1]->setText("Quitter ? Deja ?");
        boutons[2]->setText("Bouton vers la victoire");
        boutons[2]->setEnabled(false); //Parce que c'est marrant
        connect(boutons[0], SIGNAL(pressed()), this, SLOT(Etape(0,0)));
        connect(boutons[1], SIGNAL(pressed()), this, SLOT(reject()));

    And his declaration on my class :

    public slots:
        void Etape(int from, int to);

    Can I put arguments in a slot ?
    Thank's !

  • Moderators

    Hi @saxoalex, and welcome!

    @saxoalex said in Questions about slots:

    Can I put arguments in a slot ?

    No. A slot can only get arguments from the signal.

    connect(boutons[0], SIGNAL(pressed()), this, SLOT(Etape(0,0)));

    This connection is wrong because:

    • The pressed() signal has no arguments, so it can only be connected to slots that also have no arguments.
    • You cannot write values/numbers in the connection. You can only write data types (such as "bool", "int", or "QString"). Example:
      • connect(slider, SIGNAL(valueChanged(int)), spinbox, SLOT(setValue(int))); // slider is a QSlider, spinbox is a QSpinBox
    connect(boutons[1], SIGNAL(pressed()), this, SLOT(reject()));

    This connection is OK.

    Compilation works but when I run the program, he wait and don't do anything.

    First, please read

    You are using String-based connections, so your compiler cannot check them. You can change to Functor-based signals and let your compiler do the checks.

    // OK
    connect(boutons[1], &QPushButton::pressed,
            this, &Fenetre::reject);
    // Compiler error. The signal and slot are not compatible
    connect(boutons[0], &QPushButton::pressed,
            this, &Fenetre::Etape);
    // OK -- using a Lambda Expression
    connect(boutons[0], &QPushButton::pressed, [this] {

  • It work ! I understand.

    Thank you very much for this information !

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.