0

我有一个程序列出了工作目录中的所有文件(我glib用来做这个),然后我在GtkWindow一个Gtk::Label. 我通过使用筛选窗口run()

  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "Zombie-Shadowchaser SixSixSix");
app->run(*pMainWindow);

我知道如何更改标签,set_label()我可以通过单击按钮将目录中的文件列表与筛选的列表同步。因此,如果我删除或创建文件,它将删除或添加到文件的标签中。但是我怎样才能让我的程序在不点击的情况下每秒同步呢?

4

1 回答 1

1

这是一个完整的示例,如果您想了解如何使用,也可以学习g_signal_connect()

#include <gtkmm.h>

Gtk::Label *plabel; // because I"m lazzy...

/**
 ** everytime a file is created in the current directory, toCallbackFunction()
 ** will be called. The paramaters are the same of signal see signal here :
 ** http://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/gio/GFileMonitor.html#GFileMonitor-changed
 **/
void
toCallbackFunction(GFileMonitor      *monitor
                  ,GFile             *file
                  ,GFile             *other_file
                  ,GFileMonitorEvent event_type
                  ,gpointer          user_data
                  ) 
{
  plabel->set_label( g_file_get_path(file) );
}



int 
main(int  argc 
    ,char *argv[]
    )
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
  Gtk::Window window;
  Gtk::Label label;
  window.set_default_size(800, 200);

  label.set_label("test");

  window.add(label);
  label.show();
  plabel = &label;

  /* 
   * g_file_monitor() requires a file, not a path. So we use g_file_new_for_path()
   * to convert the directory (this is for demonstration)
   */   
  GFile *file = g_file_new_for_path("."); 
  GFileMonitor *monitor;
  /*
   * http://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/gio/GFile.html#g-file-monitor
   */
  monitor = g_file_monitor_directory(file, G_FILE_MONITOR_NONE, nullptr, nullptr);
  /* 
   * the next line, is how to connect the monitor to a callback function when
   * the signal changed has been triggered.
   */
  g_signal_connect(monitor, "changed", G_CALLBACK (toCallbackFunction), nullptr);


  return app->run(window);
}

在 linux 上编译:

 g++ main.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs` -std=c++11

对于 MS Windows 用户,我不是种族主义者,但我不知道如何在 Windows 上编译。任何评论都将不胜感激,我自己编写了这段代码。谢谢报告任何错误。

如何使用它 :

启动程序时,将控制台放在同一目录中并创建一个新文件,例如,

 $ echo "stack" > overflow

你应该得到类似的东西:

在此处输入图像描述

感谢nemequ

于 2015-01-19T00:31:04.617 回答