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. acessing button created in a loop
Forum Updated to NodeBB v4.3 + New Features

acessing button created in a loop

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k Views 3 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.
  • A Offline
    A Offline
    amruz
    wrote on last edited by
    #1

    in my program i am creating many buttons in a loop as shown below

    for(i=0;i<7;i++)

    {
    
        for(j=0;j<7;j++)
    
        {
    
            if(count<=26)
    
            {
    
                    QString str= QChar(s);
    
                    //QString myString = "a";
    
                    button[i][j] = new QPushButton(str);
    
                    s=s+1;
    
                    button[i][j]->setText(str);
    
                    controlsLayout->addWidget(button[i][j], i, j);
    
                    connect(button[i][j], SIGNAL(clicked()), this ,SLOT(buttonWasClicked()));
    
                count++;
    
            }
    
        }
    
    }
    

    i now need to aces these buttons using signal and slots..now what can i do is write a common program for all buttons. but i need to write separate code for each button click of the buttons above..can anyone guide me properly how to access each of the buttons clicked event separately

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you mean by "separated code" ? What will be the differences ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        You cannot assign different names for the slot function (when using loop) ,
        unless you resort to some ugly macros.

        To have different slots for the clicked(), you would have to connect to each
        using another slot name.

        However, in buttonWasClicked() you can use sender()

        like

        QPushButton* button = qobject_cast<QPushButton*>(sender());
        if( button != NULL ) {
        button point to the button that emitted the clicked()
        }
        

        There is also
        http://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage
        "The QSignalMapper class is provided for situations where many signals are connected to the same slot and the slot needs to handle each signal differently."

        which sounds like what you want?

        A 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          What do you mean by "separated code" ? What will be the differences ?

          A Offline
          A Offline
          amruz
          wrote on last edited by
          #4

          @SGaist i mean for each button click i want to print each character. basically i am trying to write a keyboard program..

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            You cannot assign different names for the slot function (when using loop) ,
            unless you resort to some ugly macros.

            To have different slots for the clicked(), you would have to connect to each
            using another slot name.

            However, in buttonWasClicked() you can use sender()

            like

            QPushButton* button = qobject_cast<QPushButton*>(sender());
            if( button != NULL ) {
            button point to the button that emitted the clicked()
            }
            

            There is also
            http://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage
            "The QSignalMapper class is provided for situations where many signals are connected to the same slot and the slot needs to handle each signal differently."

            which sounds like what you want?

            A Offline
            A Offline
            amruz
            wrote on last edited by
            #5

            @mrjj thanks a lot

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The QSignalMapper is the way to go and it will be simpler.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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