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 render a gif while a function is running?
Forum Updated to NodeBB v4.3 + New Features

How to render a gif while a function is running?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 864 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.
  • L Offline
    L Offline
    leonardort
    wrote on last edited by leonardort
    #1

    Hi,
    I'm new in this forum and I hope to write in the right section.
    I'm writing an application with Qt and I have the Login page. In this page, when I click the "loginButton" it starts a request to the server.
    This request is inside the on_loginButton_clicked() slot.
    If the server is on, there are no problems.
    Otherwise, I would like to insert a loading gif, in order to give a feedback to the user during the attempt.
    The problem is that, with QCoreApplication::processevents() the gif is showed but it is static. It "starts" only when the slot is finished.
    I read a lot of thing, maybe I can solve with a Qthread but I don't know where I can insert it.

    I attach here the important parts of the slot:

    
    void LoginWindow::on_loginButton_clicked() {
    	ui.errLabel->hide();
    	ui.notifyLabel->hide();  // here I set the gif
    
    	movie = new QMovie(":/images/loading.gif");
    	if (!movie->isValid()) {
    		ui.notifyLabel->setText("<loading.gif>");
    		auto error = movie->lastErrorString();
    	}
    
    
    	movie->setScaledSize(this->size());
    	ui.notifyLabel->setMovie(movie);
    	movie->start();
    	ui.notifyLabel->show();
    	
    	QCoreApplication::processEvents();
    	
    	//[.....] Connection with server
    }
    

    Thank you for your help!

    JonBJ 1 Reply Last reply
    0
    • L leonardort

      Hi,
      I'm new in this forum and I hope to write in the right section.
      I'm writing an application with Qt and I have the Login page. In this page, when I click the "loginButton" it starts a request to the server.
      This request is inside the on_loginButton_clicked() slot.
      If the server is on, there are no problems.
      Otherwise, I would like to insert a loading gif, in order to give a feedback to the user during the attempt.
      The problem is that, with QCoreApplication::processevents() the gif is showed but it is static. It "starts" only when the slot is finished.
      I read a lot of thing, maybe I can solve with a Qthread but I don't know where I can insert it.

      I attach here the important parts of the slot:

      
      void LoginWindow::on_loginButton_clicked() {
      	ui.errLabel->hide();
      	ui.notifyLabel->hide();  // here I set the gif
      
      	movie = new QMovie(":/images/loading.gif");
      	if (!movie->isValid()) {
      		ui.notifyLabel->setText("<loading.gif>");
      		auto error = movie->lastErrorString();
      	}
      
      
      	movie->setScaledSize(this->size());
      	ui.notifyLabel->setMovie(movie);
      	movie->start();
      	ui.notifyLabel->show();
      	
      	QCoreApplication::processEvents();
      	
      	//[.....] Connection with server
      }
      

      Thank you for your help!

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

      @leonardort
      What sort of calls are going on inside your
      //[.....] Connection with server
      ? I'm guessing they are "blocking"? Do you have some asynchronous option? Or can you push that out to a thread? Or use Qt network calls which do signals & slots?

      L 1 Reply Last reply
      2
      • L Offline
        L Offline
        leonardort
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • JonBJ JonB

          @leonardort
          What sort of calls are going on inside your
          //[.....] Connection with server
          ? I'm guessing they are "blocking"? Do you have some asynchronous option? Or can you push that out to a thread? Or use Qt network calls which do signals & slots?

          L Offline
          L Offline
          leonardort
          wrote on last edited by leonardort
          #4

          @JonB
          I have the creation of a socket, connection to it and the send of a packet.
          Yes they are "blocking" but I want that the loading gif starts BEFORE starting all these operations.

          jsulmJ 1 Reply Last reply
          0
          • L leonardort

            @JonB
            I have the creation of a socket, connection to it and the send of a packet.
            Yes they are "blocking" but I want that the loading gif starts BEFORE starting all these operations.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @leonardort Loading GIF before starting these operations will not change anything as blocking calls block event loop which is needed to play the GIF. If you can't switch to an assynchronous API move your blocking operations to a thread as @JonB suggested.s

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

            L 1 Reply Last reply
            3
            • jsulmJ jsulm

              @leonardort Loading GIF before starting these operations will not change anything as blocking calls block event loop which is needed to play the GIF. If you can't switch to an assynchronous API move your blocking operations to a thread as @JonB suggested.s

              L Offline
              L Offline
              leonardort
              wrote on last edited by
              #6

              @jsulm Do you mean a normal thread or a Qthread?
              So, your suggestion is to put the connection with server into a thread, not the movie->start(). Right?
              Can you give me a small example?

              jsulmJ 1 Reply Last reply
              0
              • L leonardort

                @jsulm Do you mean a normal thread or a Qthread?
                So, your suggestion is to put the connection with server into a thread, not the movie->start(). Right?
                Can you give me a small example?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @leonardort said in How to render a gif while a function is running?:

                Do you mean a normal thread or a Qthread?

                QThread is simply a class which manages a thread.

                You would need to move all these blocking calls into a thread.
                Please start here, you will find examples also: https://doc.qt.io/qt-5/thread-basics.html

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

                L 1 Reply Last reply
                4
                • jsulmJ jsulm

                  @leonardort said in How to render a gif while a function is running?:

                  Do you mean a normal thread or a Qthread?

                  QThread is simply a class which manages a thread.

                  You would need to move all these blocking calls into a thread.
                  Please start here, you will find examples also: https://doc.qt.io/qt-5/thread-basics.html

                  L Offline
                  L Offline
                  leonardort
                  wrote on last edited by
                  #8

                  @jsulm ok, thank you very much.
                  Using threads something change. The problem is that in a first moment I updated some labels in the UI in the slot, while now I have to manage it with a return code.

                  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