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] Differentiating multiple signals going to a single slot
Qt 6.11 is out! See what's new in the release blog

[Solved] Differentiating multiple signals going to a single slot

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.2k 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.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    Here's some code that creates a list of 10 pushbuttons and connects the clicked singal of all of them to a single slot (on_pushButtonList_clicked)
    @
    for(index = 0; index < 10; index++)
    {
    pushButton[index] = new QPushButton(this);
    pushButton[index]->resize(60, 40);
    pushButton[index]->move(400, 50 + 50 * index);
    pushButton[index]->setText(QString::number(index, 'g', 2));
    connect(pushButton[index], SIGNAL(clicked()), this, SLOT(on_pushButtonList_clicked()));
    }
    @
    When I get to the on_pushButtonList_clicked slot, how do I find out which button was clicked? In addition to getting a pointer to the button that was hit, is there a way to get its index position (pushButton[0], pushButton[6], etc.)?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Check out "QSignalMapper.":http://qt-project.org/doc/qt-4.8/qsignalmapper.html

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tolszak
        wrote on last edited by
        #3

        You can get pointer to pushbutton in slot by using QObject::sender() method.
        If you have access to pushButtonList (I suppose it's QList) you can get button index with
        pushButtonList.indexOf(sender()).

        If you don't have access to pushButtonList from slot you should check QSignalMapper as mlong suggested.

        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