Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "widget.h"
- #include <QtCore>
- #include <QtGui>
- #include <QtWidgets>
- #include <iostream>
- static int recursionDepth = 0;
- static void traverseMenu(QMenu *menu)
- {
- foreach (QAction *action, menu->actions())
- {
- if (action->isSeparator())
- {
- for(int i=0; i<=recursionDepth; ++i)
- {
- std::cout << "\t";
- }
- std::cout << ">>" << "separator" << std::endl;
- }
- else if (action->menu())
- {
- for(int i=0; i<=recursionDepth; ++i)
- {
- std::cout << "\t";
- }
- std::cout << ">>" << qPrintable(action->text()) << "(menu)" << std::endl;
- ++recursionDepth;
- traverseMenu(action->menu());
- --recursionDepth;
- }
- else
- {
- for(int i=0; i<=recursionDepth; ++i)
- {
- std::cout << "\t";
- }
- std::cout << ">>" << qPrintable(action->text()) << std::endl;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- QMenu *menu = new QMenu("Main Menu");
- menu->addAction("Level0First");
- menu->addAction("Level0Second");
- menu->addSeparator();
- QAction* third = menu->addAction("Level0Third");
- menu->addSeparator();
- QMenu *subMenu = new QMenu("Sub Menu");
- subMenu->addAction("Level1First");
- subMenu->addAction("Level1Second");
- subMenu->addSeparator();
- QAction* third1 = subMenu->addAction("Level1Third");
- subMenu->addSeparator();
- QMenu *subMenu1 = new QMenu("Sub-sub Menu");
- subMenu1->addAction("Level2First");
- subMenu1->addAction("Level2Second");
- subMenu1->addAction("Level2Third");
- subMenu1->addAction("Level2Fourth");
- subMenu1->addAction("Level2Fifth");
- third1->setMenu(subMenu1);
- subMenu->addAction("Level1Fourth");
- subMenu->addAction("Level1Fifth");
- third->setMenu(subMenu);
- menu->addAction("Level0Fourth");
- menu->addAction("Level0Fifth");
- traverseMenu(menu);
- w.show();
- return a.exec();
- }
Add Comment
Please, Sign In to add comment