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