将窗口拖动到边缘以调整大小 [英] Drag window to the edge to resize

查看:52
本文介绍了将窗口拖动到边缘以调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 QGIS 开发一个插件,我正在使用 QtCreator.我想实现一个功能,允许用户将窗口拖动到屏幕边缘来调整它的大小(例如普通窗口).我的设置是一个多屏幕桌面,我想出了这个代码:

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?

推荐答案

很简单.您需要做的就是为您的小部件添加一个网格.您可以在添加小部件之前从 QtCreator 添加它,也可以在代码中将其添加为 grid = QGridLayout();grid.addWidget(name_of_the_widget, row, col);

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

这篇关于将窗口拖动到边缘以调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