One signal to many slots (and not the other way around

  • Dear all,

    Lets say for example I have a push button that, upon being clicked, clears five doubSpinBox'es.

    I could manually do this as follows:
    @ connect (ui->pushButton, SIGNAL (clicked()), ui->doubleSpinBox, SLOT(clear()));
    connect (ui->pushButton, SIGNAL (clicked()), ui->doubleSpinBox_2, SLOT(clear()));
    connect (ui->pushButton, SIGNAL (clicked()), ui->doubleSpinBox_3, SLOT(clear()));
    connect (ui->pushButton, SIGNAL (clicked()), ui->doubleSpinBox_4, SLOT(clear()));
    connect (ui->pushButton, SIGNAL (clicked()), ui->doubleSpinBox_5, SLOT(clear()));

    Suffice it to say, it starts becoming untidy and cluttered repeating the same manual connect statements.

    I want to therefore do something like this. I want to code one connect statement that refers to a custom slot called "clear_values" which in turn clears all the double spin boxes:

    @connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(clear_values()));

    void MainWindow::clear_values()

    However the code above doesnt work, why not?

    PS: I am NOT looking for Signal mapper. Signal mapper is even more tedious and only comes in handy when there are many signals to one or more slots.

  • What errors are you getting? This code should work.

  • Moderators


    Is clearValues() declared as a slot?

    After you added clearValues() as a slot, did you clean your project, re-run qmake, and rebuild your project?

    P.S. Always describe what you mean by "doesn't work", and provide any error messages that you see. The term "doesn't work" by itself cannot help your readers figure out the problem.

Log in to reply