Can a Qt5 program running on X disable minimize and close?

584 views Asked by At

I am writing a C++ Qt5 application that runs on X. The window manager it will run under is Metacity. I have a few requirements I'm not sure how to address:

  1. Window must not be able to be closed
  2. Window must not be able to be minimized
  3. Window must always be on top

I have sort of implemented requirement 3 using:

setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);

However, right clicking on the window title still shows a popup menu:

enter image description here

From this menu, the user can turn off the "Always on Top" setting, and the Minimize and Close options are still available. I have tried:

setWindowFlags(windowFlags() & ~(Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint));

but, this function doesn't appear to do what I want with this window manager, because no functions get disabled.

One fallback position is:

  • Restart the application if it is ever closed (this is already in place for other reasons)
  • Restore the window to its normal size if it is ever minimized

Another fallback position is:

  • Use Qt::X11BypassWindowManagerHint which completely avoids the window manager, and implement window moving and sizing myself somehow.

Ideally, I would like to continue to use the window manager to offer window move and size functionality, yet turn off functions that I don't need.


(Please try to avoid comments like "well you shouldn't do that" or "that's a dumb idea" - yes, I know, but this application is not for general use, it's only used in a specialized environment, and it is subject to requirements not written by me.)

1

There are 1 answers

1
Appleman1234 On BEST ANSWER

The simple answer to your initial question is Yes.

From your question, I am not sure of all the combinations you have tried and what doesn't work for each. It isn't clear if any of the QtWindowsFlags other than Qt::WindowStaysOnTopHint are working for you.

The following Qt5 widgets windowflags example provides something you can use to test the behavior of the various WindowFlags.

Between using Window Flags, WindowRole, you should be able to set _NET_WM_ALLOWED_ACTIONS and _NET_WM_WINDOW_TYPE using Qt5 via XCB and Metacity should respect it as per their compliance file.

From examining the source code as long as recalc_window_features is called, then both the title bar if any and the right click menu shouldn't have close or minimize.The relevant source code for this located at here and here on the Qt5 side, and here on the Metacity side, in case you need to patch, trace or debug further.

As suggested by Scheff, one way of solving your problem is setting FramelessWindowHint, however I believe based on my inspection of the code that you will have more luck setting window flags in order to trigger the special case located here.

E.g.

Qt::WindowFlags flags = windowFlags();
flags |= Qt::CustomizeWindowHint;
flags |= Qt::WindowTitleHint;
flags |= Qt::WindowStaysOnTopHint;
flags &= ~Qt::WindowMinimizeButtonHint;
flags &= ~Qt::WindowMaximizeButtonHint;
flags &= ~Qt::WindowCloseButtonHint;

The code example above is untested.

I believe what you were missing was the CustomizeWindowHint, without it the flags get set to default values if the window type is QWindow as per here.

Another Stackoverflow question that may be related is Fullscreen for QDialog from within MainWindow only working sometimes , but it is more about having a QDialog behave like a QWindow to bypass a Metacity bug.