簡體   English   中英

如何提高PyQt4 QListWidget的效率

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

我有以下代碼,並且想知道是否有一種方法可以使此效率更高。 setCurrentItem()和scrollToItem()函數似乎會大大減慢該過程。 另外,我希望看到這些項目在添加完成后出現在列表中,而不是在循環完成后立即全部顯示。 任何幫助或討論都將不勝感激。

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_()

您的問題與.scrollToItem.setCurrentItem沒有任何.setCurrentItem logLoop方法中的循環不會給Qt事件循環更新任何機會。 解決該問題的一種方法是,讓Qt有機會使用QApplication.processEvents()更新GUI。 因此,如果按以下方式修改logLoop ,則應在添加項目時看到它們:

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

這在一定程度上很有用。 如果processEvents之間的時間足夠短,您將獲得響應式UI。 但是,一旦事情變得更加復雜,並且完成每個段的任務的時間增加,您就需要將該代碼段委派給單獨的線程( QThread ),以保持GUI的響應速度。

另一個問題是,一旦QListWidget有大量項目,事情就會變慢。 您可能會注意到,添加第25個項目比添加第925個項目要快。 這是因為QListWidget (或QTableWidget / QTreeWidget )不能很好地擴展。 如果您將要擁有大量項目,則應選擇模型/視圖框架( QListView / QTableView / QTreeView )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM