12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include "ui_mainwindow.h"
- #include <QMainWindow>
- #include <QPushButton>
- #include <QList>
- #include <qrandom.h>
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); }
- ~MainWindow() { delete ui; }
- private:
- QPushButton * button_1 = new QPushButton("TestButton");
- QList <QPushButton *> * list = new QList <QPushButton *>();
- void remove_(){
- while (ui->vl->itemAt(0)){
- ui->vl->removeItem(ui->vl->itemAt(0));
- }
- }
- void make_(){
- QStringList texts = {"a", "b", "c"};
- foreach (QString text, texts){
- QPushButton *button = new QPushButton(text);
- bg_gen(button);
- ui->vl->addWidget(button);
- }
- }
- void bg_gen(QWidget * wg){
- QString r,g,b;
- r.setNum(QRandomGenerator::global()->bounded(1,255));
- g.setNum(QRandomGenerator::global()->bounded(1,255));
- b.setNum(QRandomGenerator::global()->bounded(1,255));
- wg->setStyleSheet("background: rgb(" + r + "," + g + "," + b + ");");
- }
- private slots:
- void on_add_3_clicked(){make_();}
- void on_del_all_clicked(){remove_();}
- void on_del_1_clicked() { ui->vl->removeWidget(button_1);}
- void on_add_1_clicked(){ ui->vl->addWidget(button_1); }
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
|