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. Create dynamic buttons with function
Qt 6.11 is out! See what's new in the release blog

Create dynamic buttons with function

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 425 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.
  • P Offline
    P Offline
    ppitu
    wrote on last edited by
    #1

    Hi,

    I have to dynamiclly create buttons in horizontalLayout and connect this buttons to my function.

    I create button in this way:

    for(auto & mTitle : pattern.mTitles)
        {
            ui->horizontalLayout->addWidget(new QPushButton(mTitle));
        }
    

    How can I connect the function to this buttons?

    KroMignonK 1 Reply Last reply
    0
    • P ppitu

      Hi,

      I have to dynamiclly create buttons in horizontalLayout and connect this buttons to my function.

      I create button in this way:

      for(auto & mTitle : pattern.mTitles)
          {
              ui->horizontalLayout->addWidget(new QPushButton(mTitle));
          }
      

      How can I connect the function to this buttons?

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

      @ppitu said in Create dynamic buttons with function:

      How can I connect the function to this buttons?

      I don't really understand your problem here.
      Use the instance and connect the right signal to the wanted function.

      For example, with a lambda function, I would do it like this:

      for(auto & mTitle : pattern.mTitles)
      {
          auto btn = new QPushButton (mTitle);
          connect (btn, & QPushButton :: clicked, this, [btn] () {
                qDebug() << "Button" << btn << "clicked"; 
          });
          ui->horizontalLayout->addWidget(btn );
      }
      

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

      1 Reply Last reply
      3
      • P Offline
        P Offline
        ppitu
        wrote on last edited by
        #3

        I first time do it in Qt and i cant find tutorial.

        Thanks for help ;D

        KroMignonK 1 Reply Last reply
        0
        • P ppitu

          I first time do it in Qt and i cant find tutorial.

          Thanks for help ;D

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

          @ppitu said in Create dynamic buttons with function:

          I first time do it in Qt and i cant find tutorial.

          Then you don't search very well, for example in the QObject:connect() documentation:
          https://doc.qt.io/qt-5/qobject.html#connect

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

          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