简体   繁体   English

如何提高PyQt4 QListWidget的效率

[英]How do I improve the efficiency of the PyQt4 QListWidget

I have the following code and am wondering if there is a way to make this more efficient. 我有以下代码,并且想知道是否有一种方法可以使此效率更高。 The setCurrentItem() and scrollToItem() functions seem to slow the process down considerable. setCurrentItem()和scrollToItem()函数似乎会大大减慢该过程。 Also, I would like to see the items show up in the list as they are added instead of all at once after the loop has completed. 另外,我希望看到这些项目在添加完成后出现在列表中,而不是在循环完成后立即全部显示。 Any help or discussion would be greatly appreaciated. 任何帮助或讨论都将不胜感激。

import sys
from math import *
#from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import (Qt, SIGNAL, QSize)
from PyQt4.QtGui import (QApplication, QDialog, QLabel, QListWidget, QListWidgetItem,     QPushButton, QScrollArea, QTextDocument, QVBoxLayout)
from time import localtime, strftime
import time

class LogDlg(QDialog):

  def __init__(self, parent=None):
    super(LogDlg, self).__init__(parent)
    self.resize(450, 380)

    self.log1 = QListWidget()
    self.log2 = QListWidget()
    lbl = QLabel()
    lbl_2 = QLabel()
    lbl.setText("Communications Log")
    lbl_2.setText("Command/Data Log")
    self.pushButton = QPushButton()
    self.pushButton.setMaximumSize(QSize(110, 24))
    self.pushButton.setObjectName("pushButton")

    self.pushbutton = QPushButton
    self.pushButton.setText("Start Log Loop")
    layout = QVBoxLayout()
    layout.addWidget(self.pushButton)
    layout.addWidget(lbl)
    layout.addWidget(self.log1)
    layout.addWidget(lbl_2)
    layout.addWidget(self.log2)
    self.setLayout(layout)
    self.setWindowTitle("Transaction Logs")
    self.connect(self.pushButton,SIGNAL("clicked()"),self.logLoop) 
    self.time = time.time()

  def logLoop(self):
    for i in range(1000):
        print i
        self.addLog("This is a test","c",True)      

  def timeStamp(self):
    now = time.time()
    localtime = time.localtime(now)
    milliseconds = '%02d' % int((now - int(now)) * 100)
    val = time.strftime('%H:%M:%S.', localtime) + milliseconds
    return val

  def clearUi(self):
    self.log1.clear()
    self.log2.clear()

  def addLog(self, data, type="c", ts=False):
#        pass
    t = self.timeStamp()
    if ts == True:
        data = t + " " + data
    if type == "c":
        self.listItem1 = QListWidgetItem()
        self.listItem1.setText(data)
        self.log1.addItem(self.listItem1)
#        self.log1.scrollToItem(self.listItem1)
        self.log1.setCurrentItem(self.listItem1)


    elif type == "d":
        self.listItem2 = QListWidgetItem()
        self.listItem2.setText(data)
        self.log2.addItem(self.listItem2)
#        self.log2.scrollToItem(self.listItem2)
        self.log2.setCurrentItem(self.listItem2)


app = QApplication(sys.argv)
form = LogDlg()
form.open()
app.exec_()

Your issue doesn't have anything to do with .scrollToItem or .setCurrentItem . 您的问题与.scrollToItem.setCurrentItem没有任何.setCurrentItem The loop in logLoop method doesn't give any chance for the Qt event loop to update things. logLoop方法中的循环不会给Qt事件循环更新任何机会。 One way to solve it is, give the Qt a chance to update the GUI with QApplication.processEvents() . 解决该问题的一种方法是,让Qt有机会使用QApplication.processEvents()更新GUI。 So if you modify the logLoop as following, you should see the items as they are added: 因此,如果按以下方式修改logLoop ,则应在添加项目时看到它们:

  def logLoop(self):
    for i in range(1000):
        print i
        self.addLog("This is a test","c",True)
        QApplication.processEvents()

This is useful to some degree. 这在一定程度上很有用。 If the time between processEvents is small enough you'll get a responsive UI. 如果processEvents之间的时间足够短,您将获得响应式UI。 But once things get more complicated and the time to complete tasks for each segment increases, you'll need to delegate that piece of code to a separate thread ( QThread ) in order to keep the GUI responsive. 但是,一旦事情变得更加复杂,并且完成每个段的任务的时间增加,您就需要将该代码段委派给单独的线程( QThread ),以保持GUI的响应速度。

One other issue is, things will get slower once you have a large number of items in the QListWidget . 另一个问题是,一旦QListWidget有大量项目,事情就会变慢。 You may notice that adding the 25th item is faster than 925th item. 您可能会注意到,添加第25个项目比添加第925个项目要快。 That's because QListWidget (or QTableWidget / QTreeWidget ) doesn't scale well. 这是因为QListWidget (或QTableWidget / QTreeWidget )不能很好地扩展。 If you are going to have large number of items, model/view framework ( QListView / QTableView / QTreeView ) should be your choice. 如果您将要拥有大量项目,则应选择模型/视图框架( QListView / QTableView / QTreeView )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM