gtkmm xml ui 例子(from string)

文章目录

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

前言

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

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

  • #include 
    #include 
    #include 
    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 =
            ""
            "  "
            "    "
            "      _File"
            "      
    " " " " _New" " example.new" " " "
    "
    "
    " " " " _Quit" " example.quit" " " "
    "
    " " " " " _Edit" " " " _Copy" " example.copy" " " " " " _Paste" " example.paste" " " " " "
    "
    ""; //绑定行动的对应动作 实际绑定 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", "n");//表示Ctrl+N app->set_accel_for_action("example.quit", "q");//表示Ctrl+Q app->set_accel_for_action("example.copy", "c");//表示Ctrl+C app->set_accel_for_action("example.paste", "v");//表示Ctrl+V //Shows the window and returns when it is closed. return app->make_window_and_run<ExampleWindow>(argc, argv); }
    可以把那些字符串放入一个xml文件
    gtkmm xml ui 例子(from string)_第1张图片
    然后
    m_refBuilder->add_from_file(“K:\CPlusPlus\cgcc\Xml_UI.xml”);
    代替上面的add_from_string.

你可能感兴趣的:(GTK,xml,ui,gtkmm4)