1

我有以下代码,想知道是否有办法让它更有效。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_()
4

1 回答 1

1

.scrollToItem您的问题与or没有任何关系.setCurrentItem。方法中的循环logLoop不会给 Qt 事件循环任何更新事物的机会。解决它的一种方法是,让 Qt 有机会使用QApplication.processEvents(). 因此,如果您修改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)应该是您的选择。QTableViewQTreeView

于 2012-10-26T17:55:55.867 回答