gpt4 book ai didi

c++ - Gtk::Main 和 Gtk::Application::create 之间有什么区别?

转载 作者:行者123 更新时间:2023-11-30 03:23:26 25 4
gpt4 key购买 nike

两者都创建 Gtk 窗口,但我无法真正理解幕后发生的事情。我尝试将信号绑定(bind)到按钮以退出窗口,但程序在使用 Gtk::Application::create 时收到 SIGSEGV。当我更改程序以遵循 Gtk::Main 约定时。一切正常。

Gtk::Application::create 程序(无法工作):

auto app = Gtk::Application::create(argc, argv);
Gtk::Button *button = new Gtk::Button("Quit");
button->signal_clicked().connect(sigc::ptr_fun(Gtk::Main::quit));
Gtk::Window window;
window.set_default_size(200, 200);
window.add(*button);
button->show();
return app->run(window);

Gtk::Main 程序(有效):

auto app = Gtk::Main(argc, argv);
Gtk::Button *button = new Gtk::Button("Quit");
button->signal_clicked().connect(sigc::ptr_fun(app.quit));
Gtk::Window window;
window.set_default_size(200, 200);
window.add(*button);
button->show();
app.run(window);
return 0;

最佳答案

为什么不起作用

第一个代码得到 SIGSEGV 因为你正在调用静态 Gtk::Main::quit使用 Gtk::Application 时.

您可能会使用 sigc::mem_fun调用Gio::Application::quitGtk::Application 中继承,但在使用 Gtk::Application::run(Gtk::Window&) 时这不是一个好主意。 :

If you call Gio::Application::quit() while a window is connected to the application, and then return from main() without removing the window from the application, the application's destructor will not be called.

老实说我不知道​​怎么做,因为Glib::RefPtrGtk::Application::create() 返回没有办法获取对象:

Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.

改为关闭窗口

来自 Gtk::Application::add_window(Gtk::Window&) :

If all the windows managed by Gtk::Application are closed (hidden) or removed from the application then the call to run() will return.

#include <gtkmm.h>

int main()
{
auto app = Gtk::Application::create();
Gtk::Button *button = new Gtk::Button("Quit");
Gtk::Window window;
button->signal_clicked().connect(sigc::mem_fun(&window, &Gtk::Window::close));

window.set_default_size(200, 200);
window.add(*button);
window.show_all(); //for some widgets (I don't remember which) show() is not enough
return app->run(window);
}

关于c++ - Gtk::Main 和 Gtk::Application::create 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com