Pyqt how to get a widget's dimensions

2019-02-16 16:20发布

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

3条回答
叛逆
2楼-- · 2019-02-16 16:42

You can use frameGeometry or geometry depending on your needs.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-16 17:03

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()
查看更多
forever°为你锁心
4楼-- · 2019-02-16 17:04

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()
查看更多
登录 后发表回答