mainwindow.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include "ui_mainwindow.h"
  4. #include <QMainWindow>
  5. #include <QPushButton>
  6. #include <QList>
  7. #include <qrandom.h>
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class MainWindow; }
  10. QT_END_NAMESPACE
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14. public:
  15. MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); }
  16. ~MainWindow() { delete ui; }
  17. private:
  18. QPushButton * button_1 = new QPushButton("TestButton");
  19. QList <QPushButton *> * list = new QList <QPushButton *>();
  20. void remove_(){
  21. while (ui->vl->itemAt(0)){
  22. ui->vl->removeItem(ui->vl->itemAt(0));
  23. }
  24. }
  25. void make_(){
  26. QStringList texts = {"a", "b", "c"};
  27. foreach (QString text, texts){
  28. QPushButton *button = new QPushButton(text);
  29. bg_gen(button);
  30. ui->vl->addWidget(button);
  31. }
  32. }
  33. void bg_gen(QWidget * wg){
  34. QString r,g,b;
  35. r.setNum(QRandomGenerator::global()->bounded(1,255));
  36. g.setNum(QRandomGenerator::global()->bounded(1,255));
  37. b.setNum(QRandomGenerator::global()->bounded(1,255));
  38. wg->setStyleSheet("background: rgb(" + r + "," + g + "," + b + ");");
  39. }
  40. private slots:
  41. void on_add_3_clicked(){make_();}
  42. void on_del_all_clicked(){remove_();}
  43. void on_del_1_clicked() { ui->vl->removeWidget(button_1);}
  44. void on_add_1_clicked(){ ui->vl->addWidget(button_1); }
  45. private:
  46. Ui::MainWindow *ui;
  47. };
  48. #endif // MAINWINDOW_H