【问题标题】:Gtkmm 3/C++, closing the program with a button instead of the window "X"Gtkmm 3/C++,用按钮而不是窗口“X”关闭程序
【发布时间】:2018-03-27 09:33:06
【问题描述】:

大家。

我正在开发一个 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 的成员... 好吧,一步一步往前走。

【问题讨论】:

  • 现在我知道为什么我真的很喜欢用 C 编写 gtk 了,您只需处理 "delete-event" 就可以了。

标签: c++ gtkmm3


【解决方案1】:

使用了 FormMain::hide()(继承自 GtkWindow)。编译器无法识别静态过程 Gtk::Main::hide()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    相关资源
    最近更新 更多