Gtk::Main 和 Gtk::Application::create 有什么区别?
What is the difference between Gtk::Main and Gtk::Application::create?
两者都创建 Gtk windows 但我无法真正理解幕后发生的事情。我尝试将信号绑定到按钮以退出 window,但程序在使用 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;
由于没有关于具体错误的信息,很难知道问题到底是什么。我看到的一件事是您没有应用程序 ID。请参阅 this example. You can also check this 以了解有关应用程序 ID 的更多信息。
基本上,我会尝试类似的方法:
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
// ^^^^^^^^^^^^^^^^^^^ <- add something like this (see below)
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);
}
请注意,您可以(并且应该)根据自己的需要更改应用程序 ID。仔细阅读第二个 link 以查看约定并仔细选择您的 ID。
希望对您有所帮助!
为什么不起作用
第一个代码获得 SIGSEGV,因为您正在调用静态 Gtk::Main::quit when using Gtk::Application。
你或许可以使用 sigc::mem_fun to call Gio::Application::quit inherited in Gtk::Application
but that's not a good idea when using 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::RefPtr returned by Gtk::Application::create() 没有办法获取对象:
Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.
改为关闭 window
来自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);
}
两者都创建 Gtk windows 但我无法真正理解幕后发生的事情。我尝试将信号绑定到按钮以退出 window,但程序在使用 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;
由于没有关于具体错误的信息,很难知道问题到底是什么。我看到的一件事是您没有应用程序 ID。请参阅 this example. You can also check this 以了解有关应用程序 ID 的更多信息。
基本上,我会尝试类似的方法:
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
// ^^^^^^^^^^^^^^^^^^^ <- add something like this (see below)
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);
}
请注意,您可以(并且应该)根据自己的需要更改应用程序 ID。仔细阅读第二个 link 以查看约定并仔细选择您的 ID。
希望对您有所帮助!
为什么不起作用
第一个代码获得 SIGSEGV,因为您正在调用静态 Gtk::Main::quit when using Gtk::Application。
你或许可以使用 sigc::mem_fun to call Gio::Application::quit inherited in Gtk::Application
but that's not a good idea when using 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::RefPtr returned by Gtk::Application::create() 没有办法获取对象:
Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.
改为关闭 window
来自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);
}