PDA

View Full Version : Creating a Windows Menu in QT4 Menubar



yapatel
9th April 2014, 01:46
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


// Clear previous entries
windowsMenu->clear ();

// Add default options
windowsMenu->addAction(tr("Cascade"),mdiArea,SLOT(cascadeSubWindows()));
windowsMenu->addAction(tr("Tile"),mdiArea,SLOT(tileSubWindows()));
windowsMenu->addSeparator();

// Get list of open subwindows in Mdi Area
QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();

// Make sure it isn't empty
if(subWindows.isEmpty()){
return;
}

// Create windows list based off of what's open
for(int i = 0; i < subWindows.size(); i++){
QMdiSubWindow *child = subWindows.at(i);
windowsMenu->addAction(child->widget()->windowTitle(),this,SLOT(windowsMenuActivated(int)) );
}

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:


windowsMenu->addAction(child->widget()->windowTitle(),this,SLOT(windowsMenuActivated(int)) );

The windowsMenuActivated(int) slot is defined as:


void MainWindow::windowsMenuActivated(int id) {

// Get list of open subwindows in Mdi Area
QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();

// Make sure it isn't empty
if(subWindows.isEmpty()){
return;
}

// Set active selected window
mdiArea->setActiveSubWindow(subWindows.at(id));
}


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


QObject::connect: Incompatible sender/receiver arguments
QAction::triggered(bool) --> MainWindow::windowsMenuActivated(int)


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

ChrisW67
9th April 2014, 02:41
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.