gtkmm xml ui 例子(from string)

前言

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

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

  • #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文件
    image
    然后
    m_refBuilder->add_from_file(“K:\CPlusPlus\cgcc\Xml_UI.xml”);
    代替上面的add_from_string.

相关推荐

  1. Spark例子

    2024-02-04 03:00:02       51 阅读
  2. C++<span style='color:red;'>例子</span>

    C++例子

    2024-02-04 03:00:02      37 阅读
  3. Pytest 的小例子

    2024-02-04 03:00:02       58 阅读
  4. C均值算法例子

    2024-02-04 03:00:02       47 阅读
  5. store 简单引入例子

    2024-02-04 03:00:02       44 阅读
  6. Duilib初级入门例子

    2024-02-04 03:00:02       46 阅读
  7. iocp简单例子

    2024-02-04 03:00:02       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 03:00:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 03:00:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 03:00:02       82 阅读
  4. Python语言-面向对象

    2024-02-04 03:00:02       91 阅读

热门阅读

  1. 码农也得“开口说话”

    2024-02-04 03:00:02       57 阅读
  2. 建立数据库连接时出错

    2024-02-04 03:00:02       55 阅读
  3. 在 WSL Ubuntu 中安装 SSH 服务器,ssh连接wsl

    2024-02-04 03:00:02       44 阅读
  4. 假期作业2

    2024-02-04 03:00:02       48 阅读
  5. ArrayList的数据结构

    2024-02-04 03:00:02       51 阅读
  6. Mysql-备份与恢复

    2024-02-04 03:00:02       61 阅读
  7. C语言stderr、errno、strerror、perror

    2024-02-04 03:00:02       49 阅读
  8. Linux命令-arpd命令(收集免费ARP信息)

    2024-02-04 03:00:02       53 阅读