Python Pyqt 如何获取小部件的尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15204470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:30:33  来源:igfitidea点击:

Pyqt how to get a widget's dimensions

pythonwidgetsizepyqt

提问by ibi0tux

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.

我目前正在开发一个无法使用模态窗口的应用程序(由于某些应用程序限制)。但是,在某些情况下,我想模拟一个弹出窗口。为此,我动态创建了一个以 centralwidget 作为父级的小部件,并使用 move() 方法将其放置在我想要的位置。我想知道是否有办法在给定时间获取小部件的尺寸(考虑到 mainWindow 可以随时调整大小),以便我能够将占位符弹出窗口(一个简单的小部件)居中中央小部件。

Thank you

谢谢

采纳答案by Avaris

You can use frameGeometryor geometrydepending on your needs.

您可以使用frameGeometrygeometry取决于您的需要

回答by subin george

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()

回答by subin george

For getting Qt Widget size:

获取 Qt Widget 大小:

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

mainWindow = QtGui.QWidget()
width = mainWindow.frameGeometry().width()
height = mainWindow.frameGeometry().height()