Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Signal generation inside function
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Signal generation inside function

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bareil76
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bareil76
        wrote on last edited by
        #3

        ok... and by that you mean?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vezprog
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Buckets
            wrote on last edited by
            #5

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

            ba ba ba
            ba na na na

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bareil76
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved