Results 1 to 8 of 8

Thread: Enumerating QAction in a QMenuBar

  1. #1
    Join Date
    Dec 2007
    Posts
    14

    Red face Enumerating QAction in a QMenuBar

    I want to enumerate at runtime all QAction in a QMenuBar.
    My first try was to enumerate QAction in a QMenu.

    Qt Code:
    1. void MainWindow::buildAutoCompletion(QMenu * menu)
    2. {
    3. QList<QAction *> actions = menu->actions();
    4. QList<QAction *>::const_iterator it = actions.begin();
    5. for(; it != actions.end(); it++)
    6. {
    7. QAction *action = *it;
    8. ui->plainTextEdit->setPlainText(ui->plainTextEdit->toPlainText() + action->text());
    9. }
    10. }
    11.  
    12. void MainWindow::on_pushButton_clicked()
    13. {
    14. buildAutoCompletion(ui->menuFile);
    15. buildAutoCompletion(ui->menuModifica);
    16. }
    To copy to clipboard, switch view to plain text mode 
    The application crashes when I click on the push button. If I try debugging, application hangs on menu->actions() (as I can see on the stack trace).

    Qt 4.5.3 under Linux 64 bit.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enumerating QAction in a QMenuBar

    it looks like your menuFile object is not initialized...
    Can you see in the debugger if its a valid pointer?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2007
    Posts
    14

    Default Re: Enumerating QAction in a QMenuBar

    Sorry, but something strange happened...now the application works Anyway if I do a step to step debug gdb crashes

    Any idea on how can I iterate over every QMenu in a QMenuBar ?
    Last edited by cionci; 18th February 2010 at 17:36.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enumerating QAction in a QMenuBar

    Your code should work provided that your menu is not empty.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2007
    Posts
    14

    Default Re: Enumerating QAction in a QMenuBar

    My code enumerates QActions in a QMenu, but how can I enumerate QMenus in a QMenuBar ? Then for each QMenu I call my code above.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enumerating QAction in a QMenuBar

    But why you would want to do that?
    You either have existing menus (from an ui file) or, you want to create menus dynamically.
    If you have existing menus, then these menus have actions in them, and you can have these actions as members of your window, and access them at any time.
    If you create new ones at run time, again, when creating them you can store them in a vector or similar which you can return to at any time.
    I am really not clear on why you need to iterate through the menus in the way you are trying....
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Dec 2007
    Posts
    14

    Default Re: Enumerating QAction in a QMenuBar

    I want to include all possible menu actions in a QCompleter, so in line edit I could execute an action only by writing it (using hints from autocompletion) and hitting Enter. I want to add all actions automatically.

  8. #8
    Join Date
    Dec 2007
    Posts
    14

    Default Re: Enumerating QAction in a QMenuBar

    I've done it
    Qt Code:
    1. void MainWindow::lineEditAutoCompletion()
    2. {
    3. QList<QMenu *> menus = ui->menuBar->findChildren<QMenu *>();
    4. QList<QMenu *>::const_iterator it = menus.begin();
    5. for(; it != menus.end(); it++)
    6. {
    7. if((*it)->title().size() > 0)
    8. buildActionMap(*it);
    9. }
    10. QCompleter * completer = new QCompleter(actionMap.keys(), this);
    11. completer->setCaseSensitivity(Qt::CaseInsensitive);
    12. ui->lineEdit->setCompleter(completer);
    13. }
    14.  
    15. void MainWindow::buildActionMap(QMenu * menu)
    16. {
    17. QList<QAction *> actions = menu->actions();
    18. QList<QAction *>::const_iterator it = actions.begin();
    19. for(; it != actions.end(); it++)
    20. {
    21. if((*it)->text().size() > 0 && !(*it)->menu())
    22. actionMap.insert((*it)->text(), *it);
    23. }
    24. }
    25.  
    26. void MainWindow::on_lineEdit_textChanged(QString )
    27. {
    28. QPalette pal;
    29. if(actionMap.contains(ui->lineEdit->text()))
    30. {
    31. pal.setColor(QPalette::Text, Qt::darkGreen);
    32. }
    33. else
    34. {
    35. pal.setColor(QPalette::Text, Qt::red);
    36. }
    37. ui->lineEdit->setPalette(pal);
    38. }
    39.  
    40. void MainWindow::on_lineEdit_returnPressed()
    41. {
    42. if(actionMap.contains(ui->lineEdit->text()))
    43. {
    44. actionMap[ui->lineEdit->text()]->activate(QAction::Trigger);
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by cionci; 20th February 2010 at 09:00.

Similar Threads

  1. QMenuBar Updates
    By johnmauer in forum Qt Programming
    Replies: 5
    Last Post: 20th January 2010, 13:35
  2. QMenuBar and QMenu
    By thedoctor in forum Qt Programming
    Replies: 0
    Last Post: 21st December 2009, 21:01
  3. how will a cumstomized qmenubar look like under mac os x?
    By billconan in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 10:39
  4. Hide Qmenubar
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 22:16
  5. Center QMenuBar
    By pakulo in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 14:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.