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. QT Windows GUI main window freezes while running
QtWS25 Last Chance

QT Windows GUI main window freezes while running

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.5k 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.
  • R Offline
    R Offline
    Ravi0101
    wrote on 15 Nov 2021, 11:13 last edited by
    #1

    I'm trying to receive dynamic data from a NRF device over BLE and plot it but the mainwindow.ui freezes in 5 seconds. Any leads will be highly appreciated.

    Below is my Mainwindow.cpp source code.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QStatusBar>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_bleInterface = new BLEInterface(this);

    this->sampleRate = 16000;
    
    this->init_plot();
    
    connect(m_bleInterface, &BLEInterface::dataReceived,
            this, &MainWindow::dataReceived);
    connect(m_bleInterface, &BLEInterface::devicesNamesChanged,
            [this] (QStringList devices){
        ui->devicesComboBox->clear();
        ui->devicesComboBox->addItems(devices);
    });
    connect(m_bleInterface, &BLEInterface::servicesChanged,
            [this] (QStringList services){
        ui->servicesComboBox->clear();
        ui->servicesComboBox->addItems(services);
    });
    connect(m_bleInterface, &BLEInterface::statusInfoChanged,
            [this](QString info, bool){
        this->statusBar()->showMessage(info);
    });
    connect(m_bleInterface, &BLEInterface::dataReceived,
            this, &MainWindow::plotData);
    
    m_bleInterface->scanDevices();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::init_plot()
    {
    // generate some data:
    this->plotData_Y = QVector<double>(this->sampleRate); // initialize with entries 0..100

        double h = (this->sampleRate - 0) / static_cast<double>(this->sampleRate-1);
        std::vector<double> x(this->sampleRate);
        std::vector<double>::iterator i;
        double val;
        for (i = x.begin(), val = 0; i != x.end(); ++i, val += h) {
            *i = val;
        }
    
        this->plotData_X = QVector<double>::fromStdVector(x);
    
        // create graph and assign data to it:
        ui->plot->addGraph();
        ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
        // give the axes some labels:
        ui->plot->xAxis->setLabel("x");
        ui->plot->yAxis->setLabel("y");
        // set axes ranges, so we see all data:
        ui->plot->xAxis->setRange(0, this->sampleRate);
        ui->plot->yAxis->setRange(4294945000, 4294970000);
        ui->plot->replot();
    

    }

    void MainWindow::on_bleScanButton_clicked()
    {
    m_bleInterface->scanDevices();

    }

    void MainWindow::on_stopButton_clicked()
    {
    qDebug() << "stop button clicked";
    fwriter->close_wave();
    }

    void MainWindow::on_connectButton_clicked()
    {
    qDebug() << "connect button clicked";
    m_bleInterface->set_currentDevice(ui->devicesComboBox->currentIndex());
    m_bleInterface->connectCurrentDevice();
    }

    void MainWindow::plotData(QByteArray data)
    {
    data.remove(0,1);

    qDebug() << data;
    qDebug() << data.size();
    
    int pos = 0, arrsize = data.size(), sizeInArray = 3;
    QList<QByteArray> arrays;
    while(pos<arrsize){
        QByteArray arr = data.mid(pos, sizeInArray);
        qDebug() << arr;
    
        uint32_t hrm = (arr[0] << 16) | (arr[1] << 8) | (arr[2]);
        qDebug() << QString::number(hrm);
        pos+=arr.size();
    
        this->plotData_Y.removeFirst();
        this->plotData_Y.append(hrm);
        ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
        ui->plot->replot();
    }
    

    }

    void MainWindow::dataReceived(QByteArray data)
    {
    if(ui->asciiRadioButton->isChecked()){
    ui->receivedTextEdit->append("\n");
    ui->receivedTextEdit->append(data);
    }
    else{
    ui->receivedTextEdit->append(data.toHex());
    }

    uint8_t hrm = data.at(1);
    QString str_hrm = QString::number(hrm);
    ui->receivedTextEdit->append(str_hrm);
    

    }

    void MainWindow::on_servicesComboBox_currentIndexChanged(int index)
    {
    m_bleInterface->setCurrentService(index);
    }

    J 1 Reply Last reply 15 Nov 2021, 11:52
    0
    • R Ravi0101
      15 Nov 2021, 11:13

      I'm trying to receive dynamic data from a NRF device over BLE and plot it but the mainwindow.ui freezes in 5 seconds. Any leads will be highly appreciated.

      Below is my Mainwindow.cpp source code.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QStatusBar>

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      m_bleInterface = new BLEInterface(this);

      this->sampleRate = 16000;
      
      this->init_plot();
      
      connect(m_bleInterface, &BLEInterface::dataReceived,
              this, &MainWindow::dataReceived);
      connect(m_bleInterface, &BLEInterface::devicesNamesChanged,
              [this] (QStringList devices){
          ui->devicesComboBox->clear();
          ui->devicesComboBox->addItems(devices);
      });
      connect(m_bleInterface, &BLEInterface::servicesChanged,
              [this] (QStringList services){
          ui->servicesComboBox->clear();
          ui->servicesComboBox->addItems(services);
      });
      connect(m_bleInterface, &BLEInterface::statusInfoChanged,
              [this](QString info, bool){
          this->statusBar()->showMessage(info);
      });
      connect(m_bleInterface, &BLEInterface::dataReceived,
              this, &MainWindow::plotData);
      
      m_bleInterface->scanDevices();
      

      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::init_plot()
      {
      // generate some data:
      this->plotData_Y = QVector<double>(this->sampleRate); // initialize with entries 0..100

          double h = (this->sampleRate - 0) / static_cast<double>(this->sampleRate-1);
          std::vector<double> x(this->sampleRate);
          std::vector<double>::iterator i;
          double val;
          for (i = x.begin(), val = 0; i != x.end(); ++i, val += h) {
              *i = val;
          }
      
          this->plotData_X = QVector<double>::fromStdVector(x);
      
          // create graph and assign data to it:
          ui->plot->addGraph();
          ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
          // give the axes some labels:
          ui->plot->xAxis->setLabel("x");
          ui->plot->yAxis->setLabel("y");
          // set axes ranges, so we see all data:
          ui->plot->xAxis->setRange(0, this->sampleRate);
          ui->plot->yAxis->setRange(4294945000, 4294970000);
          ui->plot->replot();
      

      }

      void MainWindow::on_bleScanButton_clicked()
      {
      m_bleInterface->scanDevices();

      }

      void MainWindow::on_stopButton_clicked()
      {
      qDebug() << "stop button clicked";
      fwriter->close_wave();
      }

      void MainWindow::on_connectButton_clicked()
      {
      qDebug() << "connect button clicked";
      m_bleInterface->set_currentDevice(ui->devicesComboBox->currentIndex());
      m_bleInterface->connectCurrentDevice();
      }

      void MainWindow::plotData(QByteArray data)
      {
      data.remove(0,1);

      qDebug() << data;
      qDebug() << data.size();
      
      int pos = 0, arrsize = data.size(), sizeInArray = 3;
      QList<QByteArray> arrays;
      while(pos<arrsize){
          QByteArray arr = data.mid(pos, sizeInArray);
          qDebug() << arr;
      
          uint32_t hrm = (arr[0] << 16) | (arr[1] << 8) | (arr[2]);
          qDebug() << QString::number(hrm);
          pos+=arr.size();
      
          this->plotData_Y.removeFirst();
          this->plotData_Y.append(hrm);
          ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
          ui->plot->replot();
      }
      

      }

      void MainWindow::dataReceived(QByteArray data)
      {
      if(ui->asciiRadioButton->isChecked()){
      ui->receivedTextEdit->append("\n");
      ui->receivedTextEdit->append(data);
      }
      else{
      ui->receivedTextEdit->append(data.toHex());
      }

      uint8_t hrm = data.at(1);
      QString str_hrm = QString::number(hrm);
      ui->receivedTextEdit->append(str_hrm);
      

      }

      void MainWindow::on_servicesComboBox_currentIndexChanged(int index)
      {
      m_bleInterface->setCurrentService(index);
      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Nov 2021, 11:52 last edited by
      #2

      @Ravi0101 said in QT Windows GUI main window freezes while running:

      pos+=arr.size();

      If for some reason arr.size() starts to return 0 you will not leave the loop.
      Did you analyse your app? For example running through debugger and see where it hangs? Or at least add some debug output to see what happens.

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

      R 1 Reply Last reply 15 Nov 2021, 16:51
      4
      • J jsulm
        15 Nov 2021, 11:52

        @Ravi0101 said in QT Windows GUI main window freezes while running:

        pos+=arr.size();

        If for some reason arr.size() starts to return 0 you will not leave the loop.
        Did you analyse your app? For example running through debugger and see where it hangs? Or at least add some debug output to see what happens.

        R Offline
        R Offline
        Ravi0101
        wrote on 15 Nov 2021, 16:51 last edited by
        #3

        Hi @jsulm Thank you very much for your response.

        I debugged the while loop and it showed me this: 7cea8fdb-d974-47bf-83a0-6a895aa68920-image.png

        R 1 Reply Last reply 15 Nov 2021, 17:13
        0
        • R Ravi0101
          15 Nov 2021, 16:51

          Hi @jsulm Thank you very much for your response.

          I debugged the while loop and it showed me this: 7cea8fdb-d974-47bf-83a0-6a895aa68920-image.png

          R Offline
          R Offline
          Ravi0101
          wrote on 15 Nov 2021, 17:13 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ravi0101
            wrote on 15 Nov 2021, 17:27 last edited by
            #5

            @jsulm I got this exception:

            Exception at 0x770eb5b2, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in dwrite!DWriteCreateFactory

            J 1 Reply Last reply 15 Nov 2021, 17:36
            0
            • R Ravi0101
              15 Nov 2021, 17:27

              @jsulm I got this exception:

              Exception at 0x770eb5b2, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in dwrite!DWriteCreateFactory

              J Offline
              J Offline
              JonB
              wrote on 15 Nov 2021, 17:36 last edited by JonB
              #6

              @Ravi0101
              If you get an exception in the debugger look at the stack trace window for where it emanated from in your code.

              R 1 Reply Last reply 15 Nov 2021, 18:17
              1
              • J JonB
                15 Nov 2021, 17:36

                @Ravi0101
                If you get an exception in the debugger look at the stack trace window for where it emanated from in your code.

                R Offline
                R Offline
                Ravi0101
                wrote on 15 Nov 2021, 18:17 last edited by
                #7

                @JonB Also I have noticed that cpu usage is going to 100 % when running mainwindow.ui and then it freezes. could this be issue with the while loop in my code please?

                c04fc970-b034-49ee-9948-a49ce69b74c8-image.png

                J 1 Reply Last reply 16 Nov 2021, 06:18
                0
                • R Ravi0101
                  15 Nov 2021, 18:17

                  @JonB Also I have noticed that cpu usage is going to 100 % when running mainwindow.ui and then it freezes. could this be issue with the while loop in my code please?

                  c04fc970-b034-49ee-9948-a49ce69b74c8-image.png

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 16 Nov 2021, 06:18 last edited by
                  #8

                  @Ravi0101 said in QT Windows GUI main window freezes while running:

                  could this be issue with the while loop in my code please?

                  Yes, it can. That is why I also suggested to add some debug output.
                  Please add a qDebug() output just after the loop and see whether you ever see it. If you don't then you have an endless loop.

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

                  R 1 Reply Last reply 16 Nov 2021, 11:35
                  1
                  • J jsulm
                    16 Nov 2021, 06:18

                    @Ravi0101 said in QT Windows GUI main window freezes while running:

                    could this be issue with the while loop in my code please?

                    Yes, it can. That is why I also suggested to add some debug output.
                    Please add a qDebug() output just after the loop and see whether you ever see it. If you don't then you have an endless loop.

                    R Offline
                    R Offline
                    Ravi0101
                    wrote on 16 Nov 2021, 11:35 last edited by
                    #9

                    @jsulm Thank you, I did that and qdebug output is displayed, please find below screenshot.

                    6c20b38d-7266-4bb1-b2f6-bf6738efe162-image.png

                    J 1 Reply Last reply 16 Nov 2021, 11:40
                    0
                    • R Ravi0101
                      16 Nov 2021, 11:35

                      @jsulm Thank you, I did that and qdebug output is displayed, please find below screenshot.

                      6c20b38d-7266-4bb1-b2f6-bf6738efe162-image.png

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 16 Nov 2021, 11:40 last edited by
                      #10

                      @Ravi0101 It's not clear from the screen shot whether the output is inside the loop or outside?
                      You should post code as text not pictures.

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

                      R 1 Reply Last reply 16 Nov 2021, 11:48
                      0
                      • J jsulm
                        16 Nov 2021, 11:40

                        @Ravi0101 It's not clear from the screen shot whether the output is inside the loop or outside?
                        You should post code as text not pictures.

                        R Offline
                        R Offline
                        Ravi0101
                        wrote on 16 Nov 2021, 11:48 last edited by
                        #11

                        @jsulm please find below.

                        void MainWindow::plotData(QByteArray data)
                        {
                        auto count = data.size();
                        qDebug() << "bytearray size 1: " << count;
                        data.remove(0,1);
                        count = data.size();
                        qDebug() << "bytearray size 2: " << count;

                        qDebug() << data;
                        qDebug() << data.size();
                        
                        int pos = 0, arrsize = data.size(), sizeInArray = 3;
                        QList<QByteArray> arrays;
                        

                        std::vector<uint32_t> hrm;
                        // std::vector<double> hrm_final;
                        // uint32_t old_hrm = 0;
                        while(pos<arrsize){
                        QByteArray arr = data.mid(pos, sizeInArray);
                        qDebug() << arr;
                        uint32_t hrm = (arr[0] << 16) | (arr[1] << 8) | (arr[2]);
                        qDebug() << QString::number(hrm);
                        // if(arrsize!=0){
                        pos+=arr.size();

                        //plot with hrm diffference

                        // if(pos == 0)
                        // {
                        // old_hrm = hrm;
                        // continue;
                        // }
                        //else
                        // {
                        this->plotData_Y.removeFirst();

                                //**plot with python code reference**
                        //double hrm_final =(hrm / qPow(2,31)) * 10;
                         //this->plotData_Y.append(hrm_final);
                         //  qDebug() << QString::number(hrm_final);
                        
                                //**plot with hrm difference**
                           // this->plotData_Y.append(old_hrm-hrm);
                          // qDebug() << QString::number(old_hrm-hrm);
                        
                            //**plot with original hrm**
                             this->plotData_Y.append(hrm);
                        
                            ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
                            // set axes ranges, so we see all data:
                            ui->plot->replot();
                            }
                         qDebug() << "not an endless loop";
                        }
                        

                        //}

                        J 1 Reply Last reply 16 Nov 2021, 11:58
                        0
                        • R Ravi0101
                          16 Nov 2021, 11:48

                          @jsulm please find below.

                          void MainWindow::plotData(QByteArray data)
                          {
                          auto count = data.size();
                          qDebug() << "bytearray size 1: " << count;
                          data.remove(0,1);
                          count = data.size();
                          qDebug() << "bytearray size 2: " << count;

                          qDebug() << data;
                          qDebug() << data.size();
                          
                          int pos = 0, arrsize = data.size(), sizeInArray = 3;
                          QList<QByteArray> arrays;
                          

                          std::vector<uint32_t> hrm;
                          // std::vector<double> hrm_final;
                          // uint32_t old_hrm = 0;
                          while(pos<arrsize){
                          QByteArray arr = data.mid(pos, sizeInArray);
                          qDebug() << arr;
                          uint32_t hrm = (arr[0] << 16) | (arr[1] << 8) | (arr[2]);
                          qDebug() << QString::number(hrm);
                          // if(arrsize!=0){
                          pos+=arr.size();

                          //plot with hrm diffference

                          // if(pos == 0)
                          // {
                          // old_hrm = hrm;
                          // continue;
                          // }
                          //else
                          // {
                          this->plotData_Y.removeFirst();

                                  //**plot with python code reference**
                          //double hrm_final =(hrm / qPow(2,31)) * 10;
                           //this->plotData_Y.append(hrm_final);
                           //  qDebug() << QString::number(hrm_final);
                          
                                  //**plot with hrm difference**
                             // this->plotData_Y.append(old_hrm-hrm);
                            // qDebug() << QString::number(old_hrm-hrm);
                          
                              //**plot with original hrm**
                               this->plotData_Y.append(hrm);
                          
                              ui->plot->graph(0)->setData(this->plotData_X, this->plotData_Y);
                              // set axes ranges, so we see all data:
                              ui->plot->replot();
                              }
                           qDebug() << "not an endless loop";
                          }
                          

                          //}

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 16 Nov 2021, 11:58 last edited by
                          #12

                          @Ravi0101 Then please run through debugger untill it hangs and then check the stack trace to see where it hangs...

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

                          1 Reply Last reply
                          2

                          2/12

                          15 Nov 2021, 11:52

                          topic:navigator.unread, 10
                          • Login

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