MainWindow generate buttons dynamically
-
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?
-
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(); } -
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?
-
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(); }