Search code examples
c++gtkmm

Asynchronous Loading of images in gtkmm


Currently, in my project, we are using gtkmm pixbuf create_from_file or create_from_date which hangs up whole GUI for 1-2 seconds in case of high-resolution images and in case of loading multiple images for a screen it becomes awfully slow. Is it possible to load images asynchronously in gtkmm for the above two functions? I am able to find methods in gtk for loading images asynchronously but not in gtkmm. An example would be helpful since I am unable to find anything related to it.

  if(!imageName.empty())
  {
    //Load image in pixbuf
    picPixBuff = Gdk::Pixbuf::create_from_file(imageName);

    picPixBuff = picPixBuff->scale_simple(150,35,Gdk::INTERP_BILINEAR);

   }

I have gone through this. Related Question - How to load a widget as a different thread in gtk? (vala)


Solution

  • There are docs for that. That example does exactly what you are asking for.

    These are the functions that do the magic. The example is easy to follow.

    // notify() is called from ExampleWorker::do_work(). It is executed in the worker
    // thread. It triggers a call to on_notification_from_worker_thread(), which is
    // executed in the GUI thread.
    void ExampleWindow::notify()
    {
      m_Dispatcher.emit();
    }
    
    void ExampleWindow::on_notification_from_worker_thread()
    {
      if (m_WorkerThread && m_Worker.has_stopped())
      {
        // Work is done.
        if (m_WorkerThread->joinable())
          m_WorkerThread->join();
        delete m_WorkerThread;
        m_WorkerThread = nullptr;
        update_start_stop_buttons();
      }
      update_widgets();
    }