gtkmm xml ui 例子(from string)

文章目录

前言

  • glade生成的xml格式不被gtkmm4支持, 需要作修改

来看一个从字符串中生成UI的例子

cpp 复制代码
#include <gtkmm/application.h>
#include <gtkmm.h>
#include <iostream>
using namespace std;

class ExampleWindow : public Gtk::Window
{
public:
    ExampleWindow();
    virtual ~ExampleWindow() {};
    void on_action_file_new()
    {
        cout << "New" << endl;
    };
    void on_action_file_open() {};
    void on_action_file_quit() {};

    Gtk::Box m_Box;
};
ExampleWindow::ExampleWindow()
{
    auto m_refBuilder = Gtk::Builder::create();

    Glib::ustring ui_info =
        "<interface>"
        "  <menu id='menubar'>"
        "    <submenu>"
        "      <attribute name='label' translatable='yes'>_File</attribute>"
        "      <section>"
        "        <item>"
        "          <attribute name='label' translatable='yes'>_New</attribute>"
        "          <attribute name='action'>example.new</attribute>"
        "        </item>"
        "      </section>"
        "      <section>"
        "        <item>"
        "          <attribute name='label' translatable='yes'>_Quit</attribute>"
        "          <attribute name='action'>example.quit</attribute>"
        "        </item>"
        "      </section>"
        "    </submenu>"
        "    <submenu>"
        "      <attribute name='label' translatable='yes'>_Edit</attribute>"
        "      <item>"
        "        <attribute name='label' translatable='yes'>_Copy</attribute>"
        "        <attribute name='action'>example.copy</attribute>"
        "      </item>"
        "      <item>"
        "        <attribute name='label' translatable='yes'>_Paste</attribute>"
        "        <attribute name='action'>example.paste</attribute>"
        "      </item>"
        "    </submenu>"
        "  </menu>"
        "</interface>";
    //绑定行动的对应动作 实际绑定
    auto m_refActionGroup = Gio::SimpleActionGroup::create();
    m_refActionGroup->add_action("new", sigc::mem_fun(*this, &ExampleWindow::on_action_file_new));
    m_refActionGroup->add_action("open", sigc::mem_fun(*this, &ExampleWindow::on_action_file_open));
    m_refActionGroup->add_action("quit", sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit));
    insert_action_group("example", m_refActionGroup);//行动组example 实体诞生

    m_refBuilder->add_from_string(ui_info);
    //m_refBuilder->add_from_file("K:\\CPlusPlus\\cgcc\\Xml_UI.xml");

    auto gmenu = m_refBuilder->get_object<Gio::Menu>("menubar");
    auto pMenuBar = Gtk::make_managed<Gtk::PopoverMenuBar>(gmenu);
    m_Box.append(*pMenuBar);
    set_child(m_Box);

    pMenuBar->set_visible(true);
    m_Box.set_visible(true);
}
int main(int argc, char* argv[])
{
    auto app = Gtk::Application::create("org.gtkmm.example");
    //app快捷键 键位设置对应行动 预先设置
    app->set_accel_for_action("example.new", "<Primary>n");//表示Ctrl+N
    app->set_accel_for_action("example.quit", "<Primary>q");//表示Ctrl+Q
    app->set_accel_for_action("example.copy", "<Primary>c");//表示Ctrl+C
    app->set_accel_for_action("example.paste", "<Primary>v");//表示Ctrl+V
    //Shows the window and returns when it is closed.
    return app->make_window_and_run<ExampleWindow>(argc, argv);
}

可以把那些字符串放入一个xml文件

然后
m_refBuilder->add_from_file("K:\CPlusPlus\cgcc\Xml_UI.xml");
代替上面的add_from_string.

相关推荐
烧仙草奶茶11 小时前
【cocos creator 3.x】3Dui创建,模型遮挡ui效果
ui·3d·cocos creator·cocos3d
wkm95612 小时前
Unity程序嵌入Qt后点击UI按钮Button没有反应
qt·ui·unity
南部余额1 天前
playwright解决重复登录问题,通过pytest夹具自动读取storage_state用户状态信息
前端·爬虫·python·ui·pytest·pylawright
Antonio9151 天前
【Q&A】QT有哪些状态模式的设计?
qt·ui·状态模式
Мартин.2 天前
[Meachines] [Medium] DevOops XXE-XML-RSS+cPickle反序列化+git-leak权限提升
xml·git
珺毅同学2 天前
XML标签格式转换为YOLO TXT格式
xml·人工智能·yolo
一个幽默的程序员3 天前
Postman 如何发送 XML 格式的 API 请求?
xml·测试工具·postman
一只小灿灿3 天前
从 WPF 到 MAUI:跨平台 UI 开发的进化之路
ui·wpf
公子小六3 天前
ASP.NET Core WebApi+React UI开发入门详解
react.js·ui·c#·asp.net·.netcore
Smilecoc3 天前
EXCEL报错:无法共享此工作薄,因表包含excel表或xml映射的解决方法
xml·excel