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. Optimizing regular QCustomPlot
Forum Updated to NodeBB v4.3 + New Features

Optimizing regular QCustomPlot

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 6 Posters 2.3k Views 2 Watching
  • 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.
  • L lukutis222
    6 Jun 2024, 14:45

    @Pl45m4 Thanks

    @Pl45m4 said in Optimizing regular QCustomPlot:

    Holy mother of C cast .... (╯°□°)╯︵ ┻━┻
    :D

    Yeah I guess thats a bit extra :D Il get it fixed :D

    J Offline
    J Offline
    JonB
    wrote on 6 Jun 2024, 17:28 last edited by
    #21

    @lukutis222
    Not that it matters, but you don't need the outer cast, and only one of the inner casts, e.g.

    addPoint_current(double(sample_counter) / 20, current);
    

    would deliver the same.

    1 Reply Last reply
    0
    • L lukutis222
      6 Jun 2024, 13:32

      @jsulm
      Wow well spotted. This was totally the issue. I did on_detect_button_clicked because as soon as worker thread is started, I call that on_detect_button_clicked method which does the following:

      void MainWindow::on_detect_button_clicked()
      {
          ui->uCurrent_detected_label->setText("uCurrent being detected");
          ui->uCurrent_detected_label->setStyleSheet("QLabel {color : rgb(255, 165, 00); font : 700 }");
          if (serial_worker->Scan_serial_devices() > 0)
          {
              serial_worker->Automatic_detect(0);
          }
          else
          {
              ui->uCurrent_detected_label->setText("uCurrent not detected");
              ui->uCurrent_detected_label->setStyleSheet("QLabel {color : rgb(255, 0, 00); font : 700 }");
          }
      }
      

      It is intended to call serial_worker public methods to automatically find the and connect to the correct serialport.

      If I comment out this line:

      connect(workerThread, &QThread::started, this, &MainWindow::on_detect_button_clicked);
      

      and I manually connect to the serial port in the worker class constructor (just for testing purposes):

      SerialWorker::SerialWorker(QObject *parent)
          : QObject{parent}
      {
          serial = new QSerialPort(this);
          connect(serial, &QSerialPort::readyRead, this, &SerialWorker::readData);
      
      
          automatic_detection.detection_flag = 0;
          automatic_detection.current_device_id = 0;
          automatic_detection.command = "uCurrent?";
          automatic_detection.response = "uCurrent_OK";
          automatic_detection.max_retries = 3;
          automatic_detection.retry_count = 0;
          automatic_detection.timeout = 400;
      
          available_devices.resize(10);
          //timer_detect = new QTimer(this);
          //connect(timer_detect, &QTimer::timeout, this, &SerialWorker::detect_timeout);
      
      
      
      
          // set default sampling info
          sampling_info.sampling_time = UNLIMITED;
          sampling_info.sampling_frequency = 1; // 1HZ default // set 100
          sample_counter = 0;
          sample_counter_1_sec = 0;
      
          current_1_min_avg.resize(60);
          current_30_min_avg.resize(30);
          current_60_min_avg.resize(60);
          current_24_hr_avg.resize(1440);
      
          serial->setPortName("COM3");  // Setting the port name to COM3
          serial->setBaudRate(QSerialPort::Baud115200);  // Setting the baud rate to 115200
          serial->setDataBits(QSerialPort::Data8);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setParity(QSerialPort::NoParity);
          serial->setFlowControl(QSerialPort::NoFlowControl);
      
          Serial_connect();
      
      
      
      }
      

      Everything works as expected! My update_graph function no longer stops the readData function.

      Thank you very much, although I am not so sure why that happened. Perhaps you can clarify:

      1. Why I cannot do:
      connect(workerThread, &QThread::started, this, &MainWindow::on_detect_button_clicked);
      

      in my mainwindow.cpp ? How exactly it affects my serialworker class?

      @jsulm said in Optimizing regular QCustomPlot:

      SerialWorker should also allocate everything it needs in that method (not in constructor) to make sure everything lives in the worker thread.

      Can you clarify a little bit what you mean by that?

      S Offline
      S Offline
      SimonSchroeder
      wrote on 7 Jun 2024, 06:47 last edited by
      #22

      @lukutis222 said in Optimizing regular QCustomPlot:

      1. Why I cannot do:

      connect(workerThread, &QThread::started, this, &MainWindow::on_detect_button_clicked);

      in my mainwindow.cpp ? How exactly it affects my serialworker class?

      You have to keep in mind that the MainWindow (the this pointer in your case) lives inside the main thread and not the worker thread. The third argument to connect is the context object. If the sender and receiver are not inside the same thread, the slot call is automatically queued into the thread of the receiver or context object. To be fair, I assume that the constructor of SerialWorker is most likely also called inside the main thread before it is moved to the worker thread. If that is the case it shouldn't matter which way you are doing things. However, the constructor of SerialWorker is most likely started before the worker thread starts and thus is completely initialized. Whereas things inside the worker thread and on_detect_button_clicked might run in parallel.

      @lukutis222 said in Optimizing regular QCustomPlot:

      It has been earlier mentioned that updating the ui via other threads is not currently supported in the QT hence I am looking for another optimal method.

      The best solution is to use signals and slots. This could be little finer control than what you have already suggested. Instead of sending the full data to the GUI thread for processing, you could do some processing in the worker thread. For example, you could emit a signal carrying a QString which would be connected to ui->flash_label->setText. I hate writing additional signals just for something short and simple like this. Instead I use QMetaObject::invokeMethod(qApp, { ... }); with a lambda to post something to the GUI thread. I have collected a few scenarios of communicating between a worker thread and the GUI thread into a small header-only library: https://github.com/SimonSchroeder/QtThreadHelper

      1 Reply Last reply
      0

      21/22

      6 Jun 2024, 17:28

      • Login

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