添加属性 QtCore.Qt.FramelessWindowHint 后如何从边缘调整窗口大小 [英] How to resize a window from the edges after adding the property QtCore.Qt.FramelessWindowHint

查看:112
本文介绍了添加属性 QtCore.Qt.FramelessWindowHint 后如何从边缘调整窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚安.我见过一些具有新无边界设计的程序,但您仍然可以使用调整大小.目前我知道要删除我们使用的 pyqt 程序的边框:QtCore.Qt.FramelessWindowHint

而要更改窗口的大小,请使用 QSizeGrip.但是我们如何调整无边框窗口的大小?

这是我用来删除窗口边框的代码,但之后我还没有找到有关如何在 pyqt5 中执行此操作的信息.希望你能帮我举例说明如何解决这个问题

 from PyQt5.QtWidgets import QMainWindow,QApplication从 PyQt5 导入 QtCore类 Main(QMainWindow):def __init__(self):QMainWindow.__init__(self)self.setWindowFlags(QtCore.Qt.FramelessWindowHint)app = QApplication([])m = 主()m.show()m.resize(800,600)app.exec_()

解决方案

如果你使用 QMainWindow,你可以添加一个 QStatusBar(它会自动添加一个 QSizeGrip),只需调用

然后您可以为 QSizeGrip 小部件(浅蓝色)创建所有几何图形:

对于自定义侧边小部件:

from PyQt5 import QtCore, QtGui, QtWidgets类 SideGrip(QtWidgets.QWidget):def __init__(self, parent, edge):QtWidgets.QWidget.__init__(self, parent)如果边缘== QtCore.Qt.LeftEdge:self.setCursor(QtCore.Qt.SizeHorCursor)self.resizeFunc = self.resizeLeftelif 边缘 == QtCore.Qt.TopEdge:self.setCursor(QtCore.Qt.SizeVerCursor)self.resizeFunc = self.resizeTopelif 边缘 == QtCore.Qt.RightEdge:self.setCursor(QtCore.Qt.SizeHorCursor)self.resizeFunc = self.resizeRight别的:self.setCursor(QtCore.Qt.SizeVerCursor)self.resizeFunc = self.resizeBottomself.mousePos = 无def resizeLeft(self, delta):窗口 = self.window()width = max(window.minimumWidth(), window.width() - delta.x())geo = window.geometry()geo.setLeft(geo.right() - 宽度)window.setGeometry(geo)def resizeTop(self, delta):窗口 = self.window()height = max(window.minimumHeight(), window.height() - delta.y())geo = window.geometry()geo.setTop(geo.bottom() - 高度)window.setGeometry(geo)def resizeRight(self, delta):窗口 = self.window()width = max(window.minimumWidth(), window.width() + delta.x())window.resize(width, window.height())def resizeBottom(self, delta):窗口 = self.window()height = max(window.minimumHeight(), window.height() + delta.y())window.resize(window.width(), height)def mousePressEvent(self, event):如果 event.button() == QtCore.Qt.LeftButton:self.mousePos = event.pos()def mouseMoveEvent(self, event):如果 self.mousePos 不是 None:delta = event.pos() - self.mousePosself.resizeFunc(delta)def mouseReleaseEvent(self, event):self.mousePos = 无类 Main(QtWidgets.QMainWindow):_gripSize = 8def __init__(self):QtWidgets.QMainWindow.__init__(self)self.setWindowFlags(QtCore.Qt.FramelessWindowHint)self.sideGrips = [SideGrip(自我,QtCore.Qt.LeftEdge),SideGrip(自我,QtCore.Qt.TopEdge),SideGrip(自我,QtCore.Qt.RightEdge),SideGrip(自我,QtCore.Qt.BottomEdge),]# 角把手应该在上面";一切,否则侧面抓地力# 将优先于鼠标事件,因此我们将它们添加到 *after*;# 或者,可以使用widget.raise_(​​)self.cornerGrips = [QtWidgets.QSizeGrip(self) for i in range(4)]@财产定义抓地大小(自我):返回 self._gripSizedef setGripSize(self, size):如果大小 == self._gripSize:返回self._gripSize = max(2, size)self.updateGrips()def updateGrips(self):self.setContentsMargins(*[self.gripSize] * 4)outRect = self.rect()# 一个内在"rect 用于参考以设置尺寸夹点的几何形状inRect = outRect.adjusted(self.gripSize, self.gripSize,-self.gripSize, -self.gripSize)# 左上方self.cornerGrips[0].setGeometry(QtCore.QRect(outRect.topLeft(), inRect.topLeft()))# 右上self.cornerGrips[1].setGeometry(QtCore.QRect(outRect.topRight(), inRect.topRight()).normalized())#右下角self.cornerGrips[2].setGeometry(QtCore.QRect(inRect.bottomRight(), outRect.bottomRight()))# 左下方self.cornerGrips[3].setGeometry(QtCore.QRect(outRect.bottomLeft(), inRect.bottomLeft()).normalized())# 左边缘self.sideGrips[0].setGeometry(0, inRect.top(), self.gripSize, inRect.height())# 上边缘self.sideGrips[1].setGeometry(inRect.left(), 0, inRect.width(), self.gripSize)# 右边缘self.sideGrips[2].setGeometry(inRect.left() + inRect.width(),inRect.top(), self.gripSize, inRect.height())# 底边self.sideGrips[3].setGeometry(self.gripSize, inRect.top() + inRect.height(),inRect.width(), self.gripSize)def resizeEvent(self, event):QtWidgets.QMainWindow.resizeEvent(self, event)self.updateGrips()app = QtWidgets.QApplication([])m = 主()m.show()m.resize(240, 160)app.exec_()

