如何在 PyQt5 中发出 dataChanged [英] How to emit dataChanged in PyQt5

查看:71
本文介绍了如何在 PyQt5 中发出 dataChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The code below breaks on self.emit line. It works fine in PyQt4. How to fix this code so it works in 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)  

稍后

一个可行的解决方案:

Edited later:

A working solution:

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)

推荐答案

如你所见在这里QtCore.SIGNALPyQt4之后停止使用,因此不兼容.

As you can read here, QtCore.SIGNAL was discontinued after PyQt4 and is therefore not compatible.

本页解释了 的新型信号和槽PyQt5.语法是:

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

您的案例可以翻译为:

from PyQt5 import pyqtsignal

data_changed = pyqtsignal(QModelindex,QModelIndex)

并发出您的信号:

self.data_changed.emit(index, index)

根据以下评论改编的解决方案.

Solution adapted from comments below.

这篇关于如何在 PyQt5 中发出 dataChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