如何解决将窗口拖动到边缘以调整大小
我正在开发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_())
我的问题是:窗口确实会调整大小,但是在左侧进行操作时,屏幕和窗口之间会有间隙。另外,在屏幕之间的交界处使用此代码似乎不起作用。我还想调整窗口的大小,例如窗口的底部比任务栏的上方。 有人可以帮我优化它吗?
解决方法
这很简单。您需要做的就是向小部件添加网格。您可以在添加小部件之前从QtCreator添加它,也可以在代码中将其添加为grid = QGridLayout(); grid.addWidget(name_of_the_widget,row,col);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。