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] How to make multi QPushButton to activate slot
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to make multi QPushButton to activate slot

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.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
    depecheSoul
    wrote on last edited by
    #1

    Hello.

    I am trying to make a simple game that will have 9 QPushButtons. I am trying to make single conection that will have all 9 QpushButton and slot, and when I click the button, that button will change text, with help of slot.

    I have tried doing this like:
    @connect(button1 | button2 | button3 | button4 | button5 | button6 | button7 | button8 | button9 |, SIGNAL(clicked()), this, SLOT(buttonPressed()));@
    But it dosent work.

    Do you have some idea how can I do this???

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You need to have nine different connect statements, but you can connect those to the same slot.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        If you need to differentiate between what button was clicked, you should also look into [[doc:QSignalMapper]]. Also note that you can use a loop to do the connects, if you don't want to make 9 connect statements in your code.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          depecheSoul
          wrote on last edited by
          #4

          Thank you very much for your help.

          I have made connections with QSignalMapper, but I still have one problem.

          I am trying to make a function that will on button click change text on the clicked button.

          So far I have made a function that changes text on button click but only on the last button, so when I click on the any button it will only change text on the last button.

          Can you please help me solve this problem.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            You could keep using QSignalMapper for that, using the mapping to QWidget, but perhaps that gets a bit tedious. You could also use something like this:

            @
            MyClass::buttonClicked() {
            QAbstractButton* button = qobject_cast<QAbstractButton*>(sender());
            if (!button)
            return; // not called from button

            button->setText("Clicked");
            }
            @

            And then simply connect all the buttons clicked signals to this one slot.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              depecheSoul
              wrote on last edited by
              #6

              Thank you very much for 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