如何解决向我的 QTreeView 添加行时 dataChanged 信号变慢的问题?

huangapple go评论71阅读模式
英文:

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方法相关联。我发现连接到我的数据库非常耗时,因为我正在进行独立连接。所以我用以下命令创建了一个poolself.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 !

huangapple
  • 本文由 发表于 2023年6月22日 19:21:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定