Solved How to display Label for 30 seconds and then hide it?
-
Hi,
@MhM93 said:
1- You said :"don't call GUI function from another thread". but why?
Because, like I wrote before, GUI classes are not thread safe. Their functions must only be called from one thread. This article should provide more info: http://doc.qt.io/qt-5/threads-reentrancy.html
Let us know if something is not clear.
I need a thread to read data from serial port and listen on it. when receive data, send it to my database, so i decide to use fork. but fork not work in widget programing (as I see. It just works on console application.)
If my code is wrong so how can I listen on port without hanging my embedded device?First, learn how to use Qt's signals and slots: http://doc.qt.io/qt-5/signalsandslots.html
Then, use signals to communicate between different threads. Do not call GUI class functions from another thread.
2- You said : " you have a potential infinite loop". my loop just listen the serial port and also check the USB port.
Is it guaranteed that
fd == -1
at some point?3- You said : "You also don't warn when save.txt doesn't open and if you install your application in a system folder it will fail because you won't have the rights to do that." . How to check that?
Check the return value of
QFile::open()
.Also, think: What would you do if the program fails to open the file for some reason?
3- You said : " Get the database initialization out of there. You should do it somewhere else, e.g. before your widget creation in main.cpp" . Always the database should define in main.cpp? This is the best?
It doesn't have to be defined in main.cpp. You can also define it in another class.
And what happen if the database define in mainwindow.cpp?
Then your code becomes messy.
Ideally, mainwindow.cpp should only contain code that is related to the GUI. The serial port and the database are not related to the GUI.
4- You said : "What does openport do ? Do you really need to call it each time you want to read something ? Depending on what it is, you should have a small helper class that handles the communication to that card reader. That helper class should emit signals that you connect your MainWindow to update your GUI." Openport is a function to open the device, when I want to listen the port I call it.( just one time I call this function ) could you give me a reference for define a helper class that you mentioned?
There is a simple example in the QThread documentation: http://doc.qt.io/qt-5/qthread.html
8- You said : "Hide is not a virtual function so your re implementation in MainWindow is wrong." Hide is just a simple function that I define instead of QTimer::singleShot(... , ... , ...) . I can change the name of my method. It is not re implementation .
It is best not to use the same name as an existing function.
-
Really really thanks.
-
@MhM93 said:
Really really thanks.
You're welcome, and good luck! :) Please let us know if you want anything clarified.
-
ui->lbl->setVisible(true); before setText
QTimer::singleShot(1000, ui->lbl, &QLabel::hide);
above command just hide the text after 1 second but never blink again.
I want to blink my lbl text continuously until I click to stop button. so how it possible. guide, please me.This is the code in my case
void secBox::on_APush_clicked()
{
// need start fleshingui->flashing_text->setVisible(true); QTimer::singleShot(1000, ui->flashing_text, &QLabel::hide); //QTimer::singleShot(1000, ui->flashing_text, &QLabel::show); // this does not works
}
void secBox::on_BPush_clicked()
{
// need stop fleshing}
-
@Mijaz If you want the label blinking then don't use singleShot - as the name says it just fires once.
Create a QTimer instance, set interval and connect a slot to https://doc.qt.io/qt-5/qtimer.html#timeout -
@jsulm
thanks !
Will you send me an example where this function used, because I am unable to use it. -
@jsulm
I my case, I tried to implement this but it shows error. -
@Mijaz said in How to display Label for 30 seconds and then hide it?:
I my case, I tried to implement this but it shows error.
Which error exactly? Please show your code.
Regards
-
@Mijaz said in How to display Label for 30 seconds and then hide it?:
Will you send me an example
No, because there are already examples (you can find them in the link I sent you before):
See also QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, Analog Clock Example, and Wiggly Example. -
-
You create a new timer everytime your slot
on_APush_clicked()
is called. Make the timer a member variable and create it once.timer->start()
without parameters should work.Then the only needed step is to create a slot that does whatever you want.
Regards
-
connect(..., ui->flashing_text->setText("..."));
The last parameter to
connect()
must be either a function/method or a lambda, which in turn will execute thesetText()
you show. You cannot have it be the actual function call you are trying. This is the cause of the error message you see.It would help you if you read up on Qt signals & slots, https://doc.qt.io/qt-5/signalsandslots.html.
-
@aha_1980
I have created new simple project. it shows all ok but still not blinking. -
@Mijaz In the screen-shot you posted you still call singleShot!
Did you read the link I posted? There are even examples:QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update)); timer->start(1000);
Just connect your own slot to timeout and in that slot toggle the label...
-
@MhM93
thank you... -
@RAJALINGAM
can you please share your form window