Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to exit void QIODevice::readyRead() function ? Is this function also interrupt?
Forum Updated to NodeBB v4.3 + New Features

How to exit void QIODevice::readyRead() function ? Is this function also interrupt?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
31 Posts 4 Posters 3.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.
  • M Mucahit

    Yes that's exactly what i want but the update function didn't work.

    I had to use the update function like this:

    void QIODevice::readyRead()
    {
    data=serial.readAll();
    totaldata=totaldata+data;
    QMainWindow main;
    main.update();
    }

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #13

    @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

    void QIODevice::readyRead()
    {
    data=serial.readAll();
    totaldata=totaldata+data;
    QMainWindow main;
    main.update();
    }

    this should do nothing, that creates a new main window and destroys it immediately, you don't event show() it.

    You have to signal your mainwinow instance to update, do that via a custom signal, emitted from your iodevice


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    1
    • M Mucahit

      Yes that's exactly what i want but the update function didn't work.

      I had to use the update function like this:

      void QIODevice::readyRead()
      {
      data=serial.readAll();
      totaldata=totaldata+data;
      QMainWindow main;
      main.update();
      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #14

      @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

      QMainWindow main;

      ?
      You create a NEW QMainWindow instance here!
      Why don't you simply try to actually draw anything in your paintEvent?

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

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mucahit
        wrote on last edited by
        #15

        I dont know how to do i am new in qt and c ++. I do not know how to renew my previously found chart pie in my main window with any data I read from the serial port. When the application is first opened, the chart pie is painted with 50 percent, but it will be 70 percent when any value comes from the serial port. Sorry for bothering you.

        jsulmJ 1 Reply Last reply
        0
        • M Mucahit

          I dont know how to do i am new in qt and c ++. I do not know how to renew my previously found chart pie in my main window with any data I read from the serial port. When the application is first opened, the chart pie is painted with 50 percent, but it will be 70 percent when any value comes from the serial port. Sorry for bothering you.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #16

          @Mucahit You can redraw completely based on updated data. If paintEvent() is called the previously drawn content is deleted.

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mucahit
            wrote on last edited by
            #17

            But I want to do this outside the readyread function when all the data reading is finished, so that it does not hinder my data reading speed. How can I do that ?

            jsulmJ 1 Reply Last reply
            0
            • M Mucahit

              But I want to do this outside the readyread function when all the data reading is finished, so that it does not hinder my data reading speed. How can I do that ?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #18

              @Mucahit First: you do it already outside of readyRead()! update() does not call paintEvent() directly, it only puts the event in the event queue. Second: do you have a protocol or how do you know when the reading is done? If you have a protocall when you can use a bool variable which is set to true when all data was read (and call update()) and use this variable inside paintEvent() to check whether you need to paint or not.

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

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Mucahit
                wrote on last edited by
                #19

                I understood very well what you said. Yes i have a protocol but i can't trigger paintevent.

                jsulmJ 1 Reply Last reply
                0
                • M Mucahit

                  I understood very well what you said. Yes i have a protocol but i can't trigger paintevent.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #20

                  @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

                  but i can't trigger paintevent

                  Why not? You just need to store received data, set a bool flag and call update.

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mucahit
                    wrote on last edited by Mucahit
                    #21

                    Now my code is below:

                    QByteArray data;
                    QByteArray totaldata;

                    void QIODevice::readyRead()
                    {
                    data=serial.readAll();
                    totaldata=totaldata+data;
                    if(totaldata=="xFe/xD3/xFD")
                    {
                    MainWindow main;
                    main.update();
                    // or main.repaint();
                    }
                    }
                    void MainWindow::paintEvent(QPaintEvent *)
                    {
                    qDebug()<<totaldata; // I want to print the data I receive here because I will draw pies based on the data I received
                    }

                    I see the data i read from the serial port in the output of this code, right? But i don't see anything. As i said, i can only get my qdebug output when I hover my mouse over the window. It never prints that value on the screen if I don't move it.

                    jsulmJ 1 Reply Last reply
                    0
                    • M Mucahit

                      Now my code is below:

                      QByteArray data;
                      QByteArray totaldata;

                      void QIODevice::readyRead()
                      {
                      data=serial.readAll();
                      totaldata=totaldata+data;
                      if(totaldata=="xFe/xD3/xFD")
                      {
                      MainWindow main;
                      main.update();
                      // or main.repaint();
                      }
                      }
                      void MainWindow::paintEvent(QPaintEvent *)
                      {
                      qDebug()<<totaldata; // I want to print the data I receive here because I will draw pies based on the data I received
                      }

                      I see the data i read from the serial port in the output of this code, right? But i don't see anything. As i said, i can only get my qdebug output when I hover my mouse over the window. It never prints that value on the screen if I don't move it.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #22

                      @Mucahit Sorry, but this code does not make any sense! You already were told that creating a temporary QMainWindow instance and calling update() on it is simply wrong! You need to call update on your widget where you want to paint.
                      I suggest you take time to check some paintEvent examples, like https://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html

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

                      1 Reply Last reply
                      4
                      • M Offline
                        M Offline
                        Mucahit
                        wrote on last edited by
                        #23

                        Thank you sir, i know how i can make a watch. All i want is to call paintevent in readyread function and i couldn't solve this problem. All i want is a line of code not an article, i can't. Please can you share how to call paintevent in function code to me ?

                        jsulmJ 1 Reply Last reply
                        0
                        • M Mucahit

                          Thank you sir, i know how i can make a watch. All i want is to call paintevent in readyread function and i couldn't solve this problem. All i want is a line of code not an article, i can't. Please can you share how to call paintevent in function code to me ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #24

                          @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

                          Please can you share how to call paintevent in function code to me ?

                          This already was explained: you do NOT call paintEvent directly, Qt framework does it for you when the widget needs to be repainted. You call update() to trigger a paint event when needed.
                          "All i want is a line of code not an article" - so, you want a magical line of code without spending time to learn how to paint? You already spent one day in forum. You could have read the example in this time to understand how it works...

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

                          1 Reply Last reply
                          2
                          • M Offline
                            M Offline
                            Mucahit
                            wrote on last edited by
                            #25

                            Thank you. You tell me the way to call the update function is wrong. I wanted to learn the truth. It is not difficult to read an article, I do not understand what you mean in the article i am reading. I think i need to study and learn more about qt and c ++. Thanks for all the help.

                            jsulmJ 1 Reply Last reply
                            0
                            • M Mucahit

                              Thank you. You tell me the way to call the update function is wrong. I wanted to learn the truth. It is not difficult to read an article, I do not understand what you mean in the article i am reading. I think i need to study and learn more about qt and c ++. Thanks for all the help.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by jsulm
                              #26

                              @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

                              You tell me the way to call the update function is wrong

                              Yes, because you call update() on a temporary MainWindow instance which is completely unrelated to your actual main window instance.

                              void QIODevice::readyRead()
                              {
                              data=serial.readAll();
                              totaldata=totaldata+data;
                              if(totaldata=="xFe/xD3/xFD")
                              {
                                  update();
                              }
                              }
                              

                              There is one thing you need to change I think: you should not try to paint directly on main window. Create a custom widget (subclass QWidget) and override paintEvent() there (this is what is done in the example). Before calling update() on it you will need to pass the data to it:

                              void QIODevice::readyRead()
                              {
                              data=serial.readAll();
                              totaldata=totaldata+data;
                              if(totaldata=="xFe/xD3/xFD")
                              {
                                  ui->myPaintWidget->setData(totaldata); // myPaintWidget is your custom widget
                                  ui->myPaintWidget->update();
                              }
                              }
                              

                              Put this custom widget where you want to paint.

                              "I think i need to study and learn more about qt and c ++" - this would help.

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

                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                Mucahit
                                wrote on last edited by Mucahit
                                #27

                                Thank you very much for the detailed explanation I think I understand my mistake. I have one last step left. I am getting this error : use of undeclared identifier 'update'. The same goes for ui->. I think I made a serious mistake in the class structure.

                                ---------mainwindow.h :

                                #ifndef MAINWINDOW_H
                                #define MAINWINDOW_H

                                #include <QMainWindow>
                                #include <QPainter>

                                namespace Ui {
                                class MainWindow;
                                }

                                class MainWindow : public QMainWindow
                                {
                                Q_OBJECT

                                public:
                                explicit MainWindow(QWidget *parent = 0);
                                ~MainWindow();
                                private:
                                Ui::MainWindow *ui;
                                protected:
                                void paintEvent(QPaintEvent *);

                                };
                                #endif // MAINWINDOW_H

                                --------------main cpp :

                                #include "mainwindow.h"
                                #include <QApplication>

                                int main(int argc, char *argv[])
                                {
                                QApplication a(argc, argv);
                                MainWindow w;
                                w.show();
                                return a.exec();
                                }

                                -------------mainwindow.cpp :

                                #include "mainwindow.h"
                                #include "ui_mainwindow.h"

                                #include<QSerialPort>
                                #include<QtSerialPort/QSerialPort>

                                QSerialPort serial;
                                #include<QDebug>

                                QByteArray data;
                                QByteArray totaldata;

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

                                serial.setPortName("ttyS0");
                                serial.setBaudRate(QSerialPort::Baud115200);
                                serial.setDataBits(QSerialPort::Data8);
                                serial.setParity(QSerialPort::NoParity);
                                serial.setStopBits(QSerialPort::OneStop);
                                serial.setFlowControl(QSerialPort::NoFlowControl);
                                serial.open(QIODevice::ReadWrite);
                                
                                if(serial.isOpen()==true)
                                {
                                ui->label_23->setText("serial opened");
                                }
                                else
                                {
                                ui->label_23->setText("serial closed");
                                }
                                

                                }

                                MainWindow::~MainWindow()
                                {
                                delete ui;
                                serial.close();
                                }
                                void QIODevice::readyRead()
                                {
                                data=serial.readAll();
                                totaldata=totaldata+data;

                                if(totaldata=="xFe/xD3/xFD")
                                {
                                qDebug()<<totaldata;
                                 ui->   // now i am getting this error
                                     update(); // same error
                                }
                                

                                }
                                void MainWindow::paintEvent(QPaintEvent *)
                                {
                                if(totaldata=="xFe/xD3/xFD")
                                {
                                qDebug()<<totaldata;

                                QPainter chart1(this);
                                    QRectF size1=QRectF(200,169,this->width()-720,this->width()-720);
                                    chart1.setBrush(Qt::blue);
                                    chart1.drawPie(size1,0,126*16);
                                    chart1.setBrush(Qt::red);
                                    chart1.drawPie(size1,126*16,234*16);
                                
                                    QPainter chart2(this);
                                    QRectF size2=QRectF(360,169,this->width()-720,this->width()-720);
                                    chart2.setBrush(Qt::blue);
                                    chart2.drawPie(size2,0,180*16);
                                    chart2.setBrush(Qt::red);
                                    chart2.drawPie(size2,180*16,180*16);
                                
                                    QPainter chart3(this);
                                    QRectF size3=QRectF(500,169,this->width()-720,this->width()-720);
                                    chart3.setBrush(Qt::blue);
                                    chart3.drawPie(size3,0,288*16);
                                    chart3.setBrush(Qt::red);
                                    chart3.drawPie(size3,288*16,72*16);
                                
                                }
                                else
                                {
                                QPainter chart1(this);
                                    QRectF size1=QRectF(200,169,this->width()-720,this->width()-720);
                                    chart1.setBrush(Qt::blue);
                                    chart1.drawPie(size1,0,0*16);
                                    chart1.setBrush(Qt::red);
                                    chart1.drawPie(size1,0*16,360*16);
                                
                                    QPainter chart2(this);
                                    QRectF size2=QRectF(360,169,this->width()-720,this->width()-720);
                                    chart2.setBrush(Qt::blue);
                                    chart2.drawPie(size2,0,0*16);
                                    chart2.setBrush(Qt::red);
                                    chart2.drawPie(size2,0*16,360*16);
                                
                                    QPainter chart3(this);
                                    QRectF size3=QRectF(500,169,this->width()-720,this->width()-720);
                                    chart3.setBrush(Qt::blue);
                                    chart3.drawPie(size3,0,0*16);
                                    chart3.setBrush(Qt::red);
                                    chart3.drawPie(size3,0*16,360*16);
                                
                                    QPainter rectangle(this);
                                    rectangle.setBrush(Qt::blue);
                                    rectangle.drawRect(QRect(200,264,10,10));
                                    rectangle.setBrush(Qt::blue);
                                    rectangle.drawRect(QRect(360,264,10,10));
                                    rectangle.setBrush(Qt::blue);
                                    rectangle.drawRect(QRect(505,264,10,10));
                                 }
                                

                                }

                                jsulmJ 1 Reply Last reply
                                0
                                • M Mucahit

                                  Thank you very much for the detailed explanation I think I understand my mistake. I have one last step left. I am getting this error : use of undeclared identifier 'update'. The same goes for ui->. I think I made a serious mistake in the class structure.

                                  ---------mainwindow.h :

                                  #ifndef MAINWINDOW_H
                                  #define MAINWINDOW_H

                                  #include <QMainWindow>
                                  #include <QPainter>

                                  namespace Ui {
                                  class MainWindow;
                                  }

                                  class MainWindow : public QMainWindow
                                  {
                                  Q_OBJECT

                                  public:
                                  explicit MainWindow(QWidget *parent = 0);
                                  ~MainWindow();
                                  private:
                                  Ui::MainWindow *ui;
                                  protected:
                                  void paintEvent(QPaintEvent *);

                                  };
                                  #endif // MAINWINDOW_H

                                  --------------main cpp :

                                  #include "mainwindow.h"
                                  #include <QApplication>

                                  int main(int argc, char *argv[])
                                  {
                                  QApplication a(argc, argv);
                                  MainWindow w;
                                  w.show();
                                  return a.exec();
                                  }

                                  -------------mainwindow.cpp :

                                  #include "mainwindow.h"
                                  #include "ui_mainwindow.h"

                                  #include<QSerialPort>
                                  #include<QtSerialPort/QSerialPort>

                                  QSerialPort serial;
                                  #include<QDebug>

                                  QByteArray data;
                                  QByteArray totaldata;

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

                                  serial.setPortName("ttyS0");
                                  serial.setBaudRate(QSerialPort::Baud115200);
                                  serial.setDataBits(QSerialPort::Data8);
                                  serial.setParity(QSerialPort::NoParity);
                                  serial.setStopBits(QSerialPort::OneStop);
                                  serial.setFlowControl(QSerialPort::NoFlowControl);
                                  serial.open(QIODevice::ReadWrite);
                                  
                                  if(serial.isOpen()==true)
                                  {
                                  ui->label_23->setText("serial opened");
                                  }
                                  else
                                  {
                                  ui->label_23->setText("serial closed");
                                  }
                                  

                                  }

                                  MainWindow::~MainWindow()
                                  {
                                  delete ui;
                                  serial.close();
                                  }
                                  void QIODevice::readyRead()
                                  {
                                  data=serial.readAll();
                                  totaldata=totaldata+data;

                                  if(totaldata=="xFe/xD3/xFD")
                                  {
                                  qDebug()<<totaldata;
                                   ui->   // now i am getting this error
                                       update(); // same error
                                  }
                                  

                                  }
                                  void MainWindow::paintEvent(QPaintEvent *)
                                  {
                                  if(totaldata=="xFe/xD3/xFD")
                                  {
                                  qDebug()<<totaldata;

                                  QPainter chart1(this);
                                      QRectF size1=QRectF(200,169,this->width()-720,this->width()-720);
                                      chart1.setBrush(Qt::blue);
                                      chart1.drawPie(size1,0,126*16);
                                      chart1.setBrush(Qt::red);
                                      chart1.drawPie(size1,126*16,234*16);
                                  
                                      QPainter chart2(this);
                                      QRectF size2=QRectF(360,169,this->width()-720,this->width()-720);
                                      chart2.setBrush(Qt::blue);
                                      chart2.drawPie(size2,0,180*16);
                                      chart2.setBrush(Qt::red);
                                      chart2.drawPie(size2,180*16,180*16);
                                  
                                      QPainter chart3(this);
                                      QRectF size3=QRectF(500,169,this->width()-720,this->width()-720);
                                      chart3.setBrush(Qt::blue);
                                      chart3.drawPie(size3,0,288*16);
                                      chart3.setBrush(Qt::red);
                                      chart3.drawPie(size3,288*16,72*16);
                                  
                                  }
                                  else
                                  {
                                  QPainter chart1(this);
                                      QRectF size1=QRectF(200,169,this->width()-720,this->width()-720);
                                      chart1.setBrush(Qt::blue);
                                      chart1.drawPie(size1,0,0*16);
                                      chart1.setBrush(Qt::red);
                                      chart1.drawPie(size1,0*16,360*16);
                                  
                                      QPainter chart2(this);
                                      QRectF size2=QRectF(360,169,this->width()-720,this->width()-720);
                                      chart2.setBrush(Qt::blue);
                                      chart2.drawPie(size2,0,0*16);
                                      chart2.setBrush(Qt::red);
                                      chart2.drawPie(size2,0*16,360*16);
                                  
                                      QPainter chart3(this);
                                      QRectF size3=QRectF(500,169,this->width()-720,this->width()-720);
                                      chart3.setBrush(Qt::blue);
                                      chart3.drawPie(size3,0,0*16);
                                      chart3.setBrush(Qt::red);
                                      chart3.drawPie(size3,0*16,360*16);
                                  
                                      QPainter rectangle(this);
                                      rectangle.setBrush(Qt::blue);
                                      rectangle.drawRect(QRect(200,264,10,10));
                                      rectangle.setBrush(Qt::blue);
                                      rectangle.drawRect(QRect(360,264,10,10));
                                      rectangle.setBrush(Qt::blue);
                                      rectangle.drawRect(QRect(505,264,10,10));
                                   }
                                  

                                  }

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #28

                                  @Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:

                                  void QIODevice::readyRead()

                                  What is this?!

                                  You need a slot in MainWindow which you connect to readyRead signal from your serial port:

                                  void MainWindow::readyRead()
                                  {
                                  ...
                                  }
                                  
                                  MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                      ui(new Ui::MainWindow)
                                  {
                                      ui->setupUi(this);
                                      connect(&serial, &QSerialPort::readyRead, this, &MainWindow::readyRead);
                                  ...
                                  }
                                  

                                  Please read https://doc.qt.io/qt-5/signalsandslots.html

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

                                  1 Reply Last reply
                                  2
                                  • M Offline
                                    M Offline
                                    Mucahit
                                    wrote on last edited by
                                    #29

                                    Oh !!! thank you so much. It turns out that I searched my mistake in many different places. Do you want me to mark the problem as resolved or delete the title or change the title? Because my question is very different with the question I asked. Thank you very much again.I made you struggle.

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • M Mucahit

                                      Oh !!! thank you so much. It turns out that I searched my mistake in many different places. Do you want me to mark the problem as resolved or delete the title or change the title? Because my question is very different with the question I asked. Thank you very much again.I made you struggle.

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #30

                                      @Mucahit Up to you. But if it is solved now you should mark it as solved.

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

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Mucahit
                                        wrote on last edited by
                                        #31

                                        Of course it is solved. Thank you very much for your efforts.

                                        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