【问题标题】:PyQt5 setLayout function causing program to crash?PyQt5 setLayout 函数导致程序崩溃?
【发布时间】:2013-07-04 12:37:54
【问题描述】:

我正在使用 Mark Summerfield 的 Rapid GUI 编程书,它是为 PyQt4 编写的,我正在使用 PyQt5。有些事情肯定是不同的。

谁能看到为什么在我运行 Ubuntu 13.04 的 Linux 机器上失败了?它在 Mint 15 上运行,但有时会以分段错误结束。我认为这与 PyQt4 和 PyQt5 之间的区别有关,我一直在 qt-project.org 网站上研究 C++ 实现。到目前为止,我可以看出 QVBoxLayout 确实继承自 QDialog,并且它确实具有 setLayout 函数。但是,注释掉 _init_ 函数中的最后一行将允许程序运行而不会崩溃,而且不会向 QDialog 框添加任何小部件。

import sys
import PyQt5.QtCore
import PyQt5.QtGui
import PyQt5.QtWidgets

class Form(PyQt5.QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = PyQt5.QtWidgets.QTextBrowser()
        self.lineEdit = PyQt5.QtWidgets.QLineEdit("default statement here")
        self.lineEdit.selectAll()
        layout = PyQt5.QtWidgets.QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineEdit)
        self.setLayout(layout)    # <--- program seems to crash here



app = PyQt5.QtWidgets.QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

我收到这样的初始错误消息,重复了大约 10 次:

(python3:9896): Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion `GTK_IS_WIDGET (widget)' failed

然后是下面的代码块,一直重复,直到我终止程序:

QXcbShmImage: shmget() failed (22) for size -524284 (65535x65535)
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::translate: Painter not active
QPainter::save: Painter not active
QPainter::setClipRect: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::restore: Unbalanced save/restore
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setClipRect: Painter not active
[etc, etc, etc...]

【问题讨论】:

标签: pyqt qt5 python-3.3 qdialog pyqt5


【解决方案1】:

问题在于 QTextBrowser 的大小。

看到这个错误:

https://bugreports.qt-project.org/browse/QTBUG-32527

【讨论】:

  • 谢谢,我使用的是上面链接中给出的 QStyleFactory.create()。我认为手动设置视图区域是一个更好的解决方案,至少在错误修复出现时。当我重新使用 MainWindow 小部件时,我会尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2012-01-07
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多