当加载带有保存位置坐标的PyQt5对话框时,窗体有时会从屏幕外加载,例如当用户将对话框位置保存在具有3台监视器的计算机上,然后在只有一个监视器的不同设备上再次打开该对话框。
QDesktopWidget().availableGeometry()
对象给出了一个屏幕的尺寸--例如(0,0,1920,1040) --尽管我有三个屏幕。
form.geometry()
返回当前相对于主屏幕的位置及其尺寸。在我的示例中,主屏幕是中央屏幕,表单位于(2395,184,210,200)。如果我保存这些值,当我从我的笔记本电脑加载表单时,这个位置将被关闭。
如何确定当前设备是否可以以保存的值显示小部件?
编辑-附加备注:
我查看了height()
和width()
属性以及screenCount()
和primaryScreen()
属性,这些属性将提供更多信息,但我尚未发现一个属性,它将告诉我x/y点是否将实际显示在活动屏幕上。我可能需要使用Windows来获取rect值吗?
发布于 2019-11-06 08:01:19
感谢@ekhumoro在正确方向上的提示,我发现了以下作品(不正确!)见下文修订本):
# Get the screen real estate available to the form
ag = QDesktopWidget().availableGeometry(form)
# If the saved values fall within that real estate, we can
# safely assign the values to the form's geometry
if ag.contains(QRect(x_pos, y_pos, h_dim, v_dim)):
form.setGeometry(x_pos, y_pos, h_dim, v_dim)
else:
# Otherwise, set it to default values that ARE available
form.setGeometry(ag.x(), ag.y(), h_dim, v_dim)
QDesktopWidget对象与QApplication.desktop()对象相同,并从PyQt5.QtWidget派生。QRect是从PyQt5.QtCore导入的
修订:首先需要将表单的几何设置为保存的值,然后查看它是否属于可用的几何图形。如果窗体被保存到除主屏幕以外的任何地方,每次都会失败。
# Set the form to the saved position
form.setGeometry(x_pos, y_pos, h_dim, v_dim)
# Get the screen real estate available to the form
ag = QDesktopWidget().availableGeometry(form)
# If the saved values have placed the form within
# that real estate, we can leave it alone.
if not ag.contains(QRect(x_pos, y_pos, h_dim, v_dim)):
# Otherwise, set it to default values that ARE available
form.setGeometry(ag.x(), ag.y(), h_dim, v_dim)
https://stackoverflow.com/questions/58721258
复制相似问题