Good night. I have seen some programs with new borderless designs and still you can make use of resizing. At the moment I know that to remove the borders of a pyqt program we use: QtCore.Qt.FramelessWindowHint

And that to change the size of a window use QSizeGrip. But how can we resize a window without borders?

This is the code that I use to remove the border of a window but after that I have not found information on how to do it in pyqt5. I hope you can help me with an example of how to solve this problem

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import  QtCore

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)


        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)


app = QApplication([])
m = Main()
m.show()
m.resize(800,600)
app.exec_()

解决方案

If you use a QMainWindow you can add a QStatusBar (which automatically adds a QSizeGrip) just by calling statusBar():

This function creates and returns an empty status bar if the status bar does not exist.

Otherwise, you can manually add grips, and their interaction is done automatically based on their position. In the following example I'm adding 4 grips, one for each corner, and then I move them each time the window is resized.

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.gripSize = 16
        self.grips = []
        for i in range(4):
            grip = QSizeGrip(self)
            grip.resize(self.gripSize, self.gripSize)
            self.grips.append(grip)

    def resizeEvent(self, event):
        QMainWindow.resizeEvent(self, event)
        rect = self.rect()
        # top left grip doesn't need to be moved...
        # top right
        self.grips[1].move(rect.right() - self.gripSize, 0)
        # bottom right
        self.grips[2].move(
            rect.right() - self.gripSize, rect.bottom() - self.gripSize)
        # bottom left
        self.grips[3].move(0, rect.bottom() - self.gripSize)

UPDATE

Based on comments, also side-resizing is required. To do so a good solution is to create a custom widget that behaves similarly to QSizeGrip, but for vertical/horizontal resizing only.

For better implementation I changed the code above, used a gripSize to construct an "inner" rectangle and, based on it, change the geometry of all widgets, for both corners and sides.

Here you can see the "outer" rectangle and the "inner" rectangle used for geometry computations:

Then you can create all geometries, for QSizeGrip widgets (in light blue):

And for custom side widgets:

from PyQt5 import QtCore, QtGui, QtWidgets

