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) ;
m_refBuilder-> add_from_string ( ui_info) ;
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-> set_accel_for_action ( "example.new" , "<Primary>n" ) ;
app-> set_accel_for_action ( "example.quit" , "<Primary>q" ) ;
app-> set_accel_for_action ( "example.copy" , "<Primary>c" ) ;
app-> set_accel_for_action ( "example.paste" , "<Primary>v" ) ;
return app-> make_window_and_run < ExampleWindow> ( argc, argv) ;
}
可以把那些字符串放入一个xml文件 然后 m_refBuilder->add_from_file(“K:\CPlusPlus\cgcc\Xml_UI.xml”);
代替上面的add_from_string.
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1426937.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!