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. Playing Gif Synchronously while Other Process is Running
Forum Updated to NodeBB v4.3 + New Features

Playing Gif Synchronously while Other Process is Running

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 3 Posters 1.2k Views 1 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.
  • Q Offline
    Q Offline
    qtross
    wrote on last edited by
    #1

    Hello,

    What I am trying to do with my gui application is;
    I would like to play a gif when user click a button which calls a function. And this function read images in same folder and find their contrast value with Laplacian method for comparing which image is more focused.
    Anyway, I intend to play a gif movie in label of the gui while this procedure is running in background.

    I tried QApplication::processEvents(); which does not work as I wanted. Movie starts after procedure is finished.

    I am confused if I should use;
    "<QtConcurrentRun> - Asynchronous Run" from here.
    or I need to create another thread for running calling void here

    What I have;

    Main.cpp

    #include "screen.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        screen w;
        w.show();
    
        return a.exec();
    }
    

    screen.cpp:

    #include "screen.h"
    #include "ui_screen.h"
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <iomanip>
    #include <stdlib.h>
    #include <stdio.h>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <QString>
    #include <QMovie>
    
    
    
    
    using namespace cv;
    using namespace std;
    
    screen::screen(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::screen)
    {
        ui->setupUi(this);
        movie = new QMovie("loading.gif");
        ui->splash->setMovie(movie);
        connect(ui->pushButton, SIGNAL(clicked()), movie, SLOT(start()));
    
    }
    
    screen::~screen()
    {
        delete ui;
    }
    
    double modifiedLaplacian(const cv::Mat& src)
    {
        cv::Mat M = (Mat_<double>(3, 1) << -1, 2, -1);
        cv::Mat G = cv::getGaussianKernel(3, -1, CV_64F);
    
        cv::Mat Lx;
        cv::sepFilter2D(src, Lx, CV_64F, M, G);
    
        cv::Mat Ly;
        cv::sepFilter2D(src, Ly, CV_64F, G, M);
    
        cv::Mat FM = cv::abs(Lx) + cv::abs(Ly);
    
        double focusMeasure = cv::mean(FM).val[0];
        return focusMeasure;
    }
    
    void screen::on_pushButton_clicked()
    {
    processimages();
    QApplication::processEvents();
    
    }
    
    void screen::processimages(){
        vector<string> paths;
        string path;
        double result_of_laplacian;
        for (int i=0; i<10; i++) {  // load images in same folder
            path=std::to_string(i)+".png";
            paths.push_back(path);
    
        }
    
        for (int i =0; i<10; i++){
        cout<<"am in "<<endl;
        Mat image=imread(paths[i]);
        result_of_laplacian=modifiedLaplacian(image);
        cout<<"laplacian method:"<<result_of_laplacian<<endl;
    
        }
    }
    

    Thank you for any suggestions or help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are blocking the event loop while doing your processing.

      QtConcurrent::run would indeed be a good alternative to move your processing to another thread.

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

      Q 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        You are blocking the event loop while doing your processing.

        QtConcurrent::run would indeed be a good alternative to move your processing to another thread.

        Q Offline
        Q Offline
        qtross
        wrote on last edited by
        #3

        @SGaist said in Playing Gif Synchronously while Other Process is Running:

        QtConcurrent::run

        Thank you for the reply. I have tried QtConcurrent::run, it works just fine now.
        Here how i call the function now;

        QFuture<void> future = QtConcurrent::run(this,&screen::processimages);
        

        What I want to learn now, How can i this function If the function has it's member with QtConcurrent.

        For instance; I have this void;

        void screen::processimages(vector<strings> paths) { ... do something ..}
        
        

        I have tried this;

        vector<strings> allthepaths;
        QFuture<void> future = QtConcurrent::run(this,&screen::processimages, allthepaths);
        

        It did not work
        How can i call this function now?
        Thanks

        J.HilkJ 1 Reply Last reply
        0
        • Q qtross

          @SGaist said in Playing Gif Synchronously while Other Process is Running:

          QtConcurrent::run

          Thank you for the reply. I have tried QtConcurrent::run, it works just fine now.
          Here how i call the function now;

          QFuture<void> future = QtConcurrent::run(this,&screen::processimages);
          

          What I want to learn now, How can i this function If the function has it's member with QtConcurrent.

          For instance; I have this void;

          void screen::processimages(vector<strings> paths) { ... do something ..}
          
          

          I have tried this;

          vector<strings> allthepaths;
          QFuture<void> future = QtConcurrent::run(this,&screen::processimages, allthepaths);
          

          It did not work
          How can i call this function now?
          Thanks

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @qtross said in Playing Gif Synchronously while Other Process is Running:

          I have tried this;

          vector<strings> allthepaths;
          QFuture<void> future = QtConcurrent::run(this,&screen::processimages, allthepaths);
          

          It did not work
          How can i call this function now?
          Thanks

          that should work, probably, the type vector and strings is not registered correctly in the meta system.

          If you use QList<QString> it should work fine out of the box.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          Q 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @qtross said in Playing Gif Synchronously while Other Process is Running:

            I have tried this;

            vector<strings> allthepaths;
            QFuture<void> future = QtConcurrent::run(this,&screen::processimages, allthepaths);
            

            It did not work
            How can i call this function now?
            Thanks

            that should work, probably, the type vector and strings is not registered correctly in the meta system.

            If you use QList<QString> it should work fine out of the box.

            Q Offline
            Q Offline
            qtross
            wrote on last edited by
            #5

            @J.Hilk Yes you're right that works. But in big images which cause long progress it just cancels and i need to run 2-3 times to make it working. I did not understand why that happens. Maybe i am doing something silly. I found this page I guess i need to move this thread to another thread and give some signals for finishing this loop, in long processes it just does not finish the background thread in first attempt.

            Thanks

            J.HilkJ 1 Reply Last reply
            0
            • Q qtross

              @J.Hilk Yes you're right that works. But in big images which cause long progress it just cancels and i need to run 2-3 times to make it working. I did not understand why that happens. Maybe i am doing something silly. I found this page I guess i need to move this thread to another thread and give some signals for finishing this loop, in long processes it just does not finish the background thread in first attempt.

              Thanks

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @qtross
              it shouldn't simply cancel.

              Can you post some more of your code, or better yet, a simply minimal compileable example for us to test ourselves


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Q 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @qtross
                it shouldn't simply cancel.

                Can you post some more of your code, or better yet, a simply minimal compileable example for us to test ourselves

                Q Offline
                Q Offline
                qtross
                wrote on last edited by
                #7

                @J.Hilk Hi again, I have found what the problem was. I can't share all of my codes codes but I will share how to solve it. While my function which is in another thread works still in process, my main thread calls the result and paint it in the label. Therefore I saw my final result in first or third attempt which was luck I guess.
                I solved it with emitting finished signal at the end of the qtconcurrent function.

                Here how I do that, which may be helpful to others;

                I created a signal function in header file;

                void threadfinished();
                

                And added at end of the qtconcurrent function;

                emit threadfinished();
                

                and connect function after thread;

                connect(this, &screen::threadfinished, this, &screen::startafterthreadfinished);
                

                Thanks

                1 Reply Last reply
                3

                • Login

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