我正在使用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...]
问题在于QTextBrowser的大小。
看到这个错误:
https://bugreports.qt-project.org/browse/QTBUG-32527