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. How to use dynamically-created buttons
Forum Updated to NodeBB v4.3 + New Features

How to use dynamically-created buttons

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.7k 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.
  • H Offline
    H Offline
    Hamza.B
    wrote on last edited by Hamza.B
    #1

    hello everybody, in my Qt app I have a class that generates dynamically some buttons.
    this is the class that generates the pushbuttons: I used the method "add buttons( i, v) to generate a number of buttons, the method is used in the MainWindow class.

    #ifndef GENERATEBUTTONS_H
    #define GENERATEBUTTONS_H
    
    #include <QVBoxLayout>
    
    class GenerateButtons
    {
    public:
        GenerateButtons();
        static void addButtons(int i , QVBoxLayout V );
    };
    
    #endif // GENERATEBUTTONS_H
    
    
    #include "generatebuttons.h"
    #include <QPushButton>
    
    GenerateButtons::GenerateButtons()
    {
    
    }
    
    void GenerateButtons::addButtons(int i, QVBoxLayout V )
    {
        for(int j = 0; j < i; j++)
        {
            QPushButton *Button = new QPushButton();
            Button->setObjectName("Button" + QString::number(j));       
            V.addWidget(Button);
        }
    }
    

    main window class:

    #include "generatebuttons.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       GenerateButtons::addButtons(10 , Verticallayout );
    }
    

    what I want to do is: on each button click I can generate a debug message or Label in the same window.
    but I found difficulties because the buttons ore not created statically and can't create a slot for each button as we do with the QT designer, can someone have a proposition or a solution for this.
    thanks

    jsulmJ 1 Reply Last reply
    0
    • H Hamza.B

      hello everybody, in my Qt app I have a class that generates dynamically some buttons.
      this is the class that generates the pushbuttons: I used the method "add buttons( i, v) to generate a number of buttons, the method is used in the MainWindow class.

      #ifndef GENERATEBUTTONS_H
      #define GENERATEBUTTONS_H
      
      #include <QVBoxLayout>
      
      class GenerateButtons
      {
      public:
          GenerateButtons();
          static void addButtons(int i , QVBoxLayout V );
      };
      
      #endif // GENERATEBUTTONS_H
      
      
      #include "generatebuttons.h"
      #include <QPushButton>
      
      GenerateButtons::GenerateButtons()
      {
      
      }
      
      void GenerateButtons::addButtons(int i, QVBoxLayout V )
      {
          for(int j = 0; j < i; j++)
          {
              QPushButton *Button = new QPushButton();
              Button->setObjectName("Button" + QString::number(j));       
              V.addWidget(Button);
          }
      }
      

      main window class:

      #include "generatebuttons.h"
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
         GenerateButtons::addButtons(10 , Verticallayout );
      }
      

      what I want to do is: on each button click I can generate a debug message or Label in the same window.
      but I found difficulties because the buttons ore not created statically and can't create a slot for each button as we do with the QT designer, can someone have a proposition or a solution for this.
      thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Hamza-B said in How to use dynamically-created buttons:

      but I found difficulties because the buttons ore not created statically

      What difficulties exactly?

      QPushButton *button = new QPushButton(this);
      connect(button,...);
      

      You also can use same slot for all these buttons or use a lambda function as slot.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • L Offline
        L Offline
        Lurkki
        wrote on last edited by
        #3

        You can connect them in the function where you create them:

            for(int j = 0; j < i; j++)
            {
                QPushButton *Button = new QPushButton();
        
                connect(Button, &QPushButton::clicked, [=]() {
                    qDebug() << "button" << j << "clicked";
                });
        
                Button->setObjectName("Button" + QString::number(j));       
                V.addWidget(Button);
            }
        
        H 1 Reply Last reply
        1
        • L Lurkki

          You can connect them in the function where you create them:

              for(int j = 0; j < i; j++)
              {
                  QPushButton *Button = new QPushButton();
          
                  connect(Button, &QPushButton::clicked, [=]() {
                      qDebug() << "button" << j << "clicked";
                  });
          
                  Button->setObjectName("Button" + QString::number(j));       
                  V.addWidget(Button);
              }
          
          H Offline
          H Offline
          Hamza.B
          wrote on last edited by
          #4

          @Lurkki said in How to use dynamically-created buttons:

          connect

          I couldn't use "connect" inside the function, the class did not recognize it.
          is there some include that must be added to the class to use "connect"?

          J.HilkJ 1 Reply Last reply
          0
          • H Hamza.B

            @Lurkki said in How to use dynamically-created buttons:

            connect

            I couldn't use "connect" inside the function, the class did not recognize it.
            is there some include that must be added to the class to use "connect"?

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

            @Hamza-B
            connect is a static function of QObject. If your class is not derived from QObject, you can still use the explicitly static call

            e.g.

            QObject::connect(Button, &QPushButton::clicked, [=]() {
                        qDebug() << "button" << j << "clicked";
                    });
            
            

            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

            • Login

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