Arrastra la ventana hasta el borde para cambiar el tamaño

Aug 21 2020

Estoy trabajando en un complemento para QGIS y estoy usando QtCreator. Quería implementar una función que le permita al usuario arrastrar la ventana al borde de la pantalla para cambiar su tamaño (como ventanas normales). Como mi configuración es un escritorio de múltiples pantallas, se me ocurrió este código:

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

Éste es para cambiar el tamaño de la ventana al tocar un borde de una de las pantallas:

    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

La siguiente parte es cambiar el tamaño para volver a un tamaño normal:

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

Mi problema es: la ventana cambia de tamaño, sin embargo, hay un espacio entre la pantalla y mi ventana cuando lo hago con el lado izquierdo. Además, este código no parece funcionar cuando se usa en la unión entre pantallas. También me gustaría cambiar el tamaño de la ventana, ya que la parte inferior de la ventana es superior a la barra de tareas. ¿Alguien podría ayudarme a optimizarlo?

Respuestas

sheharbano Aug 31 2020 at 07:55

Es muy simple. Todo lo que necesita hacer es agregar una cuadrícula a su widget. Puede agregarlo desde QtCreator antes de agregar widgets o en su código comogrid = QGridLayout(); grid.addWidget(name_of_the_widget, row, col);