英文:
How to solve slowness of the dataChanged signal when adding a row to my QTreeView?
问题
你好,我想了解一下他在禁用了什么“ trolltech modelest ”以便快速发出datachanged.emit(QModelIndex(), QModelIndex())
信号。我的QTreeView
中没有很多项,所以每次发出这个信号都不应该花费0.1秒。
编辑
正如我在评论部分所说,当我删除这行代码时:self.model.dataChanged.connect(self.onDataChanged)
程序的运行速度如预期般快。这个方法并不大,我只用它来更新我的数据库中的数据(模型是基于这个数据库构建的)。
def onDataChanged(self) :
item_id = self.currentIndex().siblingAtColumn(1).data()
if self.currentIndex().isValid() :
new_name = self.currentIndex().siblingAtColumn(0).data()
con : Connexion() = Connexion()
con.update("UPDATE cdc SET nom = :1 WHERE ID = :2",[new_name,item_id])
self.update()
对象con
来自一个连接到我的数据库的类。我测试了许多查询,它们都很快(不到0.01秒),所以我相当确定问题不是从那里出现的。所以我想知道,实现自己的dataChanged
函数是否会影响Qt的功能?
谢谢你的答案。
英文:
https://stackoverflow.com/a/22379130/21860235[the post 8 years ago][1]
Hello, I would like to understand what is the trolltech modelest
that he disabled in order to have a fast datachanged.emit(QModelIndex(), QModelIndex())
signal. I don't have a lot of items in my QTreeView
, so it shouldn't take 0,1 seconds each time this signal
is emitted.
Edit
As I said in the comment section, when I remove this line : self.model.dataChanged.connect(self.onDataChanged)
the program works as fast as excpected. This method is not big at all, I only use it to update data in my database (the model is built on this database).
def onDataChanged(self) :
item_id = self.currentIndex().siblingAtColumn(1).data()
if self.currentIndex().isValid() :
new_name = self.currentIndex().siblingAtColumn(0).data()
con : Connexion() = Connexion()
con.update("UPDATE cdc SET nom = :1 WHERE ID = :2",[new_name,item_id])
self.update()
The object con
comes from a class that connects to my database. I tested many queries and they all are fast to commit (less than 0,01 second) so I'm pretty sure the problem does not come from there. So I am wondering if implementing my own dataChanged function is altering Qt's functionnalities ?
Thanks for your answers
答案1
得分: 0
我找到了一种方法来避免信号dataChanged
的延迟,因为它直接与setData
方法相关联。我发现连接到我的数据库非常耗时,因为我正在进行独立连接。所以我用以下命令创建了一个pool
:self.pool = oracledb.create_pool(user=self.username, password=self.password, port=self.port, service_name=self.service, dsn=self.dsn, min=2, max=2, increment=0)
。这允许用户同时处理2个连接。此外,我创建了另一个setData
方法,当不必要时不调用dataChanged
信号。这完全解决了我的问题,以下是新方法:
def setData2(self, index: QModelIndex, value, role: int) -> bool:
if role != Qt.EditRole:
return False
item: TreeItem = self.get_item(index)
result: bool = item.set_data(index.column(), value)
return result
希望这也解决了你的问题!
英文:
I found a way to not get the slowness of the signal dataChanged
as it is directly linked with the setData
method. I saw that connecting to my database is pretty long as I am doing standalones connections. So I created a pool
with the following command : self.pool = oracledb.create_pool(user= self.username, password= self.password, port= self.port, service_name= self.service, dsn= self.dsn, min = 2, max = 2, increment = 0)
. it allows the user to handle 2 connection at the same time. Also, I created another setData method that does not call the dataChanged
signal when it is not necessary. It solved entirely my problem, here is the new method :
def setData2(self, index: QModelIndex, value, role: int) -> bool:
if role != Qt.EditRole:
return False
item: TreeItem = self.get_item(index)
result: bool = item.set_data(index.column(), value)
return result
Hope it solved your issue too !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论