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

[SOLVED] Signal generation inside function



  • Hi,

    I want to generate a signal inside a function. BUT the catch is that the name of the signal depends on the arguments passed to the function.

    How can I acheive that.

    For example the following function would generate
    if id==1.... then

    @void MainWindow::startButtonsHandling(int id){

    QString name_StartSingle("start_signal_%1");
    
    connect(this, SIGNAL(start_signal_1()),plugNPlay,SLOT(pulses_go1()));
    

    }@

    if id was 2 we would have start_signal_2() and pulses_go2()

    Usually I use the findchild function but in this case...?

    thanks for your help.

    Will I need to pass an argument to the signal and slots instead?



  • Hi, seems that you have misunderstanded the function of QObject::connect().



  • ok... and by that you mean?



  • The signals must be defined in the header of the class.

    If you have:
    @
    // emitter class
    signals:
    void start_signal_1();
    void start_signal_2();
    void start_signal_3();
    void start_signal_4();

    // some other class accepting the slots...
    private slots:
    void pulse_go1();
    void pulse_go2();
    void pulse_go3();
    void pulse_go4();
    @

    why not just switch /case?
    @
    void MainWindow::startButtonsHandling(int id)
    {
    switch (id){
    case 0:
    connect(this, SIGNAL(start_signal_1()),plugNPlay,SLOT(pulses_go1()));
    break;
    case 1:
    connect(this, SIGNAL(start_signal_2()),plugNPlay,SLOT(pulses_go2()));
    break;
    case 2:
    connect(this, SIGNAL(start_signal_3()),plugNPlay,SLOT(pulses_go3()));
    break;
    case 3:
    connect(this, SIGNAL(start_signal_3()),plugNPlay,SLOT(pulses_go3()));
    break;
    }
    }
    @

    Rather messy, but it would work. I believe the signals and slots must be predefined for usage.



  • why would you only want to connect one signal? why not connect them all? and just activate the one the id matches?



  • Yes this is what I did,

    I did not use a switch because that makes the coding dependant on the number of "id" I have.

    What I did is connected them all and sent them to a function using signalMapper that returns the "id" of the channel. That function then start the corresponding funciton.

    Thanks for your help


Log in to reply