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. MainWindow generate buttons dynamically
Qt 6.11 is out! See what's new in the release blog

MainWindow generate buttons dynamically

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

    I have such method in my class

    int MyClass::generateGUI(std::vector<std::string> guiInfo, int argc, char **argv) {
        QApplication a(argc, argv);
        MainWindow w;
        for(std::string s: guiInfo){
          //  QPushButton *p = new QPushButton(s,);
        }
        w.show();
        return a.exec();
    }
    

    How can I add new button for each string in the vector?

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

      Hi
      Like JonB says. You better use a layout as else you must place them yourself as not tooverlap.

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          std::vector<std::string> list{"1", "2", "3", "4", "5", "6", "7"}; // the list
          auto central = w.centralWidget(); // get middle of mainwindow
          if (central) { //if it had one
              QVBoxLayout *layout = new  QVBoxLayout(central); // make layout
               for(const std::string & s: list){ // for all strings
                QPushButton *button = new QPushButton( QString::fromStdString(s) )  ; // make button
                layout->addWidget(button); // add to layout
               }
          }
          w.show();
          return a.exec();
      }
      
      
      1 Reply Last reply
      1
      • 6 63350541

        I have such method in my class

        int MyClass::generateGUI(std::vector<std::string> guiInfo, int argc, char **argv) {
            QApplication a(argc, argv);
            MainWindow w;
            for(std::string s: guiInfo){
              //  QPushButton *p = new QPushButton(s,);
            }
            w.show();
            return a.exec();
        }
        

        How can I add new button for each string in the vector?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @63350541
        By uncommenting your correct QPushButton line, and then adding the resulting button somewhere onto your MainWindow (give it a layout, put them on that, etc.)

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

          Hi
          Like JonB says. You better use a layout as else you must place them yourself as not tooverlap.

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              std::vector<std::string> list{"1", "2", "3", "4", "5", "6", "7"}; // the list
              auto central = w.centralWidget(); // get middle of mainwindow
              if (central) { //if it had one
                  QVBoxLayout *layout = new  QVBoxLayout(central); // make layout
                   for(const std::string & s: list){ // for all strings
                    QPushButton *button = new QPushButton( QString::fromStdString(s) )  ; // make button
                    layout->addWidget(button); // add to layout
                   }
              }
              w.show();
              return a.exec();
          }
          
          
          1 Reply Last reply
          1

          • Login

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