ウィンドウを端にドラッグしてサイズを変更します

Aug 21 2020

私はQGISのプラグインに取り組んでおり、QtCreatorを使用しています。ユーザーがウィンドウを画面の端にドラッグしてサイズを変更できる機能(通常のウィンドウなど)を実装したかったのです。私のセットアップはマルチスクリーンデスクトップなので、次のコードを思いつきました。

from PyQt5 import QtCore, QtGui
tracking = False
class Window(QtGui.QWidget):

def __init__(self):
    super(Window, self).__init__()
    self.timer = QtCore.QTimer(self)
    self.timer.setInterval(50)
    self.timer.timeout.connect(self.Resize)
    self.timer.start()
    self.cursor = None
    tracking = True

def Resize(self):
    global tracking

    frameGm = self.frameGeometry()
    screen = QApplication.desktop().screenNumber(QApplication.desktop().cursor().pos())
    desktop_size = QApplication.desktop().availableGeometry(screen)
    topLeftPoint = QApplication.desktop().availableGeometry(screen).topLeft()
    topRightPoint = QApplication.desktop().availableGeometry(screen).topRight()

これは、画面の1つの端に触れたときにウィンドウのサイズを変更するためのものです。

    if screen == 0 and tracking == True:
        if self.y()== 0:
            frameGm.moveTopLeft(topLeftPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width(),desktop_size.height())
            tracking = False             
        elif self.x() <= 0 :
            frameGm.moveTopLeft(topLeftPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width()/2,desktop_size.height())
            tracking = False
        elif self.x()<= desktop_size.width() and self.x()+self.width() >= desktop_size.width() :  
            frameGm.moveTopRight(topRightPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width()/2,desktop_size.height())
            tracking = False 

    if screen == 1 and tracking == True : 
        if self.y() == 0:
            frameGm.moveTopLeft(topLeftPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width(),desktop_size.height())
            tracking = False  
        elif self.x() <= QApplication.desktop().availableGeometry(screen-1).width() and self.x()+self.width() >= QApplication.desktop().availableGeometry(screen-1).width() :
            frameGm.moveTopLeft(topLeftPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width()/2,desktop_size.height())
            tracking = False
        elif self.x()+self.width() >= desktop_size.width()+QApplication.desktop().availableGeometry(screen-1).width() :
            frameGm.moveTopRight(topRightPoint)
            self.move(frameGm.topLeft())
            self.resize(desktop_size.width()/2,desktop_size.height())
            tracking = False

次の部分は、通常のサイズに戻るようにサイズを変更することです。

    if self.width() == desktop_size.width() and self.height() == desktop_size.height() and self.y()!=0:
        self.resize(200,200)
        tracking = True 
    if self.width() == desktop_size.width()/2 and self.height() == desktop_size.height() and self.x()>0 and self.x()+self.width()<desktop_size.width():
        self.resize(200,200)
        tracking = True
    if self.width() == desktop_size.width()/2 and self.height() == desktop_size.height() and self.x()> QApplication.desktop().availableGeometry(screen-1).width() and self.x()+self.width()<desktop_size.width()+QApplication.desktop().availableGeometry(screen-1).width():            
        self.resize(200,200)
        tracking = True

    elif tracking == False and screen == QApplication.desktop().screenNumber(self.pos()):
        if self.width() != desktop_size.width() and self.height() != desktop_size.height() and self.width() != desktop_size.width()/2:
            tracking=True

if __name__ == '__main__':

import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 500, 200, 200)
window.show()
sys.exit(app.exec_())

私の問題は、ウィンドウのサイズが変更されることですが、左側でそれを行うと、画面とウィンドウの間にギャップがあります。さらに、このコードは、画面間の接合部で使用すると機能しないようです。また、ウィンドウの下部がタスクバーよりも上になるように、ウィンドウのサイズを変更したいと思います。誰かが私がそれを最適化するのを手伝ってもらえますか?

回答

sheharbano Aug 31 2020 at 07:55

とても簡単です。あなたがする必要があるのはあなたのウィジェットにグリッドを追加することです。ウィジェットを追加する前にQtCreatorから追加するか、コードに次のように追加できます。grid = QGridLayout(); grid.addWidget(name_of_the_widget, row, col);