15

I am currently developing an application in which i cannot use modal windows (due to some application constraints). However, in some cases i would like to simulate a popup window. To do so i dynamically create a widget that has the centralwidget as parent and i use the move() method to place it where i want. I would like to know if there is a way to get a widget's dimensions at a given time (considering the mainWindow can be resized at any time) so that i will be able to center the placeholder popup (a simple widget) at the middle of the centralwidget.

Thank you

ibi0tux
  • 2,481
  • 4
  • 28
  • 49
  • 2
    For those looking for actual QWidget size (inner size): If you want to know the size of the widget usable space (the inner space), you need to use `geometry`, not `frameGeometry` which adds the size of the window frame to the widget own size. – mins Dec 21 '18 at 15:10

3 Answers3

40

For getting Qt Widget size:

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

mainWindow = QtGui.QWidget()
width = mainWindow.frameGeometry().width()
height = mainWindow.frameGeometry().height()
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
subin george
  • 571
  • 5
  • 6
17

For gettting screen size

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

mainWindow = QtGui.QWidget()
screenShape = QtGui.QDesktopWidget().screenGeometry()
mainWindow.resize(self.screenShape.width(), self.screenShape.height())
mainWindow.show()
subin george
  • 571
  • 5
  • 6
2

You can use frameGeometry or geometry depending on your needs.

Avaris
  • 35,883
  • 7
  • 81
  • 72
  • 9
    This is not a correct answer if you don't give a working example. Copy-pasting docs only is not an answer. Therefore @subin george gave the correct answers. – GLHF Jun 19 '16 at 00:26
  • 2
    @GLHF although you are correct with your statement... OP doesn't give code. Its all theoretical; therefore I Avaris his theorectical answer is correct as well ;-)) – ZF007 Nov 25 '17 at 13:11