class SideGrip(QtWidgets.QWidget):
    def __init__(self, parent, edge):
        QtWidgets.QWidget.__init__(self, parent)
        if edge == QtCore.Qt.LeftEdge:
            self.setCursor(QtCore.Qt.SizeHorCursor)
            self.resizeFunc = self.resizeLeft
        elif edge == QtCore.Qt.TopEdge:
            self.setCursor(QtCore.Qt.SizeVerCursor)
            self.resizeFunc = self.resizeTop
        elif edge == QtCore.Qt.RightEdge:
            self.setCursor(QtCore.Qt.SizeHorCursor)
            self.resizeFunc = self.resizeRight
        else:
            self.setCursor(QtCore.Qt.SizeVerCursor)
            self.resizeFunc = self.resizeBottom
        self.mousePos = None

    def resizeLeft(self, delta):
        window = self.window()
        width = max(window.minimumWidth(), window.width() - delta.x())
        geo = window.geometry()
        geo.setLeft(geo.right() - width)
        window.setGeometry(geo)

    def resizeTop(self, delta):
        window = self.window()
        height = max(window.minimumHeight(), window.height() - delta.y())
        geo = window.geometry()
        geo.setTop(geo.bottom() - height)
        window.setGeometry(geo)

    def resizeRight(self, delta):
        window = self.window()
        width = max(window.minimumWidth(), window.width() + delta.x())
        window.resize(width, window.height())

    def resizeBottom(self, delta):
        window = self.window()
        height = max(window.minimumHeight(), window.height() + delta.y())
        window.resize(window.width(), height)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.mousePos = event.pos()

    def mouseMoveEvent(self, event):
        if self.mousePos is not None:
            delta = event.pos() - self.mousePos
            self.resizeFunc(delta)

    def mouseReleaseEvent(self, event):
        self.mousePos = None


class Main(QtWidgets.QMainWindow):
    _gripSize = 8
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.sideGrips = [
            SideGrip(self, QtCore.Qt.LeftEdge), 
            SideGrip(self, QtCore.Qt.TopEdge), 
            SideGrip(self, QtCore.Qt.RightEdge), 
            SideGrip(self, QtCore.Qt.BottomEdge), 
        ]
        # corner grips should be "on top" of everything, otherwise the side grips
        # will take precedence on mouse events, so we are adding them *after*;
        # alternatively, widget.raise_() can be used
        self.cornerGrips = [QtWidgets.QSizeGrip(self) for i in range(4)]

    @property
    def gripSize(self):
        return self._gripSize

    def setGripSize(self, size):
        if size == self._gripSize:
            return
        self._gripSize = max(2, size)
        self.updateGrips()

    def updateGrips(self):
        self.setContentsMargins(*[self.gripSize] * 4)

        outRect = self.rect()
        # an "inner" rect used for reference to set the geometries of size grips
        inRect = outRect.adjusted(self.gripSize, self.gripSize,
            -self.gripSize, -self.gripSize)

        # top left
        self.cornerGrips[0].setGeometry(
            QtCore.QRect(outRect.topLeft(), inRect.topLeft()))
        # top right
        self.cornerGrips[1].setGeometry(
            QtCore.QRect(outRect.topRight(), inRect.topRight()).normalized())
        # bottom right
        self.cornerGrips[2].setGeometry(
            QtCore.QRect(inRect.bottomRight(), outRect.bottomRight()))
        # bottom left
        self.cornerGrips[3].setGeometry(
            QtCore.QRect(outRect.bottomLeft(), inRect.bottomLeft()).normalized())

        # left edge
        self.sideGrips[0].setGeometry(
            0, inRect.top(), self.gripSize, inRect.height())
        # top edge
        self.sideGrips[1].setGeometry(
            inRect.left(), 0, inRect.width(), self.gripSize)
        # right edge
        self.sideGrips[2].setGeometry(
            inRect.left() + inRect.width(), 
            inRect.top(), self.gripSize, inRect.height())
        # bottom edge
        self.sideGrips[3].setGeometry(
            self.gripSize, inRect.top() + inRect.height(), 
            inRect.width(), self.gripSize)

    def resizeEvent(self, event):
        QtWidgets.QMainWindow.resizeEvent(self, event)
        self.updateGrips()


app = QtWidgets.QApplication([])
m = Main()
m.show()
m.resize(240, 160)
app.exec_()

这篇关于添加属性 QtCore.Qt.FramelessWindowHint 后如何从边缘调整窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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