RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1430751
Accepted
ардуино IDE
ардуино IDE
Asked:2022-07-18 18:41:57 +0000 UTC2022-07-18 18:41:57 +0000 UTC 2022-07-18 18:41:57 +0000 UTC

QSizeGrip 无法正常工作

  • 772

我写了这段代码,但它不能正常工作(在我看来)。
如果您知道如何使其正常工作,请提供帮助。

拉伸就像Qt Dsigner的,但我不明白它是怎么发生的。

它应该像这样工作:

像 QtDsigner 一样伸展

from PyQt5 import QtCore, QtGui, QtWidgets


class SizeGrip(QtWidgets.QSizeGrip):
    def __init__(self, location_angle, parent):
        super().__init__(parent)

        parent.installEventFilter(self)
        self.setFixedSize(30, 30)
        self.location_angle = location_angle

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.Resize:
            if self.location_angle == 'left_top':
                geo = QtCore.QRect(0, 0, 30, 30)
                self.setGeometry(geo)
            elif self.location_angle == 'center_top':
                _geo = self.rect()
                _geo.moveBottomRight(source.rect().bottomRight())
                x = _geo.x() // 2
                geo = QtCore.QRect(x, 0, 30, 30)
                self.setGeometry(geo)
            elif self.location_angle == 'right_top':
                _geo = self.rect()
                _geo.moveBottomRight(source.rect().bottomRight())
                x = _geo.x()
                geo = QtCore.QRect(x, 0, 30, 30)
                self.setGeometry(geo)
            elif self.location_angle == 'left_bottom':
                _geo = self.rect()
                _geo.moveBottomRight(source.rect().bottomRight())
                y = _geo.y()
                geo = QtCore.QRect(0, y, 30, 30)
                self.setGeometry(geo)
            elif self.location_angle == 'right_bottom':
                geo = self.rect()
                geo.moveBottomRight(source.rect().bottomRight())
                self.setGeometry(geo)
        return super().eventFilter(source, event)

    def paintEvent(self, event):
        qp = QtGui.QPainter(self)
        qp.setPen(QtCore.Qt.white)
        qp.setBrush(QtCore.Qt.red)
        qp.drawRect(QtCore.QRect(10, 10, 10, 10))


class Container(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.sizeGrip1 = SizeGrip('left_top', self)
        self.sizeGrip1_1 = SizeGrip('center_top', self)
        self.sizeGrip2 = SizeGrip('right_top', self)
        self.sizeGrip3 = SizeGrip('left_bottom', self)
        self.sizeGrip4 = SizeGrip('right_bottom', self)

        self.startPos = None
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(20, 20, 20, 20)
        self.setStyleSheet('''
            Container {
                background: lightblue;
                border: 0px;
                border-radius: 4px;
            }
        ''')

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

    def mouseMoveEvent(self, event):
        if self.startPos:
            self.move(self.pos() + (event.pos() - self.startPos))

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


class GraphicsRoundedFrame(QtWidgets.QGraphicsProxyWidget):
    def __init__(self):
        super().__init__()

        self.container = Container()
        self.setWidget(self.container)

    def addWidget(self, widget):
        self.container.layout().addWidget(widget)

    def paint(self, qp, opt, widget):
        qp.save()
        p = QtGui.QPainterPath()
        p.addRoundedRect(self.boundingRect().adjusted(0, 0, -.5, -.5), 4, 4)
        qp.setClipPath(p)
        super().paint(qp, opt, widget)
        qp.restore()


class View(QtWidgets.QGraphicsView):
    def __init__(self):
        super().__init__()

        scene = QtWidgets.QGraphicsScene()
        self.setScene(scene)
        self.setRenderHints(QtGui.QPainter.Antialiasing)
        scene.setSceneRect(0, 0, 1024, 768)

        texture = QtGui.QImage(30, 30, QtGui.QImage.Format_ARGB32)
        qp = QtGui.QPainter(texture)
        qp.setBrush(QtCore.Qt.white)
        qp.setPen(QtGui.QPen(QtGui.QColor(189, 190, 191), 2))
        qp.drawRect(texture.rect())
        qp.end()
        scene.setBackgroundBrush(QtGui.QBrush(texture))

        testFrame = GraphicsRoundedFrame()
        scene.addItem(testFrame)
        testFrame.container.layout().addStretch(1)
        testFrame.addWidget(QtWidgets.QPushButton('I am a button'))


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = View()
    w.show()
    sys.exit(app.exec_())
python ооп
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2022-07-19T00:25:07Z2022-07-19T00:25:07Z

    试试这样:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.Qt import *
    
    
    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 Container(QtWidgets.QWidget): 
        _gripSize = 8
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.setMinimumSize(80, 80) 
            self.sideGrips = [
                SideGrip(self, QtCore.Qt.LeftEdge), 
                SideGrip(self, QtCore.Qt.TopEdge), 
                SideGrip(self, QtCore.Qt.RightEdge), 
                SideGrip(self, QtCore.Qt.BottomEdge), 
            ]
            
            self.cornerGrips = [QtWidgets.QSizeGrip(self) for i in range(4)]
      
            self.resize(300, 200)     
            self.startPos = None
            layout = QtWidgets.QVBoxLayout(self)
            layout.setContentsMargins(20, 20, 20, 20)
            self.setStyleSheet("""
                Container {
                    background: lightblue;
                    border: 0px;
                    border-radius: 4px;
                }
            """)
            
        def mousePressEvent(self, event):
            if event.button() == QtCore.Qt.LeftButton:
                self.startPos = event.pos()
    
        def mouseMoveEvent(self, event):
            if self.startPos:
                self.move(self.pos() + (event.pos() - self.startPos))
    
        def mouseReleaseEvent(self, event):
            self.startPos = None        
    
        @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()
            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.QWidget.resizeEvent(self, event)
            self.updateGrips()        
    
    
    class GraphicsRoundedFrame(QtWidgets.QGraphicsProxyWidget):
        def __init__(self):
            super().__init__()
            self.container = Container()
            self.setWidget(self.container)
    
        def addWidget(self, widget):
            self.container.layout().addWidget(widget)
    
        def paint(self, qp, opt, widget):
            qp.save()
            p = QtGui.QPainterPath()
            p.addRoundedRect(self.boundingRect().adjusted(0, 0, -.5, -.5), 4, 4)
            qp.setClipPath(p)
            super().paint(qp, opt, widget)
            qp.restore()
    
    
    class View(QtWidgets.QGraphicsView):
        def __init__(self):
            super().__init__()
    
            scene = QtWidgets.QGraphicsScene()
            self.setScene(scene)
            self.setRenderHints(QtGui.QPainter.Antialiasing)
            scene.setSceneRect(0, 0, 1024, 768)
    
            texture = QtGui.QImage(30, 30, QtGui.QImage.Format_ARGB32)
            qp = QtGui.QPainter(texture)
            qp.setBrush(QtCore.Qt.white)
            qp.setPen(QtGui.QPen(QtGui.QColor(189, 190, 191), 2))
            qp.drawRect(texture.rect())
            qp.end()
            scene.setBackgroundBrush(QtGui.QBrush(texture))
    
            testFrame = GraphicsRoundedFrame()
            scene.addItem(testFrame)
            testFrame.container.layout().addStretch(1)
            testFrame.addWidget(QtWidgets.QPushButton('I am a button'))
    
    
    if __name__ == '__main__':
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = View()
        w.show()
        sys.exit(app.exec_())
    

    在此处输入图像描述

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5