我正在使用 Mark Summerfield 为 PyQt4 编写的 Rapid GUI 编程书,我正在使用 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...]