Drag window to the edge to resize

隐身守侯 提交于 2021-01-05 11:29:56

问题


I'm working on a plugin for QGIS and i'm using QtCreator. I wanted to implement a function that allows the user to drag the window to the edge of the screen to resize it (such as ordinary windows). My setup being a multiple screen desktop, I came up with this code:

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

This one is to resize the window when touching an edge of one of the screen:

    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

The following part is to resize to come back to a normal size:

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

My problem being: the window does resize however there is a gap between the screen and my window when doing it with the left side. Plus, this code doesn't seem to work when used at the junction between screens. I would also like to resize the window such as the bottom of the window is upper than the taskbar. Could someone help me to optimize it?


回答1:


It is very simple. All you need to do is to add a grid to your widget. You can either add it from QtCreator before adding widgets or in your code as grid = QGridLayout(); grid.addWidget(name_of_the_widget, row, col);



来源:https://stackoverflow.com/questions/63518997/drag-window-to-the-edge-to-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!