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. Best way to connect a grid of buttons to a function which takes the row and column of the button as argument
Forum Updated to NodeBB v4.3 + New Features

Best way to connect a grid of buttons to a function which takes the row and column of the button as argument

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.8k 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.
  • S Offline
    S Offline
    saa_
    wrote on last edited by
    #1

    Suppose I have a grid of buttons. When user clicks on any of these buttons I would like to know what button the user clicked (the row and column number for the button) and use this info to do something

    So I have managed to do this, but would like to see if there are other, perhaps better, ways of achieving this. Using a widget application this is what I have done so far:

    Widget::Widget(QWidget *parent): QWidget(parent)
    {
        QGridLayout * grid = new QGridLayout(this);
    
        for(int i = 0; i < columns; i++)
        {
            for(int j = 0; j < rows; j++)
            {
                QPushButton * button = new QPushButton(this);
                grid->addWidget(button, j, i);
    
                // Set size text etc. for each button 
    
                connect(button, &QPushButton::clicked, [=](){
                    func(i, j);     // Call the function which uses i and j here
                });
            }
        }
    }
    
    void Widget::func(int i, int j)
    {
        // Do stuff with i and j here
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Using a custom widget to hold the buttons and lambda to capture col, row seems
      pretty fine.

      You could add a new signal

      void ButtonPressed(int col, int row);

      To "Widget"
      and emit that in the lambda to allow other classes to hook into the clicked signal to do custom processing if
      needed.

      That way you can reuse widget in other cases, where clicking on a button will do something different.

      
                  connect(button, &QPushButton::clicked, [=](){
                      emit ButtonPressed(i,j);
                  });
      
      JonBJ 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        Using a custom widget to hold the buttons and lambda to capture col, row seems
        pretty fine.

        You could add a new signal

        void ButtonPressed(int col, int row);

        To "Widget"
        and emit that in the lambda to allow other classes to hook into the clicked signal to do custom processing if
        needed.

        That way you can reuse widget in other cases, where clicking on a button will do something different.

        
                    connect(button, &QPushButton::clicked, [=](){
                        emit ButtonPressed(i,j);
                    });
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @mrjj
        If there are 100 rows and 100 columns there will be 10,000 connect()s. (And 10,000 QPushButtons; but that's not what we've been asked about.)

        mrjjM S 2 Replies Last reply
        0
        • JonBJ JonB

          @mrjj
          If there are 100 rows and 100 columns there will be 10,000 connect()s. (And 10,000 QPushButtons; but that's not what we've been asked about.)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB
          Hehe, right.
          Well if the question was how do i have an obscene amount of pushbuttons I would have
          told him to paint them and keep a list of rects and do own hittesting.
          10K buttons in a layout would also be heavy when resizing :)

          1 Reply Last reply
          2
          • JonBJ JonB

            @mrjj
            If there are 100 rows and 100 columns there will be 10,000 connect()s. (And 10,000 QPushButtons; but that's not what we've been asked about.)

            S Offline
            S Offline
            saa_
            wrote on last edited by
            #5

            @JonB
            Thanks for your answer. I am just trying to learn QT right now, and this was just a random problem I gave myself :)

            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