0

I am developing a desktop application on macOS. I have a class that is a subclass of QMainWindow. Inside this window there are many dockwidgets. I need to set WindowModality to WindowModal, so user cannot interact with other opened windows. But my window has a menubar with many menus that have some QActions inside and when I setWindowModality(Qt::WindowModal) it automatically disables every action in menu and I need them to be enabled.

Can anybody provide some simple solution for this? Or it is not possible?

Thank you very much.

EDIT:

I have many windows in my application. I have a real main window from which you can open another window and also from that window another window. And here is the situation where i need my child windows to be modal. But they also have their own menubar which gets automatically disabled when window modality is turned on. Ive been googling it already maybe for 10 hours without any solution. I cannot test it but I guess that on windows the menubar would not disable because the native menu is quite different.

simonvaros
  • 86
  • 5

2 Answers2

0

If there is no specific need of using QWindow, then it'll be easier to achive this using QDialog class instead. Using QDialog you can simply show dialog as modal using exec() method.

EDIT: Basically, you can add QMenuBar element to every QLayout class object using QLayout::setMenuBar method. If you want to add menu bar to QDialog, you've got to set layout for your dialog, then programatically create desired QMenuBar object and pass it to QDialog layout (which you can access using QDialog::layout method). Simple example below:

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QMenuBar* menu = new QMenuBar();
    QMenu* fileMenu = menu->addMenu("File"); //Create 'File' menu inside menu bar
    QAction* closeAction = fileMenu->addAction("Close"); //Create 'Close' action in 'File' menu
    connect(closeAction, QAction::triggered, this, close); //Close dialog after triggering 'Close' action
    layout()->setMenuBar(menu); //Add menu bar to QDialog layout
}
Rhathin
  • 1,176
  • 2
  • 14
  • 20
  • I think that is inappropriate. Imagine you are developing MS Word and you are making textEditWindow (where you set font size color and stuff). Would you be happy with using QDialog? :/ – simonvaros Aug 21 '18 at 15:35
  • 2
    If it's your main window, why there is need to make it modal? – Rhathin Aug 21 '18 at 15:58
  • It still doesn't clarify what you're trying to do. Modal windows are used to force user to focus on certain window (e.g. Yes/No question dialog), blocking interaction with application's every other window. In your case, as you described, you're trying to set modality on your main window to block interactions with other openned windows. Question is what is purpose to make it in such unusual way? Also It'd be good, as was mentioned before, to provide [mcve], so we could better understand this situation. – Rhathin Aug 22 '18 at 09:51
  • Im not posting a code because it would not help :D seriously, and ok i update again. Sorry – simonvaros Aug 22 '18 at 10:18
  • Unfortunately, I couldn't reproduce this problem on Windows. Probably it's platform specific as you said before. However, can you tell me where do you call `setWindowModality`? Also, according to my answer, it worth to mention that QDialogs can have menu bars as well. – Rhathin Aug 22 '18 at 11:19
  • I call it in constructor. And how do I put menubar to qdialog? Because I tried but it did not work. – simonvaros Aug 23 '18 at 13:02
  • I've added explaination to my answer. – Rhathin Aug 24 '18 at 07:58
-1

Please consider the usage of Qt::ApplicationModal.

This keeps the modality but gives you other behaviour on MAC.