창을 가장자리로 끌어 크기를 조정합니다.

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

이것은 화면 중 하나의 가장자리를 터치 할 때 창 크기를 조정하는 것입니다.

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