Solved How to exit void QIODevice::readyRead() function ? Is this function also interrupt?
-
Of course,
QByteArray data;
QByteArray totaldata;void QIODevice::readyRead()
{
data=serial.readAll();
totaldata=totaldata+data;
}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
} -
@Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:
I want to print the data I receive here because I will draw pies based on the data I received
And what is the problem?
-
Does not print this data without moving the mouse. So I can't get out of this readyread function and switch to paintevent.
-
@Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:
paintEvent is not being called, just because totalData changed ...
try calling update() it will schedule a paint event call at the next possible time.
void QIODevice::readyRead() { data=serial.readAll(); totaldata=totaldata+data; update(); }
-
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();
} -
@Mucahit said in How to exit void QIODevice::readyRead() function ? Is this function also interrupt?:
QMainWindow main;
main.update();You really can't do this. That is a local variable, it has nothing to do with your main window. This is basic C++ stuff.
-
@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
-
@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? -
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.
-
@Mucahit You can redraw completely based on updated data. If paintEvent() is called the previously drawn content is deleted.
-
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 ?
-
@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.
-
I understood very well what you said. Yes i have a protocol but i can't trigger paintevent.
-
@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.
-
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.