Solved How to exit void QIODevice::readyRead() function ? Is this function also interrupt?
-
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.
-
@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 -
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 ?
-
@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... -
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.
-
@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.
-
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_OBJECTpublic:
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)); }
}
-
@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
-
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.
-
@Mucahit Up to you. But if it is solved now you should mark it as solved.
-
Of course it is solved. Thank you very much for your efforts.