How to emit dataChanged in PyQt5

2024/9/29 9:21:58

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, pyqtSignalclass 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, pyqtSignalclass 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)
Answer

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

This page explains the new-style signals and slots for PyQt5. The syntax is:

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

Your case could be translated to:

from PyQt5 import pyqtsignaldata_changed = pyqtsignal(QModelindex,QModelIndex)

and to emit your signal:

self.data_changed.emit(index, index)

Edit: Solution adapted from comments below.

https://en.xdnf.cn/q/71232.html

Related Q&A

django: gettext and coercing to unicode

I have following code in my django application.class Status(object):def __init__(self, id, desc):self.id = idself.desc = descdef __unicode__(self):return self.descSTATUS = Status(0, _(u"Some text&…

Telegram bot api keyboard

I have problem with Telegram Bot Api and with "ReplyKeyboard". Im using Python 2.7 and I send post request:TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, …

Use of torch.stack()

t1 = torch.tensor([1,2,3]) t2 = torch.tensor([4,5,6]) t3 = torch.tensor([7,8,9])torch.stack((t1,t2,t3),dim=1)When implementing the torch.stack(), I cant understand how stacking is done for different di…

Is it possible to sort a list with reduce?

I was given this as an exercise. I could of course sort a list by using sorted() or other ways from Python Standard Library, but I cant in this case. I think Im only supposed to use reduce().from funct…

Flask-WTF set time limit on CSRF token

Im currently using Flask-WTF v0.13.1, i have a few forms on my website, all created including the CSRF token.For some reasons i have to set a different expiration on each form, so far i could set manua…

Extracting Intermediate layer outputs of a CNN in PyTorch

I am using a Resnet18 model. ResNet((conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats…

In Tensorflow, how to unravel the flattened indices obtained by tf.nn.max_pool_with_argmax?

I meet a problem: After I use the tf.nn.max_pool_with_argmax, I obtain the indices i.e. argmax: A Tensor of type Targmax. 4-D. The flattened indices of the max values chosen for each output.How to unr…

How write csv file without new line character in last line?

I have a code like this to write csv file in python import csv with open(eggs.csv, wb) as csvfile:spamwriter = csv.writer(csvfile, delimiter= ,quotechar=|, quoting=csv.QUOTE_MINIMAL)spamwriter.writerow…

Getting tests to parallelize using nose in python

I have a directory with lots of .py files (say test_1.py, test_2.py and so on) Each one of them is written properly to be used with nose. So when I run nosetests script, it finds all the tests in all t…

Python IDLE is not starting on Windows 7

I used to use Python 2.7 and then IDLE was working. I uninstalled it and installed Python 3.1. Right now Idle cannot launch. What should i do to get it running?NOTE: I tried c:\Python31\pythonw.exe c:…