如何在 PySide 中自动刷新 table 视图?
How to auto refresh table view in PySide?
我有下面的代码片段来从网站上抓取数据并在 table 视图中显示结果。如何每 1 分钟或 30 秒更新或刷新 table。反正有没有在pyside中做到这一点?
import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree
def getData():
url = "" # Removed valid url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []
for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results
class MyWindow(QWidget):
def __init__(self, data_list, header, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Click on column title to sort")
table_model = MyTableModel(self, data_list, header)
table_view = QTableView()
table_view.setModel(table_model)
font = QFont("Verdana", 9)
table_view.setFont(font)
table_view.resizeColumnsToContents()
table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(table_view)
self.setLayout(layout)
class MyTableModel(QAbstractTableModel):
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.header)
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
header, data_list = getData()
app = QApplication([])
win = MyWindow(data_list, header)
win.show()
app.exec_()
请帮忙。
使用 Qtimer 更改 Table 视图的模型。
import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree
import sched, time
s = sched.scheduler(time.time, time.sleep)
def getData():
url = "" # Removed url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []
for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Top Shares")
self.table_model = MyTableModel(self)
self.table_view = QTableView()
self.table_view.setModel(self.table_model)
font = QFont("Verdana", 9)
self.table_view.setFont(font)
self.table_view.resizeColumnsToContents()
self.table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(self.table_view)
self.setLayout(layout)
# Added timer
timer = QTimer(self)
timer.timeout.connect(self.show_data)
timer.start(10000)
def show_data(self):
self.table_model = MyTableModel(self)
self.table_view.setModel(self.table_model)
class MyTableModel(QAbstractTableModel):
class_counter = 0
def __init__(self, parent, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.header, self.mylist = getData()
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.header)
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
app = QApplication([])
win = MyWindow()
win.show()
win.repaint()
win.update()
app.exec_()
我有下面的代码片段来从网站上抓取数据并在 table 视图中显示结果。如何每 1 分钟或 30 秒更新或刷新 table。反正有没有在pyside中做到这一点?
import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree
def getData():
url = "" # Removed valid url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []
for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results
class MyWindow(QWidget):
def __init__(self, data_list, header, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Click on column title to sort")
table_model = MyTableModel(self, data_list, header)
table_view = QTableView()
table_view.setModel(table_model)
font = QFont("Verdana", 9)
table_view.setFont(font)
table_view.resizeColumnsToContents()
table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(table_view)
self.setLayout(layout)
class MyTableModel(QAbstractTableModel):
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.header)
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
header, data_list = getData()
app = QApplication([])
win = MyWindow(data_list, header)
win.show()
app.exec_()
请帮忙。
使用 Qtimer 更改 Table 视图的模型。
import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree
import sched, time
s = sched.scheduler(time.time, time.sleep)
def getData():
url = "" # Removed url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []
for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Top Shares")
self.table_model = MyTableModel(self)
self.table_view = QTableView()
self.table_view.setModel(self.table_model)
font = QFont("Verdana", 9)
self.table_view.setFont(font)
self.table_view.resizeColumnsToContents()
self.table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(self.table_view)
self.setLayout(layout)
# Added timer
timer = QTimer(self)
timer.timeout.connect(self.show_data)
timer.start(10000)
def show_data(self):
self.table_model = MyTableModel(self)
self.table_view.setModel(self.table_model)
class MyTableModel(QAbstractTableModel):
class_counter = 0
def __init__(self, parent, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.header, self.mylist = getData()
def rowCount(self, parent):
return len(self.mylist)
def columnCount(self, parent):
return len(self.header)
def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None
def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
app = QApplication([])
win = MyWindow()
win.show()
win.repaint()
win.update()
app.exec_()