C++ (Qt)#include <QApplication>#include <QMainWindow>#include <QMenuBar>#include <QStatusBar>#include <QMenu>#include <QUiLoader>#include <QFile>#include <QMetaObject>#include <QDebug> class Actions : public QObject{ Q_OBJECTpublic: Actions( QMainWindow* mw ) : QObject( mw ) , sb_( mw->statusBar() ) { foreach( QMenu* m, mw->menuBar()->findChildren< QMenu* >() ) QObject::connect( m, SIGNAL( triggered(QAction*) ), this, SLOT( invokeAction(QAction*) ) ); } private: QStatusBar* sb_; private Q_SLOTS: void invokeAction( QAction* a ) { QMenu* menu = qobject_cast< QMenu* >( sender() ); if( !menu->actions().contains( a ) ) // ignore submenu actions return; QString func_name = sender()->objectName(); if( func_name.startsWith( "menu" ) ) func_name.remove( 0, 4 ); func_name.replace( 0, 1, func_name.at( 0 ).toLower() ); QString action_name = a->objectName(); if( action_name.startsWith( "action" ) ) action_name.remove( 0, 6 ); action_name.replace( 0, 1, action_name.at( 0 ).toUpper() ); func_name += action_name; if( a->isCheckable() ) QMetaObject::invokeMethod( this, func_name.toLatin1().constData(), Qt::QueuedConnection, Q_ARG( bool, a->isChecked() ) ); else QMetaObject::invokeMethod( this, func_name.toLatin1().constData(), Qt::QueuedConnection ); } public Q_SLOTS: void fileNew() { sb_->showMessage( "fileNew() invoked..." ); } void fileClose() { sb_->showMessage( "fileClose() invoked..." ); } void fileQuit() { qApp->exit( 0 ); } void openFile() { sb_->showMessage( "openFile() invoked..." ); } void openUrl() { sb_->showMessage( "openUrl() invoked..." ); } void viewNormal( bool set ) { sb_->showMessage( QString( "viewNormal(%1) invoked..." ).arg( set ) ); } void viewFullScreen( bool set ) { sb_->showMessage( QString( "viewFullScreen(%1) invoked..." ).arg( set ) ); }}; int main( int argc, char* argv[] ){ QApplication a( argc, argv ); QFile f( "mw.ui" ); QUiLoader loader; QMainWindow* mw = qobject_cast< QMainWindow* >( loader.load( &f ) ); if( !mw ) { qDebug() << "Can't load " << f.fileName(); return 1; } Actions actions( mw ); mw->show(); return a.exec();}
XML<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"/> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>26</height> </rect> </property> <widget class="QMenu" name="menuFile"> <property name="title"> <string>File</string> </property> <widget class="QMenu" name="menuOpen"> <property name="title"> <string>Open</string> </property> <addaction name="actionFile"/> <addaction name="actionUrl"/> </widget> <addaction name="actionNew"/> <addaction name="menuOpen"/> <addaction name="actionSave"/> <addaction name="actionSave_As"/> <addaction name="separator"/> <addaction name="actionClose"/> <addaction name="actionQuit"/> </widget> <widget class="QMenu" name="menuEdit"> <property name="title"> <string>Edit</string> </property> <addaction name="actionCut"/> <addaction name="actionCopy"/> <addaction name="actionPaste"/> </widget> <widget class="QMenu" name="menuView"> <property name="title"> <string>View</string> </property> <addaction name="actionNormal"/> <addaction name="actionFullScreen"/> </widget> <addaction name="menuFile"/> <addaction name="menuEdit"/> <addaction name="menuView"/> </widget> <widget class="QStatusBar" name="statusbar"/> <action name="actionClose"> <property name="text"> <string>Close</string> </property> </action> <action name="actionFile"> <property name="text"> <string>File</string> </property> </action> <action name="actionUrl"> <property name="text"> <string>Url</string> </property> </action> <action name="actionQuit"> <property name="text"> <string>Quit</string> </property> </action> <action name="actionNew"> <property name="text"> <string>New</string> </property> </action> <action name="actionSave"> <property name="text"> <string>Save</string> </property> </action> <action name="actionSave_As"> <property name="text"> <string>Save As...</string> </property> </action> <action name="actionCut"> <property name="text"> <string>Cut</string> </property> </action> <action name="actionCopy"> <property name="text"> <string>Copy</string> </property> </action> <action name="actionPaste"> <property name="text"> <string>Paste</string> </property> </action> <action name="actionNormal"> <property name="checkable"> <bool>true</bool> </property> <property name="text"> <string>Normal</string> </property> </action> <action name="actionFullScreen"> <property name="checkable"> <bool>true</bool> </property> <property name="text"> <string>Full Screen</string> </property> </action> </widget> <resources/> <connections/></ui>
QMenu * menu = action.menu();if (menu != myMenuBar) // че то тут.