Python 3.x 将窗口拖到边缘以调整大小
我正在为QGIS开发一个插件,我正在使用QtCreator。我想实现一个功能,允许用户将窗口拖动到屏幕边缘以调整其大小(例如普通窗口)。我的设置是一个多屏幕桌面,我产生了以下代码:Python 3.x 将窗口拖到边缘以调整大小,python-3.x,qt,pyqt5,qgis,Python 3.x,Qt,Pyqt5,Qgis,我正在为QGIS开发一个插件,我正在使用QtCreator。我想实现一个功能,允许用户将窗口拖动到屏幕边缘以调整其大小(例如普通窗口)。我的设置是一个多屏幕桌面,我产生了以下代码: from PyQt5 import QtCore, QtGui tracking = False class Window(QtGui.QWidget): def __init__(self): super(Window, self).__init__() self.timer = QtCore.Q
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
如果屏幕==0且跟踪==True:
如果self.y()==0:
框架GM.moveTopLeft(topLeftPoint)
self.move(frameGm.topLeft())
self.resize(桌面大小.width(),桌面大小.height())
跟踪=错误
elif self.x()=桌面大小.width()+QApplication.desktop().availableGeometry(screen-1).width():
框架GM.moveTopRight(topRightPoint)
self.move(frameGm.topLeft())
self.resize(桌面大小.width()/2,桌面大小.height())
跟踪=错误
以下部分将调整大小以恢复到正常大小:
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_())
如果self.width()==desktop\u size.width()和self.height()==desktop\u size.height()和self.y()=0:
自我调整大小(200200)
跟踪=真
如果self.width()==desktop\u size.width()/2和self.height()==desktop\u size.height()和self.x()>0和self.x()+self.width().QApplication.desktop().availableGeometry(screen-1.width()和self.x()+self.width()非常简单。您所需要做的就是将网格添加到您的小部件中。您可以在添加小部件之前从QtCreator添加它,也可以在代码中添加为grid=QGridLayout();addWidget(小部件的名称、行、列)代码>它非常简单。您所需要做的就是将网格添加到您的小部件中。您可以在添加小部件之前从QtCreator添加它,也可以在代码中添加为grid=QGridLayout();addWidget(小部件的名称、行、列)代码>