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. How to make a refresh button?
QtWS25 Last Chance

How to make a refresh button?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.1k Views
  • 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.
  • Roma13R Offline
    Roma13R Offline
    Roma13
    wrote on last edited by
    #1

    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();
    }
    
    
    
    jsulmJ 1 Reply Last reply
    0
    • Roma13R Roma13

      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();
      }
      
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Roma13R 1 Reply Last reply
      2
      • jsulmJ jsulm

        @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?

        Roma13R Offline
        Roma13R Offline
        Roma13
        wrote on last edited by Roma13
        #3

        @jsulm

        mainLayout->addLayout(appliance);
        

        I added this layout into mainLayout, still didn't work.

        1 Reply Last reply
        0

        • Login

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