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. QPushButton and Connect with variables
Qt 6.11 is out! See what's new in the release blog

QPushButton and Connect with variables

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views
  • 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.
  • N Offline
    N Offline
    nike3
    wrote on last edited by
    #1

    I have the following problem:

    I want to create an unknown amount of QPushButtons and connect the clicked event to a function which needs a variable. It looks like this:

            buttons[i] = new QPushButton(filename);
    
            buttons[i]->connect(buttons[i], SIGNAL(clicked(bool)), this, SLOT(readbutton(i)));
    

    Without the "i" in the readbutton function it would work like this. But I need to know which button was clicked in the function "readbutton". Can I use the connect function or is there another easy way to call the function with the integer variable "i".
    I'm new to QT and a programming beginner so it would be great if you could it explain it in an easy understandable way.

    Thanks a lot.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It works with the new signal & slot syntax and a small lambda

      connect(buttons[i], &QPusbButton::clicked, this, [this, i]() { readButton(i); });
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      6
      • N Offline
        N Offline
        nike3
        wrote on last edited by
        #3

        Thank you very much!

        1 Reply Last reply
        1

        • Login

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