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. connect (): how to pass a parameter to the function
Forum Updated to NodeBB v4.3 + New Features

connect (): how to pass a parameter to the function

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 4.8k 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.
  • J.HilkJ J.Hilk

    @Alby said in connect (): how to pass a parameter to the function:

    @J-Hilk It is QPushButton

    well, no.
    But I assume the pointers stored in the list/vector/array are of type QPushButton*

    for(i = 0; i < 9; i++)
     {
        connect(buttons[i],  &QPushButton::clicked, this, [=]()->void{ check(i); }); // = -> we capture everything by copy, that includes the value of I during the creation of this connect
    }
    
    A Offline
    A Offline
    Alby
    wrote on last edited by
    #8

    @J-Hilk Yes, I was wrong I meant * QPushButton.

    I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button:

      QPushButton * btn = new QPushButton ();
    

    I set its geometry and created a connection:

     connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});
    

    With this button, your solution works.

    So the problem is the button array, which doesn't work, how do I fix it?

    Other information:

    the array is instantiated in the class's .h file, like so:

    QPushButton * buttons [9];
    

    and then in the .ccp file, in a method of the same class, I did this:

    for (i = 0; i <9; i ++)
    {
        for (y = 0; y <= height * 2; y = y + height)
        {
            for (x = 0; x <= width * 2; x = x + width)
            {
                buttons [i] = new QPushButton (grill);
                buttons [i] -> setObjectName ("cell" + QString :: number (i));
                buttons [i] -> setGeometry (x, y, width, height);
            }
       }
    }
    

    In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem.

    The QFrame is always instantiated in the .h file of the class, like this:

    QFrame * grill = new QFrame ();
    
    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • A Alby

      @J-Hilk Yes, I was wrong I meant * QPushButton.

      I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button:

        QPushButton * btn = new QPushButton ();
      

      I set its geometry and created a connection:

       connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});
      

      With this button, your solution works.

      So the problem is the button array, which doesn't work, how do I fix it?

      Other information:

      the array is instantiated in the class's .h file, like so:

      QPushButton * buttons [9];
      

      and then in the .ccp file, in a method of the same class, I did this:

      for (i = 0; i <9; i ++)
      {
          for (y = 0; y <= height * 2; y = y + height)
          {
              for (x = 0; x <= width * 2; x = x + width)
              {
                  buttons [i] = new QPushButton (grill);
                  buttons [i] -> setObjectName ("cell" + QString :: number (i));
                  buttons [i] -> setGeometry (x, y, width, height);
              }
         }
      }
      

      In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem.

      The QFrame is always instantiated in the .h file of the class, like this:

      QFrame * grill = new QFrame ();
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #9

      @Alby said in connect (): how to pass a parameter to the function:

      I tried your solution, first with the buttons inside the array, it didn't work

      again, it didn't work is not a sufficient error description.
      Whats the compiler error?

      So the problem is the button array, which doesn't work, how do I fix it?

      it should work with or without the array, important is only the pointer to the object instance.

      just in case:
      you can do the following:

      for (i = 0; i <9; i ++)
      {
          for (y = 0; y <= height * 2; y = y + height)
          {
              for (x = 0; x <= width * 2; x = x + width)
              {
                  auto btn = new QPushButton (grill);
                  connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});
                  buttons [i] = btn
                  buttons [i] -> setObjectName ("cell" + QString :: number (i));
                  buttons [i] -> setGeometry (x, y, width, height);
              }
         }
      }
      

      if that doesn't work either, than you're probably mixing your buttons up, and are actually not pressing, the button you're think you're pressing.


      Never mind, reading is a virtue.
      see @KroMignon 's post, you need to bring your loops into order.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • A Alby

        @J-Hilk Yes, I was wrong I meant * QPushButton.

        I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button:

          QPushButton * btn = new QPushButton ();
        

        I set its geometry and created a connection:

         connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});
        

        With this button, your solution works.

        So the problem is the button array, which doesn't work, how do I fix it?

        Other information:

        the array is instantiated in the class's .h file, like so:

        QPushButton * buttons [9];
        

        and then in the .ccp file, in a method of the same class, I did this:

        for (i = 0; i <9; i ++)
        {
            for (y = 0; y <= height * 2; y = y + height)
            {
                for (x = 0; x <= width * 2; x = x + width)
                {
                    buttons [i] = new QPushButton (grill);
                    buttons [i] -> setObjectName ("cell" + QString :: number (i));
                    buttons [i] -> setGeometry (x, y, width, height);
                }
           }
        }
        

        In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem.

        The QFrame is always instantiated in the .h file of the class, like this:

        QFrame * grill = new QFrame ();
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #10

        @Alby said in connect (): how to pass a parameter to the function:

        for (i = 0; i <9; i ++)
        {
           for (y = 0; y <= height * 2; y = y + height)
           {
               for (x = 0; x <= width * 2; x = x + width)
               {
                   buttons [i] = new QPushButton (grill);
                   buttons [i] -> setObjectName ("cell" + QString :: number (i));
                   buttons [i] -> setGeometry (x, y, width, height);
               }
          }
        }
        

        What are you doing here?!?
        This don't make sense to me.
        Are you aware that you will only have in buttons[] the last QPushButton (with highest X and Y values). All other instance references are lost.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        A 1 Reply Last reply
        3
        • KroMignonK KroMignon

          @Alby said in connect (): how to pass a parameter to the function:

          for (i = 0; i <9; i ++)
          {
             for (y = 0; y <= height * 2; y = y + height)
             {
                 for (x = 0; x <= width * 2; x = x + width)
                 {
                     buttons [i] = new QPushButton (grill);
                     buttons [i] -> setObjectName ("cell" + QString :: number (i));
                     buttons [i] -> setGeometry (x, y, width, height);
                 }
            }
          }
          

          What are you doing here?!?
          This don't make sense to me.
          Are you aware that you will only have in buttons[] the last QPushButton (with highest X and Y values). All other instance references are lost.

          A Offline
          A Offline
          Alby
          wrote on last edited by
          #11

          @KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0

          I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.

          But then how do I create a series of buttons inside an array, without losing the various references to the instance.

          KroMignonK J.HilkJ 2 Replies Last reply
          0
          • A Alby

            @KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0

            I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.

            But then how do I create a series of buttons inside an array, without losing the various references to the instance.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #12

            @Alby said in connect (): how to pass a parameter to the function:

            I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0
            I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.
            But then how do I create a series of buttons inside an array, without losing the various references to the instance.

            Why do you have 3 loop?
            I can understand loop over x and y to build the matrix, but why i?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            A 1 Reply Last reply
            0
            • A Alby

              @KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0

              I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.

              But then how do I create a series of buttons inside an array, without losing the various references to the instance.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #13

              @Alby

              int main(int argc, char *argv[])
              {
                  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                  QApplication app(argc, argv);
              
                  QWidget frame;
                  frame.resize(600,600);
                  const int col = 3;
                  const int row = 3;
                  const int width = frame.width() / col;
                  const int height = frame.width() / row;
              
                  QList<QPushButton*> buttons;
              
                  auto check = [](int i)->void{qDebug() << i;};
              
                  int i{0};
                  for(int y{0}; y < row; y++){
                      for(int x{0}; x < col; x++){
                          auto btn = new QPushButton(&frame);
                          btn->setGeometry(x * width, y *height, width, height);
                          QObject::connect(btn, &QPushButton::clicked, [=]()->void{check(i);});
                          buttons.append(btn);
                          i++;
                      }
                  }
                  frame.show();
              
                  return app.exec();
              }
              

              you should seriously consider using a QGridLayout for this.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              A 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @Alby said in connect (): how to pass a parameter to the function:

                I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0
                I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.
                But then how do I create a series of buttons inside an array, without losing the various references to the instance.

                Why do you have 3 loop?
                I can understand loop over x and y to build the matrix, but why i?

                A Offline
                A Offline
                Alby
                wrote on last edited by
                #14

                @KroMignon I realized it now, sorry I was wrong that cycle is not needed

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Alby

                  int main(int argc, char *argv[])
                  {
                      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                      QApplication app(argc, argv);
                  
                      QWidget frame;
                      frame.resize(600,600);
                      const int col = 3;
                      const int row = 3;
                      const int width = frame.width() / col;
                      const int height = frame.width() / row;
                  
                      QList<QPushButton*> buttons;
                  
                      auto check = [](int i)->void{qDebug() << i;};
                  
                      int i{0};
                      for(int y{0}; y < row; y++){
                          for(int x{0}; x < col; x++){
                              auto btn = new QPushButton(&frame);
                              btn->setGeometry(x * width, y *height, width, height);
                              QObject::connect(btn, &QPushButton::clicked, [=]()->void{check(i);});
                              buttons.append(btn);
                              i++;
                          }
                      }
                      frame.show();
                  
                      return app.exec();
                  }
                  

                  you should seriously consider using a QGridLayout for this.

                  A Offline
                  A Offline
                  Alby
                  wrote on last edited by
                  #15

                  @J-Hilk

                  I will try to use QGridLayout, the last things () about your code:

                  • What is 'auto'? , in the line:
                    auto btn = new QPushButton(&frame);

                  • I saw that you make use of this syntax ex: int y {0}, what is it?

                  • What does this mean:
                    [] (int i) ?
                    in the line:
                    auto check = [] (int i) -> void {qDebug () << i;};

                  • What does this mean?:
                    [=] () -> void
                    in the line:
                    QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});

                  Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much

                  J.HilkJ 1 Reply Last reply
                  0
                  • A Alby

                    @J-Hilk

                    I will try to use QGridLayout, the last things () about your code:

                    • What is 'auto'? , in the line:
                      auto btn = new QPushButton(&frame);

                    • I saw that you make use of this syntax ex: int y {0}, what is it?

                    • What does this mean:
                      [] (int i) ?
                      in the line:
                      auto check = [] (int i) -> void {qDebug () << i;};

                    • What does this mean?:
                      [=] () -> void
                      in the line:
                      QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});

                    Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #16

                    @Alby said in connect (): how to pass a parameter to the function:

                    Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much

                    don't worry, we all started sometime, somewhere.
                    That said, starting with Qt and c++ in parallel is a hard path, I did it myself, so it's not impossible :D but be warned.

                    What is 'auto'?

                    mmh, if you haven't heard of it, ignore it :P
                    its a place holder for compile time type deduction...
                    for more information:
                    https://en.cppreference.com/w/cpp/keyword/auto

                    I saw that you make use of this syntax ex: int y {0}, what is it?

                    it is one of the many, many ways to initialise a variable in c++ and the newest one and the one the c++ committee recommends to use.

                    What does this mean:
                    [] (int i) ?
                    in the line:
                    auto check = [] (int i) -> void {qDebug () << i;};

                    its a lambda, in this case an unnamed function inside a function
                    for more information
                    https://en.cppreference.com/w/cpp/language/lambda

                    QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});

                    an other lambda to call the previous lambda, when the clicked signal is emitted!

                    🙈


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    A 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @Alby said in connect (): how to pass a parameter to the function:

                      Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much

                      don't worry, we all started sometime, somewhere.
                      That said, starting with Qt and c++ in parallel is a hard path, I did it myself, so it's not impossible :D but be warned.

                      What is 'auto'?

                      mmh, if you haven't heard of it, ignore it :P
                      its a place holder for compile time type deduction...
                      for more information:
                      https://en.cppreference.com/w/cpp/keyword/auto

                      I saw that you make use of this syntax ex: int y {0}, what is it?

                      it is one of the many, many ways to initialise a variable in c++ and the newest one and the one the c++ committee recommends to use.

                      What does this mean:
                      [] (int i) ?
                      in the line:
                      auto check = [] (int i) -> void {qDebug () << i;};

                      its a lambda, in this case an unnamed function inside a function
                      for more information
                      https://en.cppreference.com/w/cpp/language/lambda

                      QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});

                      an other lambda to call the previous lambda, when the clicked signal is emitted!

                      🙈

                      A Offline
                      A Offline
                      Alby
                      wrote on last edited by
                      #17

                      @J-Hilk Thanks very much

                      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