开发者

How to find active QMainWindow instance?

开发者 https://www.devze.com 2023-01-11 10:12 出处:网络
Greetings all, In my QT Application I have several QMainWindow instances. I keep track of opended QWindow objects in a Application Context object;

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 f开发者_如何学运维ocused window?

Any tips on this?

Thanks in advance,

umanga


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.


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消