我正在尝试与xfce4-settings-manager接口,这是我在标准的c gtk+-3.0库中成功做到的,但我一直在努力在gtkmm3中复制它。xfce4-settings-manager将一个--socked-id选项传递给客户端,客户机将使用GtkPlug通过id连接到套接字。正如我前面提到的,我成功地用C编写了它,我已经将这些代码放在github 这里中。我更喜欢使用C++作为一种更实用的方式来学习语言,也因为它对C具有更高的功能。
我一直在努力用正确的方法来处理参数,以正确的方式使用Gtk::塞入几个小时的研究和很少的结果。如果有人能够对处理命令行参数和gtkmm3中的gtkmm3的适当方式/文档提供一些见解,这将是非常值得赞赏的,如果您能够提供任何同样值得赞赏的示例的话。提前谢谢你!
发布于 2021-06-18 18:15:56
下面是一个类似于您的示例,在C++中使用Gtkmm 3:
#include <string>
#include <gtkmm.h>
#include <gtkmm/plug.h>
// Simple command line argument parser.
//
// Documented here:
//
// https://gitlab.gnome.org/GNOME/glibmm/-/blob/master/examples/options/main.cc
//
class CmdArgParser : public Glib::OptionGroup
{
public:
CmdArgParser(const std::string& p_name, const std::string& p_description, const std::string& p_help)
: Glib::OptionGroup{p_name, p_description, p_help}
{
// Define the 'socket ID' argument options:
Glib::OptionEntry socketIDArg;
socketIDArg.set_long_name("socket-id");
socketIDArg.set_short_name('s');
socketIDArg.set_flags(Glib::OptionEntry::FLAG_IN_MAIN);
socketIDArg.set_description("Settings manager socket");
// Register it in the parser. It value will be recorded in m_socketID for later usage.
add_entry(socketIDArg, m_socketID);
}
// Override this to handle errors. I skipped it for simplicity.
// void on_error(Glib::OptionContext& context, const Glib::Error& error) override;
::Window GetSocketID() const
{
return m_socketID;
}
private:
int m_socketID = 0;
};
// This is what is going to be plugged into xfce4-settings-manager:
//
// Documented here:
//
// https://developer.gnome.org/gtkmm-tutorial/3.22/sec-plugs-sockets-example.html.en
//
class SettingsPlug : public Gtk::Plug
{
public:
SettingsPlug(::Window p_socketID)
: Gtk::Plug{p_socketID}
{
m_button.set_label("A plug with Gtkmm3!");
add(m_button);
show_all_children();
}
private:
Gtk::Button m_button;
};
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example.plug");
// Parse command line arguments and retreive socket ID:
Glib::init();
setlocale(LC_ALL, "");
Glib::OptionContext context;
CmdArgParser parser{
"Socket ID",
"Command line argument for socket ID communication.",
"No help available, sorry"
};
context.set_main_group(parser);
context.parse(argc, argv);
::Window socketID = parser.GetSocketID();
// Handle plug:
SettingsPlug plug{socketID};
plug.show();
app->run(plug);
return 0;
}为了简化代码,我删除了错误处理和Glade文件的使用。您可以使用以下方法构建它:
g++ main.cpp -o example.out `pkg-config --cflags --libs gtkmm-3.0`https://stackoverflow.com/questions/67955225
复制相似问题