tags:

views:

44

answers:

2

Greetings all,

In my QT Application I have several QMainWindow instances. I keep track of opended QWindow objects in a Application Context object;

At onepoint ,when a Menu item is clicked , I want to go through all this QWindows and check which Window is active and execute some operations.

Please refer to following code snippet :

//Just a confirmation dialog
QMessageBox msgBox(this->getMainWindow());
msgBox.setText("This will discard existing project");
msgBox.setInformativeText("All Contour data and Volume data will be discarded");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
if(msgBox.exec()==QMessageBox::Cancel)
{
 return
}


//This is where I check the active window.

QList<MainWindow*> lst= applicationContext.getOpendedWindows();

for(i=0;i<lst.size();i++)
{
  MainWindow *win=lst.value(i);

   //Check if this is the active window
   if(win.isActive()){  // tried (win==QApplication::activeWindow()) also
      //DO SOMETHING with 'win' object
   }
  }
}

This check works fine when I dont show the confirmation dialog before the check. That is, when I comment out the line 'msgBox.exe()' I can find an active window from the list.

Does the displaying of QMessageBox change the focused window?

Any tips on this?

Thanks in advance,

umanga

+1  A: 

The simplest solution to your problem I can think of is running the check for the active window before you show the message box.

And yes, the QMessageBox becomes the active window once you call msgBox.exec().

Greg S
+1  A: 

If you have multiple main windows, I think you also have multiple menus? If so, I would associate the slot reacting to the menu action with the mainwindow, either make it a slot of the mainwindow or a slot of an object that knows the corresponding main window.

You can also identify the active window before the messagebox, like Greg suggests, but I wouldn't consider that optimal either.

Frank