【问题标题】:PySide6: The errors "Fatal Python error: Cannot recover from stack overflow" occurrs when I use my custom class' rewritten function sizeHint()PySide6:当我使用自定义类的重写函数 sizeHint() 时出现错误“致命 Python 错误:无法从堆栈溢出中恢复”
【发布时间】:2021-12-06 12:06:42
【问题描述】:

我模仿QT的official documentation写一个计算器例子。

我尝试将源代码重写为 python 并进行一些更改。我的代码很糟糕:

import sys

from PySide6.QtWidgets import (QApplication, QSizePolicy, QWidget, QToolButton, QGridLayout)
from PySide6 import QtCore


class Advanced_Calculator(QWidget):
    def __init__(self):
        super(Advanced_Calculator, self).__init__()

        self.mainwindow = QGridLayout(self)

        # Digit buttons and operator buttons

        self.widget_button = QWidget()

        self.button_digit = []

        for i in range(0, 10):
            self.button_digit.append(Button(str(i)))

        self.button_factor = Button("!")
        self.button_lbracket = Button("(")
        self.button_rbracket = Button(")")
        self.button_backspace = Button("<-")
        self.button_division = Button("/")
        self.button_log = Button("log")
        self.button_multiply = Button("X")
        self.button_sqrt = Button("√")
        self.button_minus = Button("-")
        self.button_power = Button("^")
        self.button_plus = Button("+")
        self.button_abs = Button("|x|")
        self.button_const = Button("Const")
        self.button_dot = Button(".")
        self.button_equal = Button("=")

        # Buttons layout with 0 spacing

        self.layout_button = QGridLayout()
        self.layout_button.setSpacing(0)

        self.layout_button.addWidget(self.button_factor, 0, 0, 1, 1)
        self.layout_button.addWidget(self.button_lbracket, 0, 1, 1, 1)
        self.layout_button.addWidget(self.button_rbracket, 0, 2, 1, 1)
        self.layout_button.addWidget(self.button_backspace, 0, 3, 1, 1)
        self.layout_button.addWidget(self.button_division, 0, 4, 1, 1)
        self.layout_button.addWidget(self.button_log, 1, 0, 1, 1)

        for i in range(1, 10):
            self.layout_button.addWidget(self.button_digit[i], 3 - ((i - 1) // 3), (i - 1) % 3 + 1, 1, 1)

        self.layout_button.addWidget(self.button_multiply, 1, 4, 1, 1)
        self.layout_button.addWidget(self.button_sqrt, 2, 0, 1, 1)
        self.layout_button.addWidget(self.button_minus, 2, 4, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_plus, 3, 4, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_power, 3, 0, 1, 1)
        self.layout_button.addWidget(self.button_abs, 4, 0, 1, 1)
        self.layout_button.addWidget(self.button_const, 4, 1, 1, 1)
        self.layout_button.addWidget(self.button_digit[0], 4, 2, 1, 1)
        self.layout_button.addWidget(self.button_dot, 4, 3, 1, 1)
        self.layout_button.addWidget(self.button_equal, 4, 4, 1, 1)

        self.widget_button.setLayout(self.layout_button)

        # button layout set to mainwindow

        self.mainwindow.addWidget(self.widget_button)


class Button(QToolButton):
    def __init__(self, text, parent=None):
        super(Button, self).__init__(parent)

        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.setText(text)

    def sizeHint(self) -> QtCore.QSize:
        size = self.sizeHint()
        size.setHeight(size.height + 20)
        size.setWidth(max(size.width(), size.height()))
        # size.rheight() += 20
        # size.rwidth() = max(size.width(), size.height())

        return size


if __name__ == "__main__":
    app = QApplication([])

    Calculator = Advanced_Calculator()
    Calculator.show()

    sys.exit(app.exec())

当我调试这些代码时,出现“致命的 Python 错误:无法从堆栈溢出中恢复”。现在我知道该错误是由 sizeHint() 函数引起的。问题出在哪里?

顺便说一句,这个函数是我用我对 sizeHint() 函数的理解重写的。官方文档使用了我将它们制作成 cmets 的两行代码,它也不起作用。嗯,我也想不通问题出在哪里。

【问题讨论】:

  • 请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pyside pyside6


【解决方案1】:

考虑代码...

def sizeHint(self) -> QtCore.QSize:
    size = self.sizeHint()
    size.setHeight(size.height + 20)
    size.setWidth(max(size.width(), size.height()))
    # size.rheight() += 20
    # size.rwidth() = max(size.width(), size.height())

    return size

线...

size = self.sizeHint()

导致Button::sizeHint 调用自身导致无限递归,从而导致堆栈溢出。也许您打算改为调用基类实现...

size = super(Button, self).sizeHint()

【讨论】:

  • 顺便问一下,QSize 类如何使用 rheight() 或 rwidth() 函数。我像 cmets 代码那样做,但我失败了。
猜你喜欢
  • 2018-11-03
  • 2017-04-19
  • 1970-01-01
  • 2016-11-10
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多