大家。
我正在开发一个 gtkmm 应用程序,需要一些帮助才能让“关闭”按钮工作。正如 gtkmm 文档所建议的,我为主窗口对象派生了一个类,创建了一些成员,并将 main() 函数主要用于读取 glade UI 文件、实例化表单和启动主循环。
有3个文件,命名方便解释:Declarations.h、Declarations.cpp、Program.cpp
在“Declarations.h”中,我有从 Gtk 窗口继承的类:
#include <gtkmm.h>
class MainWindowClass : public Gtk::ApplicationWindow
{
protected:
Gtk::Button *button_close;
// other buttons here
protected:
void on_button_close_clicked();
// other callback functions here
public:
MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade); // Constructor
// Destructor, other public members
};
在“Declarations.cpp”中,我有实现:
#include "Declarations.h"
using namespace Gtk;
// Implementing the constructor
MainWindowClass::MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade) :
Gtk::Window(cobject), builder(refGlade)
{
builder->get_widget("button_close", button_close);
// Getting other widgets from the glade file
button_close->signal_clicked().connect(sigc::mem_fun(*this, &MainWindowClass::on_button_close_clicked));
// Connecting other callback functions here
}
// Implementing the callback for the "Close" button, ** PROBLEM IS HERE **
void MainWindowClass::on_button_close_clicked()
{
//gtk_main_quit(); Apparently GTK+/C only, compiler doesn't complain but causes a segfault when clicking the button
//Gtk::Application::quit(); Won't compile
}
Program.cpp 从文件中读取 UI 并启动主程序循环:
#include <gtkmm.h>
#include "Declarations.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "Damn this close button");
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("Program_UI.glade");
MainWindowClass our_main_window;
return app->run(our_main_window);
}
我省略了一些不相关的代码(其他对象和回调的),因为它们可以工作,这是导致我麻烦的关闭过程,尽管使用“X”关闭应用程序有效。我还考虑过尝试调用“app”的quit() 或destroy() 函数(如果它们存在),但是回调函数不知道“app”存在。你们有什么建议?
非常感谢。
** 编辑:使用继承自 GtkWindow 的 FormMain::hide() 修复此问题。我认为静态过程 Gtk::Main::hide() 会做到这一点,但编译器说 hide() 不是 Gtk::Main 的成员......好吧,一次向前迈出一步。