这是一个完整的示例,如果您想了解如何使用,也可以学习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
你应该得到类似的东西:
![在此处输入图像描述](https://i.stack.imgur.com/8NAqy.png)
感谢nemequ