如何确定PyQt5对话框是否会在屏幕外显示

在加载带有保存的位置坐标的PyQt5对话框小部件时,有时会在屏幕外加载表单,例如,当用户将对话框的位置保存在具有3个监视器的计算机上,然后在只有一个监视器的另一台设备上再次打开对话框

QDesktopWidget().availableGeometry()对象为我提供了一个屏幕的尺寸-例如(0,0,1920,1040)-即使我有三个屏幕。

form.geometry()返回相对于主屏幕及其尺寸的当前位置。在我的示例中,主屏幕是中心屏幕,表单位于(2395、184、210、200)。如果保存这些值,则从笔记本电脑加载表单时,该位置将不在屏幕上。

如何确定当前设备是否可以以保存的值显示小部件?

编辑-其他备注:

我已经研究了height()width()属性以及screenCount()primaryScreen()属性,这些属性将提供更多信息,但是我尚未发现可以告诉我x / y点是否实际上会显示在活动屏幕上。我可能需要使用Windows api来获取rect值吗?

tiancai61 回答:如何确定PyQt5对话框是否会在屏幕外显示

通过@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,v_dim)
else:
    # Otherwise,set it to default values that ARE available
    form.setGeometry(ag.x(),ag.y(),v_dim)

QDesktopWidget对象与QApplication.desktop()对象相同,并且是从PyQt5.QtWidgets派生的。 QRect是从PyQt5.QtCore导入的

版本: 首先需要将表单的几何图形设置为保存的值,然后然后查看其是否在可用几何图形内。如果将表单保存在除主屏幕之外的任何位置,则每次如上所述的首先检查都会失败。

    # Set the form to the saved position
    form.setGeometry(x_pos,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,v_dim)):
        # Otherwise,set it to default values that ARE available
        form.setGeometry(ag.x(),v_dim)

本文链接:https://www.f2er.com/3155698.html

大家都在问