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 and SLOTS
Forum Updated to NodeBB v4.3 + New Features

[Solved] SIGNAL and SLOTS

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.6k 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.
  • D Offline
    D Offline
    DarkRoast
    wrote on last edited by
    #1

    Hi all

    I am wondering about SIGNAL and SLOTS

    I have the following code:

    @ for(int i = 0; i < NUMBERS; i++)
    {
    NumbersLabel[i] = new QLabel(" N" + QString::number(i+1));

        NumbersSpinBox[i] = new QSpinBox;
        NumbersSpinBox[i]->setRange(MINNUMBER, MAXNUMBER);
        NumbersSpinBox[i]->setSpecialValueText(" ");
    
        connect(NumbersSpinBox[i], SIGNAL(editingFinished()), this, SLOT(NumberEditingFinished(i)));
    
        hLayout->addWidget(NumbersLabel[i]);
        hLayout->addWidget(NumbersSpinBox[i]);
    }@
    

    I know it doesn't work... But it illustrates what I want.

    I want to connect editingFinished() from all my SpinBoxe's to the SLOT NumberEditingFinished(). I was hoping that I could send a argument to the SLOT function, i.e I want to know who is emitting the signal.

    How can I do this ? do I have to make my own SIGNAL somehow ? Or is there another way to do this ?

    Thanks in advance
    DarkRoast

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Daniel S.
      wrote on last edited by
      #2

      Hi!

      If you just want to know which QObject (or QSpinBox in your example) is emitting the signal, just try
      "QObject::sender()":http://doc.qt.nokia.com/4.7/qobject.html#sender.

      @void MyObject::NumberEditingFinished
      {
      QObject* emittingObject = sender();
      if(emittingObject == 0)
      {
      //slot is NOT activated by a signal
      }
      else
      {
      //slot is activated by a signal emitted from emittingObject
      }
      }@

      1 Reply Last reply
      0
      • ZlatomirZ Offline
        ZlatomirZ Offline
        Zlatomir
        wrote on last edited by
        #3

        I'm not sure i got your problem correctly, but signal mapper looks like what you are looking for.

        In "this":http://doc.qt.nokia.com/4.7/signalsandslots.html page you have documentation for signals and slots, including default parameters and signal mapper.

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          Hi DarkRoast,

          There are some things to tell you:

          a slot might never have more parameters than the signal cobnnected to it, less is ok, but not more.

          in a connect you can't define a value for a parameter, as the parameter of a slot is filled by the signal parameters

          As Zlatomir already said, "QSignalMapper":http://doc.qt.nokia.com/4.7/qsignalmapper.html might solve your problem (or QObject::sender() which gives you a pointer to the signal sender object).

          Once again:

          It is ok to have signalA(class1, class2) connected to

          • slotA(class1, class2)
          • slotB()
          • slotC()

          but not to

          • slotA(class1, class2, class3)

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DarkRoast
            wrote on last edited by
            #5

            Thanks for all your replies!

            I ended with using QSignalMapper, as seen in code beneath:

            @ QSignalMapper *signalMapper = new QSignalMapper(this);
            connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(NumberEditingFinished(int)));

            for(int i = 0; i < NUMBERS; i++)
            {
                NumbersLabel[i] = new QLabel(" N" + QString::number(i+1));
            
                NumbersSpinBox[i] = new QSpinBox;
                NumbersSpinBox[i]->setRange(MINNUMBER, MAXNUMBER);
                NumbersSpinBox[i]->setSpecialValueText(" ");
            
                signalMapper->setMapping(NumbersSpinBox[i], i);
                connect(NumbersSpinBox[i], SIGNAL(editingFinished()), signalMapper, SLOT(map()));
            
                hLayout->addWidget(NumbersLabel[i]);
                hLayout->addWidget(NumbersSpinBox[i]);
            }@ 
            

            This way my NumberEditingFinished(int) function always knows who is emitting the signal.

            Thanks

            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