gpt4 book ai didi

python - PyQt5 对象的 SEGV_MAPERR

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:55 27 4
gpt4 key购买 nike

为什么下面的演示代码会产生SEGV_MAPERR?如何解决它?

提示:一旦删除了带有“#Ref1”注释的行,就不会产生错误。在提取问题的生产代码中这并不容易完成。

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebChannel

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setHtml("")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
page = self.webView.page() # REF1
for i in range(2):
self.init_page()

def init_page(self):
class EditObject(QtCore.QObject):
@QtCore.pyqtSlot(str)
def edit(self, s):
print("test")
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
print(1)
if poChannel is None:
poChannel = QtWebChannel.QWebChannel()
self.webView.page().setWebChannel(poChannel)
print(2)
objects = poChannel.registeredObjects()
print(objects)
poChannel.registerObject("editObject", editObject)
self.webView.setHtml("")

from PyQt5 import QtWebEngineWidgets

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
<小时/>

这类似于 Issues with PyQt5==5.10 on Ubuntu 18 ,但带有示例代码。对于示例代码,https://gist.github.com/gioditalia/03c9fd5d793aeccbe065fea45d842431已适应。

最佳答案

问题在于,poChannel 是一个局部变量,在执行 init_page 后将被删除,因此在第二次迭代中,poChannel 引用将指向不正确的内存地址。因此解决方案是将其循环扩展到 View 的循环,因此我们利用 Qt 属性并将其作为父级传递给 self.webView

poChannel = QtWebChannel.QWebChannel(self.webView)
<小时/>

尽管正如 PyQt 在 the docs 中指出的那样和生成的文件:#警告!在此文件中所做的所有更改都将丢失!,修改 .ui 生成的类并不方便,而是必须创建另一个继承自适当部件的类并使用 Qt Designer 提供的接口(interface)。

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)

class EditObject(QtCore.QObject):
@QtCore.pyqtSlot(str)
def edit(self, s):
print("test")

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
for i in range(2):
self.init_page()

def init_page(self):
editObject = EditObject(self.webView.page())
poChannel = self.webView.page().webChannel()
if poChannel is None:
poChannel = QtWebChannel.QWebChannel(self)
self.webView.page().setWebChannel(poChannel)
objects = poChannel.registeredObjects()
poChannel.registerObject("editObject", editObject)

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

关于python - PyQt5 对象的 SEGV_MAPERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com