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.

相关推荐
syj_1113 小时前
初识ArkUI
ui·arkts·arkui
芋芋qwq11 小时前
Unity UI射线检测 道具拖拽
ui·unity·游戏引擎
鸿蒙自习室12 小时前
鸿蒙多线程开发——线程间数据通信对象02
ui·harmonyos·鸿蒙
大霞上仙15 小时前
element ui table 每行不同状态
vue.js·ui·elementui
栈老师不回家1 天前
Element UI 组件库详解【Vue】
前端·vue.js·ui
郭梧悠1 天前
HarmonyOS(57) UI性能优化
ui·性能优化·harmonyos
wyh要好好学习1 天前
WPF数据加载时添加进度条
ui·wpf
code_shenbing1 天前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计
vvw&2 天前
如何使用 Docker Compose 安装 WireGuard UI
linux·服务器·ui·docker·容器·wireguard·异地组网
老码沉思录2 天前
Android开发实战班 - 现代 UI 开发之 Material Design及自定义主题
android·ui