How to get the width of a window frame before creating any windows?

EDIT: This application will run on Windows, Mac, and various Linux distributions. I know Linux has problems with this, but what about Windows? Mac?

Is there a way to get the frame width for a regular window, PRIOR to display any windows? After showing the window, I know that I can subtract size() from frameSize() , but this does not work until the window appears.

I looked QApplication::style()->pixelMetric() and I can get the title bar height using

 QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight) 

but I don't see any options to get the width of the rest of the frame around the window.

The only solution I have found so far is this:

  • set the opacity of the window to 0 (so that the user does not see it),
  • show window
  • then subtract size() from frameSize()

Is there a better way?

+6
source share
2 answers

I posted the proposed solution in another question on StackOverflow, but I will also post it here. You can move the window somewhere far off the screen before showing it, and then request it for your geometry and finally move it to where you want (if that is what you need geometry for). For example, to center the main window on the main screen without flickering, I do the following:

 MainWindow mainWindow; QRect primaryScreenGeometry(QApplication::desktop()->screenGeometry()); mainWindow.move(-50000,-50000); mainWindow.show(); mainWindow.move((primaryScreenGeometry.width() - mainWindow.width()) / 2.0, (primaryScreenGeometry.height() - mainWindow.height()) / 2.0); 

I tested this code only on Windows XP and Qt 4.8.x. Hope it works on other platforms as well.

+5
source

If you have not seen this, the Qt doc Window and dialog widgets page contains a lot of information about this.

You do not say which platform you are running on, but it is X11, the answer seems to be “No”, there is no better way:

X11 Features

In X11, a window has no border until the window manager decorates it. This happens asynchronously at some point in time after calling QWidget :: show () and the first drawing event that the window receives, or this does not happen at all. Keep in mind that X11 has no policy (others call it flexible). Thus, you cannot make any safe assumption about the decorative frame that your window will receive. Basic rule: there is always one user who uses a window manager who violates your assumption and who will complain to you.

(I like your way to bypass opacity 0: neat!)

+3
source

Source: https://habr.com/ru/post/897262/


All Articles