Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

EDIT: this app will run on Windows, Mac, and various Linux distros. I'm aware Linux has issues with this, but what about Windows? Mac?

Is there any way to get the width of the frame for a normal window, PRIOR to showing any windows? After showing a window, I know I can subtract the size() from the frameSize(), but that doesn't work until after the window is shown.

I've looked at QApplication::style()->pixelMetric(), and I can get the height of the title bar using

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

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

The only solution I've found so far is to:

  • set the window opacity to 0 (so the user doesn't see it),
  • show the window
  • then subtract size() from frameSize()

Is there a better way?

like image 243
David Burson Avatar asked Sep 13 '11 20:09

David Burson


People also ask

What is the width and length of a window?

The first two digits refer to the window's width (ft/in) and the last two digits to the window's height (ft/in). Examples: Window 2 feet, 0 inches wide by 3 feet, 0 inches high is a 2030 window. Window 3 feet, 4 inches wide by 4 feet, 4 inches high is a 3444 window.

Does window size include frame?

Measure Window WidthDo not include any trim in this measurement. To expose the jamb (the vertical side of the window frame) at the bottom of the window, raise the window sash. Place a tape measure horizontally between the inside jamb on the left and the right.

How do you read window measurements?

A window's size notation is basically width and height combined. The first two #'s are for width and the second two #'s are for height. A 2426 replacement window would have a width of 2'4″ and a height of 2'6″.

How much smaller should window be than rough opening?

Recommended rough opening is between 3/4" (19mm) - 1" (25mm) larger than the window width and height. Ensure that the rough opening is plumb, level and square, and the walls in the opening are not twisted.


2 Answers

I posted a suggested solution in a different question at StackOverflow, but I'll post it here as well. You can move the window to somewhere far outside the screen before showing it and then query it for its geometry, and finally move it to where you want it (if that is what you need the geometry for). For instance to center a main window on the primary 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've only tested this code on Windows XP and Qt 4.8.x. Hopefully it works on other platforms as well.

like image 152
Daniel Hedberg Avatar answered Sep 18 '22 02:09

Daniel Hedberg


In case you haven't seen it, the Qt doc page Window and Dialog Widgets contains a lot of information about this.

You don't say what platform you are running on, but it it's X11, the answer seems to be 'No', there isn't a better way:

X11 Peculiarities

On X11, a window does not have a frame until the window manager decorates it. This happens asynchronously at some point in time after calling QWidget::show() and the first paint event the window receives, or it does not happen at all. Bear in mind that X11 is policy-free (others call it flexible). Thus you cannot make any safe assumption about the decoration frame your window will get. Basic rule: There's always one user who uses a window manager that breaks your assumption, and who will complain to you.

(I like your workaround of setting the opacity to 0: neat!)

like image 35
Clare Macrae Avatar answered Sep 21 '22 02:09

Clare Macrae