Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to display Label for 30 seconds and then hide it?
Forum Updated to NodeBB v4.3 + New Features

How to display Label for 30 seconds and then hide it?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
32 Posts 8 Posters 20.3k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #15

    You're doing it wrong. Like @JKSH wrote, don't call GUI function from another thread. But that's not all, you have a potential infinite loop, you are executing database queries without checking that they did indeed ran. 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.

    You MainWindow is doing way to many things.

    The first thing to do is to clean things a bit:

    • Get the database initialization out of there. You should do it somewhere else, e.g. before your widget creation in main.cpp
    • 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.
    • Why mix std string and QString ? There's no real need in this case, just use QString.
    • Why do you need to sleep during 10ms ?
    • What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?
    • Hide is not a virtual function so your reimplementation in MainWindow is wrong.
    • What do you copy in CopyToUsb ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    MhM93M 1 Reply Last reply
    0
    • SGaistS SGaist

      You're doing it wrong. Like @JKSH wrote, don't call GUI function from another thread. But that's not all, you have a potential infinite loop, you are executing database queries without checking that they did indeed ran. 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.

      You MainWindow is doing way to many things.

      The first thing to do is to clean things a bit:

      • Get the database initialization out of there. You should do it somewhere else, e.g. before your widget creation in main.cpp
      • 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.
      • Why mix std string and QString ? There's no real need in this case, just use QString.
      • Why do you need to sleep during 10ms ?
      • What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?
      • Hide is not a virtual function so your reimplementation in MainWindow is wrong.
      • What do you copy in CopyToUsb ?
      MhM93M Offline
      MhM93M Offline
      MhM93
      wrote on last edited by
      #16

      @SGaist said:

      You're doing it wrong. Like @JKSH wrote, don't call GUI function from another thread. But that's not all, you have a potential infinite loop, you are executing database queries without checking that they did indeed ran. 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.

      You MainWindow is doing way to many things.

      The first thing to do is to clean things a bit:

      • Get the database initialization out of there. You should do it somewhere else, e.g. before your widget creation in main.cpp
      • 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.
      • Why mix std string and QString ? There's no real need in this case, just use QString.
      • Why do you need to sleep during 10ms ?
      • What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?
      • Hide is not a virtual function so your re implementation in MainWindow is wrong.
      • What do you copy in CopyToUsb ?

      Hi and thanks for reply.
      I have a lot of question :

      1- You said :"don't call GUI function from another thread". but why? 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?

      2- You said : " you have a potential infinite loop". my loop just listen the serial port and also check the USB port. If USB connected to device should get backup from database and save it in save.txt and copy that to USB.

      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? I open the file with bash system command as root (the user is root in my embed). Is it possible to not opening the file as root? (So I should make admin rule while I create the file?)

      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? And what happen if the database define in mainwindow.cpp?

      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?

      5- You said : "Why mix std string and QString ? There's no real need in this case, just use QString." Sorry. I correct it. this cause my little experience in writing code with c++ and Qt.

      6- You said : " Why do you need to sleep during 10ms ?" . When I read data from RFID card with EM-18 module reader. It sends data in 2 part. for example like this :
      RFID MONITORING => '010B7528'
      RFID MONITORING => '297E'
      but the real card ID is : '010B7528297E'
      I put that sleep and read data again and attach the both. (I do not find the reason why my data comes in 2 part)

      7- You said : "What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?" I want to copy that to text file with a specific format to get data from another application for example win base program to save data in SQL or MySQL or etc.

      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 .

      9-You said : "What do you copy in CopyToUsb ?" It is just copy save.txt to USB flash

      H.Ghassami

      JKSHJ 1 Reply Last reply
      0
      • MhM93M MhM93

        @SGaist said:

        You're doing it wrong. Like @JKSH wrote, don't call GUI function from another thread. But that's not all, you have a potential infinite loop, you are executing database queries without checking that they did indeed ran. 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.

        You MainWindow is doing way to many things.

        The first thing to do is to clean things a bit:

        • Get the database initialization out of there. You should do it somewhere else, e.g. before your widget creation in main.cpp
        • 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.
        • Why mix std string and QString ? There's no real need in this case, just use QString.
        • Why do you need to sleep during 10ms ?
        • What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?
        • Hide is not a virtual function so your re implementation in MainWindow is wrong.
        • What do you copy in CopyToUsb ?

        Hi and thanks for reply.
        I have a lot of question :

        1- You said :"don't call GUI function from another thread". but why? 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?

        2- You said : " you have a potential infinite loop". my loop just listen the serial port and also check the USB port. If USB connected to device should get backup from database and save it in save.txt and copy that to USB.

        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? I open the file with bash system command as root (the user is root in my embed). Is it possible to not opening the file as root? (So I should make admin rule while I create the file?)

        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? And what happen if the database define in mainwindow.cpp?

        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?

        5- You said : "Why mix std string and QString ? There's no real need in this case, just use QString." Sorry. I correct it. this cause my little experience in writing code with c++ and Qt.

        6- You said : " Why do you need to sleep during 10ms ?" . When I read data from RFID card with EM-18 module reader. It sends data in 2 part. for example like this :
        RFID MONITORING => '010B7528'
        RFID MONITORING => '297E'
        but the real card ID is : '010B7528297E'
        I put that sleep and read data again and attach the both. (I do not find the reason why my data comes in 2 part)

        7- You said : "What do you use save.txt for ? Wouldn't a copy of your sqlite database make more sense ?" I want to copy that to text file with a specific format to get data from another application for example win base program to save data in SQL or MySQL or etc.

        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 .

        9-You said : "What do you copy in CopyToUsb ?" It is just copy save.txt to USB flash

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #17

        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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        1
        • MhM93M Offline
          MhM93M Offline
          MhM93
          wrote on last edited by
          #18

          Really really thanks.

          H.Ghassami

          JKSHJ 1 Reply Last reply
          0
          • MhM93M MhM93

            Really really thanks.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #19

            @MhM93 said:

            Really really thanks.

            You're welcome, and good luck! :) Please let us know if you want anything clarified.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • MijazM Offline
              MijazM Offline
              Mijaz
              wrote on last edited by
              #20

              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 fleshing

              ui->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

              }

              jsulmJ 1 Reply Last reply
              0
              • MijazM Mijaz

                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 fleshing

                ui->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

                }

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

                @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

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

                MijazM 2 Replies Last reply
                1
                • jsulmJ jsulm

                  @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

                  MijazM Offline
                  MijazM Offline
                  Mijaz
                  wrote on last edited by
                  #22

                  @jsulm
                  thanks !
                  Will you send me an example where this function used, because I am unable to use it.

                  jsulmJ 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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

                    MijazM Offline
                    MijazM Offline
                    Mijaz
                    wrote on last edited by
                    #23

                    @jsulm
                    I my case, I tried to implement this but it shows error.

                    aha_1980A 1 Reply Last reply
                    0
                    • MijazM Mijaz

                      @jsulm
                      I my case, I tried to implement this but it shows error.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #24

                      @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

                      Qt has to stay free or it will die.

                      MijazM 1 Reply Last reply
                      0
                      • MijazM Mijaz

                        @jsulm
                        thanks !
                        Will you send me an example where this function used, because I am unable to use it.

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

                        @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.

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

                        1 Reply Last reply
                        0
                        • aha_1980A aha_1980

                          @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

                          MijazM Offline
                          MijazM Offline
                          Mijaz
                          wrote on last edited by
                          #26

                          @aha_1980
                          errr.png

                          aha_1980A JonBJ 2 Replies Last reply
                          0
                          • MijazM Mijaz

                            @aha_1980
                            errr.png

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #27

                            @Mijaz

                            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

                            Qt has to stay free or it will die.

                            MijazM 1 Reply Last reply
                            2
                            • MijazM Mijaz

                              @aha_1980
                              errr.png

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #28

                              @Mijaz

                              connect(..., ui->flashing_text->setText("..."));
                              

                              The last parameter to connect()must be either a function/method or a lambda, which in turn will execute the setText() 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.

                              1 Reply Last reply
                              4
                              • aha_1980A aha_1980

                                @Mijaz

                                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

                                MijazM Offline
                                MijazM Offline
                                Mijaz
                                wrote on last edited by
                                #29

                                @aha_1980
                                eeeer.png
                                I have created new simple project. it shows all ok but still not blinking.

                                jsulmJ 1 Reply Last reply
                                0
                                • MijazM Mijaz

                                  @aha_1980
                                  eeeer.png
                                  I have created new simple project. it shows all ok but still not blinking.

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

                                  @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...

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

                                  1 Reply Last reply
                                  2
                                  • MhM93M MhM93

                                    this is runread function :

                                    MainWindow::MainWindow(QWidget *parent) :
                                        QMainWindow(parent),
                                        ui(new Ui::MainWindow)
                                    {
                                        ui->setupUi(this);
                                        ui->statusBar->addWidget(ui->label,0);
                                        ui->label->setText("the status of device");
                                        ui->lbl_welcome->hide();
                                        //QTimer::singleShot(2000,ui->lbl_welcome,SLOT(hide()));
                                    
                                        QtConcurrent::run(this,&MainWindow::runread);
                                        //-----------------------
                                    
                                    }
                                    
                                    void MainWindow::runread()
                                    {
                                        //-----------------------
                                        fd=-1;
                                        db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                                        QString path = "VirtualDB";
                                        db.setDatabaseName(path);
                                    
                                        if(!db.open())
                                        {
                                            ui->label->setText("Error to open database");
                                        }
                                        else
                                        {
                                            QSqlQuery q;
                                            q.exec("create table IF NOT EXISTS Absent(card varchar(20) , "
                                                                    "DateTime varchar(50));");
                                            while(fd==-1)
                                            {
                                                openport();
                                    
                                                if (fd!=-1)
                                                {
                                                    configport();
                                                    ui->label->setText(" open port");
                                                    ui->label->setText("  Put your card please: ");
                                    
                                                    if(fd!=-1)
                                                    {
                                                        for(int i=0;i<100;i++)
                                                        {
                                                             if ((numbytes = read(fd,buf2, MAXDATASIZE)) != -1)
                                                             {
                                                                 ui->label->setText(" read card");
                                    
                                                                buf2[numbytes] = '\0';
                                                                usleep(10000);
                                                                QString s1(buf2);
                                                                memset(buf2,'\0',MAXDATASIZE);
                                                                read(fd,buf2, MAXDATASIZE-1) ;
                                                                QString s2(buf2);
                                                                memset(buf2,'\0',MAXDATASIZE);
                                                                string card=(s1+s2).toStdString();
                                                                QString dt=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
                                                                
                                                                q.prepare("insert into Absent(card,DateTime) values(?,?);");
                                                                q.bindValue(0,card.c_str());
                                                                q.bindValue(1,dt);
                                                                if(!q.exec())
                                                                    qDebug()<<q.lastError();
                                                                string result=(" WELL COME ");
                                                                
                                                                ui->lbl_welcome->show();
                                    //                            QTimer::singleShot(30000, ui->lbl_welcome,&QLabel::hide);
                                                                //QTimer::singleShot(2000,ui->lbl_welcome,SLOT(hide()));
                                                                hide();
                                    
                                                             }
                                                             else
                                                             {
                                                                 if(TransferDatatoUSB())
                                                                 {
                                                                     QString filename="save.txt";
                                                                     QFile file( filename );
                                    
                                                                    
                                                                     q.exec("select card,DateTime from Absent;");
                                    
                                                                         CreateFile();
                                                                         if ( file.open(QIODevice::ReadWrite) )
                                                                         {
                                                                             QTextStream stream( &file );
                                                                             while (q.next()) {
                                                                                 QString str=q.value(0).toString();
                                                                                 str.append("     ");
                                                                                 str.append(q.value(1).toString());
                                                                                 str.append(" ");
                                                                                 //o <<str.toStdString();
                                                                                 stream << str << endl;
                                                                         }
                                                                     }
                                                                     CopyToUSB();
                                                                 }
                                    
                                                             }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    
                                    }
                                    
                                    // I do this instead of QTimer but I thinck it is not true.(but it works)
                                    void MainWindow::hide()
                                    {
                                        //QTimer::singleShot(2000,ui->lbl_welcome,SLOT(hide()));
                                        sleep(2);
                                        ui->lbl_welcome->hide();
                                    }
                                    
                                    R Offline
                                    R Offline
                                    RAJALINGAM
                                    wrote on last edited by
                                    #31

                                    @MhM93
                                    thank you...

                                    R 1 Reply Last reply
                                    0
                                    • R RAJALINGAM

                                      @MhM93
                                      thank you...

                                      R Offline
                                      R Offline
                                      RAJALINGAM
                                      wrote on last edited by
                                      #32

                                      @RAJALINGAM
                                      can you please share your form window

                                      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