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.

相关推荐
CHICX12291 小时前
【Hadoop】改一下core-site.xml和hdfs-site.xml配置就可以访问Web UI
xml·大数据·hadoop
胡耀超15 小时前
知识图谱入门——8: KG开发常见数据格式:OWL、RDF、XML、GraphML、JSON、CSV。
xml·json·知识图谱·csv·owl·graphml·gml
编程、小哥哥1 天前
手写mybatis之Mapper XML的解析和注册使用
xml·java·mybatis
神一样的老师1 天前
如何解决 Photoshop 中的“暂存盘已满”错误
ui·photoshop
伊织code2 天前
GraphRAG-Local-UI - 基于 GraphRAG 支持本地的聊天UI
ui·llm·rag·graphrag·local-ui
Avalon7123 天前
UniVue大版本更新:UniVue2.0.0-preview
游戏·ui·unity·c#
极客先躯3 天前
Hadoop-HDFS-core-site.xml,hdfs-site.xml,yarn-site.xml,krb5.conf 都是做什么的?
xml·运维·hadoop·hdfs
iQM753 天前
TinyVue:一款轻量级且功能强大的Vue UI组件库
前端·javascript·vue.js·ui·jenkins·excel
MarcoPage4 天前
第 30 章 XML
xml