How to make a refresh button?
Unsolved
General and Desktop
-
I want to show some widgets, but I don't know how many are they. So I made a refresh button and an onclicked slot. When I clicked it , widgets may be added or removed. It worked on the first time, but failed when I click the button.
I have tried update(), repaint(), showNormal, then the exe crashed.
I have tried to delete the old layout, then the exe crashed.
Thank you for your time.#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); dbInitial(); on_refresh_clicked(); mainLayout = new QVBoxLayout(); ui->centralwidget->setLayout(mainLayout); // QHBoxLayout * firstline = new QHBoxLayout(); // firstline->addWidget(ui->firstline); mainLayout->addWidget(ui->firstline); mainLayout->addLayout(appliance); //// bool ifShowOffline; // QWidget *firstline = new QWidget(ui->centralwidget); // QPushButton *refresh = new QPushButton("刷新",firstline); // QPushButton *serialPort = new QPushButton("调试串口",firstline); // QCheckBox *checkbox_ifShowOffline = new QCheckBox(firstline); // checkbox_ifShowOffline->setText("展示不在线设备"); // QCheckBox *checkbox_ifShowCharts = new QCheckBox(firstline); // checkbox_ifShowCharts->setText("展示图表"); // QGridLayout *layout = new QGridLayout(firstline); // layout->addWidget(refresh, 1, 1); // layout->addWidget(serialPort,1,2); // layout->addWidget(checkbox_ifShowOffline,1,3); // layout->addWidget(checkbox_ifShowCharts,1,4); // firstline->showNormal(); // setCentralWidget(firstline); } MainWindow::~MainWindow() { delete ui; } void MainWindow::dbInitial(){ db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("nilm.db"); db.open(); qDebug()<<"数据库连接成功"; } void MainWindow::on_refresh_clicked() { qDebug()<<"刷新"; //crashed // appliance->destroyed(); // if(!appliance->isEmpty()){ // delete.appliance; // } query = new QSqlQuery("select * from nilm where WorkStatu = 1;",db); QSqlRecord rec = query->record(); appliance = new QHBoxLayout(); while(query->next()){ rec = query->record(); //测试先复制,查看是否构造成功 QString Type = query->value("Type").toString(); bool WorkStatus = query->value("WorkStatu").toBool(); int HealthStatus = query->value("HealthStatu").toInt(); //新建appliancestatus对象 ApplianceStatus *applianceTemp = new ApplianceStatus(Type, WorkStatus, HealthStatus,ui->centralwidget); appliance->addWidget(applianceTemp); } //following codes lead to crash // ui->centralwidget->showNormal(); // ui->centralwidget->update(); // ui->centralwidget->repaint(); }
-
@Roma13 said in How to make a refresh button?:
appliance = new QHBoxLayout();
This layout does not belong to any widget, so what do you expect from adding widgets to it?