Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to show a text for 3 sec?
Qt 6.11 is out! See what's new in the release blog

How to show a text for 3 sec?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.4k 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 Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    I want to show a qlabel for 3 sec and used the following code. The label is initially hidden.

    ui->qlabel->show();
    QThread::sleep(3);
    ui->qlabel->hide();
    

    But the label didn't show up at all. I tried to comment ui->qlabel->hide(); and the label appeared after 3 sec waiting. What's wrong with these steps?

    1 Reply Last reply
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hi,
      By using QThread::sleep(), you are blocking the event loop, which can cause the effect you are describing.

      Do that instead:

      ui->qlabel->show();
      QTimer::singleShot(3000, ui->qlabel, &QWidget::hide);
      

      You should avoid as much as possible usage of QThread::sleep(), QThread::msleep() or QThread::usleep() from the GUI (main) thread, try to use signal & slots or QTimer to make this kind of behavior to keep you application responsive and avoid "freezing" of the window.

      M 1 Reply Last reply
      7
      • Gojir4G Gojir4

        Hi,
        By using QThread::sleep(), you are blocking the event loop, which can cause the effect you are describing.

        Do that instead:

        ui->qlabel->show();
        QTimer::singleShot(3000, ui->qlabel, &QWidget::hide);
        

        You should avoid as much as possible usage of QThread::sleep(), QThread::msleep() or QThread::usleep() from the GUI (main) thread, try to use signal & slots or QTimer to make this kind of behavior to keep you application responsive and avoid "freezing" of the window.

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @Gojir4 said in How to show a text for 3 sec?:

        Hi,
        By using QThread::sleep(), you are blocking the event loop, which can cause the effect you are describing.

        Do that instead:

        ui->qlabel->show();
        QTimer::singleShot(3000, ui->qlabel, &QWidget::hide);
        

        You should avoid as much as possible usage of QThread::sleep(), QThread::msleep() or QThread::usleep() from the GUI (main) thread, try to use signal & slots or QTimer to make this kind of behavior to keep you application responsive and avoid "freezing" of the window.

        This worked perfectly. Thanks so much!

        I have another question. Is it possible to set a fade effect when the label is hiding or showing?

        jsulmJ 1 Reply Last reply
        0
        • M MasterBlade

          @Gojir4 said in How to show a text for 3 sec?:

          Hi,
          By using QThread::sleep(), you are blocking the event loop, which can cause the effect you are describing.

          Do that instead:

          ui->qlabel->show();
          QTimer::singleShot(3000, ui->qlabel, &QWidget::hide);
          

          You should avoid as much as possible usage of QThread::sleep(), QThread::msleep() or QThread::usleep() from the GUI (main) thread, try to use signal & slots or QTimer to make this kind of behavior to keep you application responsive and avoid "freezing" of the window.

          This worked perfectly. Thanks so much!

          I have another question. Is it possible to set a fade effect when the label is hiding or showing?

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

          @MasterBlade See http://doc.qt.io/qt-5/animation-overview.html

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

          1 Reply Last reply
          1
          • Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #5

            I think this should do the job (not tested and compiled):

            //mywidget.h
            QPropertyAnimation *m_animation;
            
            //mywidget.cpp
            //Constructor
            void MyWidget::MyWidget(QWidget *parent) 
                : QWidget(parent), ui(new Ui::MyWidget)
            {
                //...    
                m_animation = new QPropertyAnimation(ui->qlabel, "windowOpacity", this);    
                //See QPropertyAnimation documentation for more parameters
                m_animation->setDuration(1000);    
                m_animation->setStartValue(0.0);
                m_animation->setEndValue(1.0);
                //...
            }
            
            void MyWidget::showLabel(int duration)
            {   
                ui->qlabel->setWindowOpacity(0.0);
                ui->qlabel->show();
                m_animation->setDirection(QPropertyAnimation::Forward);
                m_animation->start();    
                //Ensure it's visible with 100% opacity at least 1 sec. 
                QTimer::singleShot(qMax(2000, duration), this, &MyWidget::hideLabel);
            }
            
            void MyWidget::hideLabel()
            {
                m_animation->setDirection(QPropertyAnimation::Backward);
                m_animation->start();
                QTimer::singleShot(m_animation.duration(), ui->qlabel, &QWidget::hide);
            }
            
            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved