gpt4 book ai didi

python - 如何在 PyQt5 中发出 dataChanged

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:34 26 4
gpt4 key购买 nike

下面的代码在 self.emit 行中断。它在 PyQt4 中运行良好。如何修复此代码以使其在 PyQt5 中工作?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal

class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)

def createEditor(self, parent, option, index):
return QtWidgets.QLineEdit()

@QtCore.pyqtSlot()
def setModelData(self, editor, model, index):
self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)

稍后编辑:

可行的解决方案:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal

class Communicate(QObject):
data_changed = pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex)

class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
self.c = Communicate()

@QtCore.pyqtSlot()
def setModelData(self, editor, model, index):
self.c.data_changed.emit(index, index)

最佳答案

As you can read here , QtCore.SIGNALPyQt4 之后停产,因此不兼容。

This page解释了 PyQt5 的新型信号和槽。语法是:

PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])

您的案例可以翻译成:

from PyQt5 import pyqtsignal

data_changed = pyqtsignal(QModelindex,QModelIndex)

并发出你的信号:

self.data_changed.emit(index, index)

编辑:根据以下评论改编的解决方案。

关于python - 如何在 PyQt5 中发出 dataChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177144/

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