Results 1 to 2 of 2

Thread: Creating a Windows Menu in QT4 Menubar

  1. #1
    Join Date
    Jul 2012
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    5

    Default Creating a Windows Menu in QT4 Menubar

    Hi -

    I have a QT4 application that uses the MenuBar with the typical menus (File, Edit, Settings, etc). I want to add a "Windows" menu to it - with the function of being able to show the open MdiSubWindows in my MdiArea.

    I tried

    Qt Code:
    1. // Clear previous entries
    2. windowsMenu->clear ();
    3.  
    4. // Add default options
    5. windowsMenu->addAction(tr("Cascade"),mdiArea,SLOT(cascadeSubWindows()));
    6. windowsMenu->addAction(tr("Tile"),mdiArea,SLOT(tileSubWindows()));
    7. windowsMenu->addSeparator();
    8.  
    9. // Get list of open subwindows in Mdi Area
    10. QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();
    11.  
    12. // Make sure it isn't empty
    13. if(subWindows.isEmpty()){
    14. return;
    15. }
    16.  
    17. // Create windows list based off of what's open
    18. for(int i = 0; i < subWindows.size(); i++){
    19. QMdiSubWindow *child = subWindows.at(i);
    20. windowsMenu->addAction(child->widget()->windowTitle(),this,SLOT(windowsMenuActivated(int)));
    21. }
    To copy to clipboard, switch view to plain text mode 

    That compiles and generates a list of open MdiSubWindows as I want. The trouble I am having is using that created menu to then select/switch to one of those windows - and I think the problem is in the following line:

    Qt Code:
    1. windowsMenu->addAction(child->widget()->windowTitle(),this,SLOT(windowsMenuActivated(int)));
    To copy to clipboard, switch view to plain text mode 

    The windowsMenuActivated(int) slot is defined as:

    Qt Code:
    1. void MainWindow::windowsMenuActivated(int id) {
    2.  
    3. // Get list of open subwindows in Mdi Area
    4. QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();
    5.  
    6. // Make sure it isn't empty
    7. if(subWindows.isEmpty()){
    8. return;
    9. }
    10.  
    11. // Set active selected window
    12. mdiArea->setActiveSubWindow(subWindows.at(id));
    13. }
    To copy to clipboard, switch view to plain text mode 

    This all compiles fine, but when I run the program, I get:

    Qt Code:
    1. QObject::connect: Incompatible sender/receiver arguments
    2. QAction::triggered(bool) --> MainWindow::windowsMenuActivated(int)
    To copy to clipboard, switch view to plain text mode 

    All I want to do is set the selected window (from the windowsMenu) to be the active mdnsubwindow...

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Creating a Windows Menu in QT4 Menubar

    A QAction can carry a piece of user data (QAction::setData()) that you can use to identify a subwindow but you need to be careful using indexes into the MDI area's subwindow list because the list order is not fixed.

    I used this approach: When you create a QMdiSubWindow also create a matching checkable QAction parented to the subwindow. That action's triggered() signal should be connected to the subwindow's show() and setFocus(). Insert the action into your Window menu and also into a QActionGroup to ensure only one is checked. When the MDI area's current subwindow changes look up the matching action and call setChecked(). If a subwindow is deleted then it automatically removes itself from the menu because the action is destroyed along with it.

Similar Threads

  1. QT MenuBar operation different in Windows vs OSX
    By KenJustKen in forum Qt Programming
    Replies: 0
    Last Post: 30th April 2012, 18:08
  2. Frame (MenuBar) corrupt inside Windows app
    By Grinchman in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 04:35
  3. Custom menu align in menubar
    By wirasto in forum Qt Programming
    Replies: 0
    Last Post: 20th December 2009, 14:31
  4. Move the menuBar() to the system menu
    By jlbrd in forum Qt Programming
    Replies: 3
    Last Post: 6th March 2009, 10:38
  5. Moving menu to the right menubar corner
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 05:24

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.