Funkce při nečinnosti

Když chcete určit metodu, která má být zavolána, když se nic jiného neděje, použijte následující:

sigc::connection  Glib::SignalIdle::connect(const sigc::slot<bool()>& slot,
                                    int priority = Glib::PRIORITY_DEFAULT_IDLE);

Způsobí to, že gtkmm zavolá určenou metodu kdykoliv, když se nic neděje. Můžete přidat prioritu (nižší čísla znamenají vyšší prioritu). Jsou dva způsoby, jak obsluhu signálu odstranit: zavolat disconnect() na objektu sigc::connection, nebo vrátit false z obsluhy signálu, která byla deklarována následovně:

bool idleFunc();

Protože je tahle metoda velmi podobná metodě rozebrané v předchozím, mělo by tohle vysvětlení k pochopení fungování stačit. Pro jistotu je zde ale příklad:

Source Code

File: idleexample.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLE_IDLEEXAMPLE_H
#define GTKMM_EXAMPLE_IDLEEXAMPLE_H

#include <gtkmm.h>
#include <iostream>

class IdleExample : public Gtk::Window
{
public:
  IdleExample();

protected:
  // Signal Handlers:
  bool on_timer();
  bool on_idle();
  void on_button_clicked();

  // Member data:
  Gtk::Box m_Box;
  Gtk::Button m_ButtonQuit;
  Gtk::ProgressBar m_ProgressBar_c;
  Gtk::ProgressBar m_ProgressBar_d;
};

#endif // GTKMM_EXAMPLE_IDLEEXAMPLE_H

File: main.cc (For use with gtkmm 4)

#include "idleexample.h"
#include <gtkmm/application.h>

int main (int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  return app->make_window_and_run<IdleExample>(argc, argv);
}

File: idleexample.cc (For use with gtkmm 4)

#include "idleexample.h"

IdleExample::IdleExample() :
  m_Box(Gtk::Orientation::VERTICAL, 5),
  m_ButtonQuit("_Quit", true)
{
  m_Box.set_margin(5);

  // Put buttons into container

  // Adding a few widgets:
  set_child(m_Box);
  m_Box.append(*Gtk::make_managed<Gtk::Label>("Formatting Windows drive C:"));
  m_Box.append(*Gtk::make_managed<Gtk::Label>("100 MB"));
  m_Box.append(m_ProgressBar_c);
  m_ProgressBar_c.set_expand();

  m_Box.append(*Gtk::make_managed<Gtk::Label>(""));

  m_Box.append(*Gtk::make_managed<Gtk::Label>("Formatting Windows drive D:"));
  m_Box.append(*Gtk::make_managed<Gtk::Label>("5000 MB"));
  m_Box.append(m_ProgressBar_d);
  m_ProgressBar_d.set_expand();

  auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL,10);
  m_Box.append(*hbox);
  hbox->append(m_ButtonQuit);
  m_ButtonQuit.set_expand();
  m_ButtonQuit.set_halign(Gtk::Align::END);
  m_ButtonQuit.set_valign(Gtk::Align::END);

  // Connect the signal handlers:
  m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this,
              &IdleExample::on_button_clicked) );

  // formatting drive c in timeout signal handler - called once every 50ms
  Glib::signal_timeout().connect( sigc::mem_fun(*this, &IdleExample::on_timer),
          50 );

  // formatting drive d in idle signal handler - called as quickly as possible
  Glib::signal_idle().connect( sigc::mem_fun(*this, &IdleExample::on_idle) );
}


void IdleExample::on_button_clicked()
{
  hide();
}

// this timer callback function is executed once every 50ms (set in connection
// above).  Use timeouts when speed is not critical. (ie periodically updating
// something).
bool IdleExample::on_timer()
{
  double value = m_ProgressBar_c.get_fraction();

  // Update progressbar 1/500th each time:
  m_ProgressBar_c.set_fraction(value + 0.002);

  return value < 0.99;  // return false when done
}


// This idle callback function is executed as often as possible, hence it is
// ideal for processing intensive tasks.
bool IdleExample::on_idle()
{
  double value = m_ProgressBar_d.get_fraction();

  // Update progressbar 1/5000th each time:
  m_ProgressBar_d.set_fraction(value + 0.0002);

  return value < 0.99;  // return false when done
}

Tento příklad ukazuje malý rozdíl mezi metodou idle a timeout. Když potřebujete metodu, která je volána pravidelně a rychlost není důležitá, pak budete chtít metodu timeout. Pokud potřebujete takovou, která je volána tak často, jak je to možné (např. výpočet fraktálu na pozadí), pak použijte metodu idle.

Zkuste příklad spustit a zvýšit zátěž systému. Horní ukazatel průběhu bude nabývat rovnoměrně, dolní se zpomalí.