Below is the example code.
import sys
from PyQt5 import QtCore, QtWidgets, QtSql, uic
class FilterProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._filter_value = None
#property
def filter_value(self):
return self._filter_value
#filter_value.setter
def filter_value(self, value):
self._filter_value = value
self.invalidateFilter()
def filterAcceptsRow(self, sourceRow, sourceParent):
if self.filter_value is None:
return super().filterAcceptsRow(sourceRow, sourceParent)
if self.filterKeyColumn() >= 0:
value = (
self.sourceModel()
.index(sourceRow, self.filterKeyColumn(), sourceParent)
.data(self.filterRole())
)
return value == self.filter_value
for column in range(self.columnCount()):
value = (
self.sourceModel()
.index(sourceRow, column, sourceParent)
.data(self.filterRole())
)
if value == self.filter_value:
return True
return False
def setFilterRegExp(self, filter):
self.filter_value = None
super().setFilterRegExp(filter)
class UI(QtWidgets.QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("tableview.ui", self)
self.tableView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("book.db")
db.open()
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("card")
self.model.select()
self.proxy = FilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.tableView.setModel(self.proxy)
self.model.select()
self.refresh.clicked.connect(self.refresh_table)
r = self.model.record()
column_names = [r.field(i).name().title() for i in range(r.count())]
self.comboBox.addItems([x for x in column_names])
self.horizontalHeader = self.tableView.horizontalHeader()
self.horizontalHeader.sectionClicked.connect(
self.tableView_horizontalHeader_sectionClicked
)
self.lineEdit.textChanged.connect(self.lineEdit_textChanged)
def tableView_horizontalHeader_sectionClicked(self, logicalIndex):
menu = QtWidgets.QMenu(self)
values = []
for row in range(self.model.rowCount()):
value = self.model.index(row, logicalIndex).data(self.proxy.filterRole())
values.append(value)
action_all = QtWidgets.QAction("All", self)
action_all.setData(None)
menu.addAction(action_all)
menu.addSeparator()
for value in sorted(list(set(values))):
action = QtWidgets.QAction(str(value), self)
action.setData(value)
menu.addAction(action)
headerPos = self.tableView.mapToGlobal(self.horizontalHeader.pos())
posY = headerPos.y() + self.horizontalHeader.height()
posX = headerPos.x() + self.horizontalHeader.sectionPosition(logicalIndex)
action = menu.exec_(QtCore.QPoint(posX, posY))
if action is not None:
self.proxy.setFilterKeyColumn(logicalIndex)
self.proxy.filter_value = action.data()
def lineEdit_textChanged(self):
search = QtCore.QRegExp(
self.lineEdit.text(), QtCore.Qt.CaseInsensitive, QtCore.QRegExp.RegExp
)
self.proxy.setFilterKeyColumn(self.comboBox.currentIndex())
self.proxy.setFilterRegExp(search)
def refresh_table(self):
print("refresh")
def main():
app = QtWidgets.QApplication(sys.argv)
w = UI()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I am using FilterProxyModel(QtCore.QSortFilterProxyModel), QTableView and QtSql.QSqlTableModel. I am able to filter rows. My question is How to refresh the proxymodel or tableview to show all rows with push button. As per above image the rows was filtered with qmenu. Now i want to refresh and show the all rows after clicked With the Qpushbutton, I want to show all rows with Not only with the menu option but also with the Qpushbutton. Is it possible to refresh, How to do?
If you want to remove the filter then you must set None to filter_value:
def refresh_table(self):
self.proxy.filter_value = None
Related
How can I implement a simple filterable List in PyQt5/PySide2?
Test code:
import sys
from PySide2.QtCore import QAbstractListModel
from PySide2.QtGui import Qt
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QListView
class MyModel(QAbstractListModel):
def __init__(self):
QAbstractListModel.__init__(self)
self.test_data = ['Test','abc', 'Test2', 'aaa', 'bbb']
self.filtered_data = self.test_data
self.filter = ''
self.refresh()
def refresh(self):
if self.filter:
self.filtered_data = [x for x in self.test_data if self.filter in x]
else:
self.filtered_data = self.test_data
def data(self, index, role):
if role == Qt.DisplayRole:
return self.filtered_data[index.row()]
def rowCount(self, index):
return len(self.filtered_data)
def text_changed():
model.filter = le.text()
model.refresh()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
le = QLineEdit()
le.textChanged.connect(text_changed)
model = MyModel()
lv = QListView(model=model)
layout = QVBoxLayout()
layout.addWidget(le)
layout.addWidget(lv)
window.setLayout(layout)
window.show()
app.exec_()
The Filter itself works, but the ListView is not refreshed properly. When I change the filter, the list is not updated, until I move with the mouse to a entry.
That you change the information of an internal list does not notify in sight that something has changed, for each type of change certain methods must be invoked before or after the change.
In this case, beginResetModel and endResetModel are enough
def refresh(self):
self.beginResetModel()
if self.filter:
self.filtered_data = [x for x in self.test_data if self.filter in x]
else:
self.filtered_data = self.test_data
self.endResetModel()
Instead another more elegant solution is to use a QSortFilterProxyModel:
class MyModel(QAbstractListModel):
def __init__(self):
QAbstractListModel.__init__(self)
self.test_data = ['Test','abc', 'Test2', 'aaa', 'bbb']
self.filtered_data = self.test_data
def data(self, index, role):
if role == Qt.DisplayRole:
return self.filtered_data[index.row()]
def rowCount(self, index):
return len(self.filtered_data)
def text_changed():
proxy.filter = le.text()
class SortFilterProxyModel(QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._filter = ""
def filterAcceptsRow(self, sourceRow, sourceParent):
index = self.sourceModel().index(sourceRow, 0, sourceParent)
if self.filter:
return self.filter in index.data()
return True
#property
def filter(self):
return self._filter
#filter.setter
def filter(self, f):
self._filter = f
self.invalidateFilter()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
le = QLineEdit()
le.textChanged.connect(text_changed)
model = MyModel()
proxy = SortFilterProxyModel()
proxy.setSourceModel(model)
lv = QListView(model=proxy)
layout = QVBoxLayout()
layout.addWidget(le)
layout.addWidget(lv)
window.setLayout(layout)
window.show()
app.exec_()
Below is the example code:
import sys
from PyQt5 import QtCore, QtWidgets, QtSql, uic
class FilterProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._filter_value = None
#property
def filter_value(self):
return self._filter_value
#filter_value.setter
def filter_value(self, value):
self._filter_value = value
self.invalidateFilter()
def filterAcceptsRow(self, sourceRow, sourceParent):
if self.filter_value is None:
return super().filterAcceptsRow(sourceRow, sourceParent)
if self.filterKeyColumn() >= 0:
value = (
self.sourceModel()
.index(sourceRow, self.filterKeyColumn(), sourceParent)
.data(self.filterRole())
)
return value == self.filter_value
for column in range(self.columnCount()):
value = (
self.sourceModel()
.index(sourceRow, column, sourceParent)
.data(self.filterRole())
)
if value == self.filter_value:
return True
return False
def setFilterRegExp(self, filter):
self.filter_value = None
super().setFilterRegExp(filter)
class UI(QtWidgets.QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("tableview.ui", self)
self.tableView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("book.db")
db.open()
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("card")
self.model.select()
self.proxy = FilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.tableView.setModel(self.proxy)
self.model.select()
self.edit.clicked.connect(self.edit_items)
self.refresh.clicked.connect(self.refresh_table)
r = self.model.record()
column_names = [r.field(i).name().title() for i in range(r.count())]
self.comboBox.addItems([x for x in column_names])
self.horizontalHeader = self.tableView.horizontalHeader()
self.horizontalHeader.sectionClicked.connect(
self.tableView_horizontalHeader_sectionClicked
)
self.lineEdit.textChanged.connect(self.lineEdit_textChanged)
def tableView_horizontalHeader_sectionClicked(self, logicalIndex):
menu = QtWidgets.QMenu(self)
values = []
for row in range(self.model.rowCount()):
value = self.model.index(row, logicalIndex).data(self.proxy.filterRole())
values.append(value)
action_all = QtWidgets.QAction("All", self)
action_all.setData(None)
menu.addAction(action_all)
menu.addSeparator()
for value in sorted(list(set(values))):
action = QtWidgets.QAction(str(value), self)
action.setData(value)
menu.addAction(action)
headerPos = self.tableView.mapToGlobal(self.horizontalHeader.pos())
posY = headerPos.y() + self.horizontalHeader.height()
posX = headerPos.x() + self.horizontalHeader.sectionPosition(logicalIndex)
action = menu.exec_(QtCore.QPoint(posX, posY))
if action is not None:
self.proxy.setFilterKeyColumn(logicalIndex)
self.proxy.filter_value = action.data()
def lineEdit_textChanged(self):
search = QtCore.QRegExp(
self.lineEdit.text(), QtCore.Qt.CaseInsensitive, QtCore.QRegExp.RegExp
)
self.proxy.setFilterKeyColumn(self.comboBox.currentIndex())
self.proxy.setFilterRegExp(search)
def edit_items(self):
if not self.model.rowCount():
return
index = self.tableView.currentIndex()
if index.isValid():
row = index.row()
else:
row = 0
name_line = QtWidgets.QLineEdit(readOnly=True)
age_edit = QtWidgets.QSpinBox()
gender_combo = QtWidgets.QComboBox()
genders = "M", "F"
gender_combo.addItems(genders)
date_of_birth = QtWidgets.QDateEdit()
date_of_birth.setDisplayFormat("d-MMM-yyyy")
updateButton = QtWidgets.QPushButton("Update")
mapper = QtWidgets.QDataWidgetMapper()
mapper.setSubmitPolicy(QtWidgets.QDataWidgetMapper.ManualSubmit)
mapper.setModel(self.tableView.model())
mapper.addMapping(name_line, 0)
mapper.addMapping(age_edit, 1)
mapper.addMapping(gender_combo, 2)
mapper.addMapping(date_of_birth, 3)
mapper.setCurrentIndex(row)
dialog = QtWidgets.QDialog()
dialog.setWindowTitle("Edit Window")
layout = QtWidgets.QVBoxLayout(dialog)
formLayout = QtWidgets.QFormLayout()
layout.addLayout(formLayout)
formLayout.addRow("Name", name_line)
formLayout.addRow("Age", age_edit)
formLayout.addRow("Gender", gender_combo)
formLayout.addRow("Date of Birth", date_of_birth)
layout.addWidget(updateButton)
updateButton.clicked.connect(dialog.accept)
if dialog.exec_():
mapper.submit()
def refresh_table(self):
print("refresh")
def main():
app = QtWidgets.QApplication(sys.argv)
w = UI()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I am trying to edit row data from Qsqltablemodel using with QDataWidgetMapper(). The date column is in text format in My database table, I want to use date format as "d-MMM-yyyy". When i am trying to edit the row, the date column is setting the default date format as "1-1-2000".
How to do this?
Below is the example Image:
By default QtSql can handle string columns with a specific format such as QDate and QDateTime (as indicated in the sqlite docs), but in this case it does not comply with those formats so Qt does not know how to interpret them and displays them as text. So you must convert that text into QDate, and vice versa, using a delegate:
class ItemDelegate(QtWidgets.QItemDelegate):
def setEditorData(self, editor, index):
if index.column() == 3 and isinstance(editor, QtWidgets.QDateEdit):
text = index.data()
date = QtCore.QDate.fromString(text, "d-MMM-yyyy")
editor.setDate(date)
return
super().setEditorData(editor, index)
def setModelData(self, editor, model, index):
if index.column() == 3 and isinstance(editor, QtWidgets.QDateEdit):
text = editor.date().toString("d-MMM-yyyy")
model.setData(index, text)
return
super().setModelData(editor, model, index)
# ...
mapper = QtWidgets.QDataWidgetMapper()
delegate = ItemDelegate(mapper)
mapper.setItemDelegate(delegate)
# ...
I'm trying to update a table/table model after loading a new csv so it shows the new values. Would updating the widget also some how manage this? I've cut down the code quite a bit below. You can use any 2 column csv:
0, 0, 0, 0, 0, 0,
import pandas as pd
from PySide2 import QtWidgets, QtGui, QtCore
def read_data(fname):
df = pd.read_csv(fname, sep=',', usecols=(0, 1), header=None)
df = df.dropna(how='any')
df = df[pd.to_numeric(df[0], errors='coerce').notnull()]
data_arr = df.to_numpy(dtype='float64')
return data_arr
class CustomTableModel(QtCore.QAbstractTableModel):
def __init__(self, data=None):
QtCore.QAbstractTableModel.__init__(self)
self.load_data(data)
def load_data(self, data):
self.input_x = data[:, 0]
self.input_y = data[:, 1]
self.column_count = 2
self.row_count = len(self.input_x)
def rowCount(self, parent=QtCore.QModelIndex()):
return self.row_count
def columnCount(self, parent=QtCore.QModelIndex()):
return self.column_count
def headerData(self, section, orientation, role):
if role != QtCore.Qt.DisplayRole:
return None
if orientation == QtCore.Qt.Horizontal:
return ("x", "y")[section]
else:
return "{}".format(section)
def data(self, index, role=QtCore.Qt.DisplayRole):
column = index.column()
row = index.row()
if role == QtCore.Qt.DisplayRole:
if column == 0:
return str(self.input_x[row])
elif column == 1:
return str(self.input_y[row])
elif role == QtCore.Qt.BackgroundRole:
return QtGui.QColor(QtCore.Qt.white)
elif role == QtCore.Qt.TextAlignmentRole:
return QtCore.Qt.AlignRight
return None
class Widget(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
try:
self.data
except AttributeError:
self.open_csv()
self.model = CustomTableModel(self.data)
self.table_view = QtWidgets.QTableView()
self.table_view.setModel(self.model)
resize = QtWidgets.QHeaderView.ResizeToContents
self.horizontal_header = self.table_view.horizontalHeader()
self.vertical_header = self.table_view.verticalHeader()
self.horizontal_header.setSectionResizeMode(resize)
self.vertical_header.setSectionResizeMode(resize)
self.horizontal_header.setStretchLastSection(False)
# Creating layout
self.main_layout = QtWidgets.QVBoxLayout()
self.file_button = QtWidgets.QPushButton('CSV Import', self)
self.file_button.clicked.connect(self.open_csv)
self.main_layout.addWidget(self.table_view)
self.main_layout.addWidget(self.file_button)
self.setLayout(self.main_layout)
def open_csv(self):
filename, *_ = QtWidgets.QFileDialog.getOpenFileName(self, self.tr('Open CSV'),
self.tr("~/Desktop/"), self.tr('Files (*.csv)'))
self.data = read_data(filename)
return None
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, widget):
QtWidgets.QMainWindow.__init__(self)
self.setWindowTitle('Linear Plotter')
self.setCentralWidget(widget)
self.menu = self.menuBar()
self.file_menu = self.menu.addMenu('File')
exit_action = QtWidgets.QAction('Exit', self)
exit_action.setShortcut(QtGui.QKeySequence.Quit)
exit_action.triggered.connect(self.close)
self.file_menu.addAction(exit_action)
self.status = self.statusBar()
self.status.showMessage('Data loaded and plotted')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = Widget()
window = MainWindow(widget)
window.show()
sys.exit(app.exec_())
Not sure if QWidget.update() would work? I could not get it working.
How could I implement a QLineEdit in the column header for filtering data inside my QTreeView?
Expected Result something like this:
So far I've got this:
What I have so far is everything but the QLineEdit for the filter.
Full working code example (peewee ORM necessary):
import sys
import re
from peewee import *
from PyQt5 import QtWidgets, QtGui, QtCore
COUNT_PERS_COLS = 3
col_persID, col_persLAST_NAME, col_persFIRST_NAME = range(COUNT_PERS_COLS)
db = SqliteDatabase(':memory:')
def _human_key(key):
parts = re.split(r'(\d*\.\d+|\d+)', key)
return tuple((e.swapcase() if i % 2 == 0 else float(e))
for i, e in enumerate(parts))
class FilterHeader(QtWidgets.QHeaderView):
filterActivated = QtCore.pyqtSignal()
def __init__(self, parent):
super().__init__(QtCore.Qt.Horizontal, parent)
self._editors = []
self._padding = 4
self.setStretchLastSection(True)
# self.setResizeMode(QHeaderView.Stretch) obsolete
self.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.setDefaultAlignment(
QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.setSortIndicatorShown(False)
self.sectionResized.connect(self.adjustPositions)
parent.horizontalScrollBar().valueChanged.connect(
self.adjustPositions)
def setFilterBoxes(self, count):
while self._editors:
editor = self._editors.pop()
editor.deleteLater()
for index in range(count):
editor = QtWidgets.QLineEdit(self.parent())
editor.setPlaceholderText('Filter')
editor.returnPressed.connect(self.filterActivated.emit)
self._editors.append(editor)
self.adjustPositions()
def sizeHint(self):
size = super().sizeHint()
if self._editors:
height = self._editors[0].sizeHint().height()
size.setHeight(size.height() + height + self._padding)
return size
def updateGeometries(self):
if self._editors:
height = self._editors[0].sizeHint().height()
self.setViewportMargins(0, 0, 0, height + self._padding)
else:
self.setViewportMargins(0, 0, 0, 0)
super().updateGeometries()
self.adjustPositions()
def adjustPositions(self):
for index, editor in enumerate(self._editors):
height = editor.sizeHint().height()
editor.move(
self.sectionPosition(index) - self.offset() + 2,
height + (self._padding // 2))
editor.resize(self.sectionSize(index), height)
def filterText(self, index):
if 0 <= index < len(self._editors):
return self._editors[index].text()
return ''
def setFilterText(self, index, text):
if 0 <= index < len(self._editors):
self._editors[index].setText(text)
def clearFilters(self):
for editor in self._editors:
editor.clear()
class HumanProxyModel(QtCore.QSortFilterProxyModel):
def lessThan(self, source_left, source_right):
data_left = source_left.data()
data_right = source_right.data()
if type(data_left) == type(data_right) == str:
return _human_key(data_left) < _human_key(data_right)
return super(HumanProxyModel, self).lessThan(source_left, source_right)
class Person(Model):
persId = CharField()
lastName = CharField()
firstName = CharField()
class Meta:
database = db
class winMain(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
self.setGeometry(300,200,700,500)
self.show()
def createPersonModel(self,parent):
model = QtGui.QStandardItemModel(0, COUNT_PERS_COLS, parent)
#model.setHeaderData(col_persID, QtCore.Qt.Horizontal, "ID")
#model.setHeaderData(col_persLAST_NAME, QtCore.Qt.Horizontal, "Last Name")
#model.setHeaderData(col_persFIRST_NAME, QtCore.Qt.Horizontal, "First Name")
#model.setHorizontalHeaderLabels('One Two Three' .split())
model.setHorizontalHeaderLabels(['ID', 'Last Name', 'First Name'])
return model
def addPerson(self, model, id, last_name, first_name):
model.insertRow(0)
model.setData(model.index(0, col_persID), id)
model.setData(model.index(0, col_persLAST_NAME), last_name)
model.setData(model.index(0, col_persFIRST_NAME), first_name)
def handleFilterActivated(self):
#header = self.tableView.horizontalHeader() # QTableView()-command
header = self.treeView.header()
for index in range(header.count()):
print((index, header.filterText(index)))
def setupUi(self):
self.treeView = QtWidgets.QTreeView(self)
self.treeView.setGeometry(0,0,700,500)
self.treeView.setSortingEnabled(True)
self.treeView.setAlternatingRowColors(True)
self.treeView.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.treeView.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.treeView.setAnimated(True)
self.treeView.setItemsExpandable(True)
#layout = QtWidgets.QVBoxLayout(self)
#layout.addWidget(self.treeView)
header = FilterHeader(self.treeView)
# self.tableView.setHorizontalHeader(header) # QTableView()-command
self.treeView.setHeader(header)
model = self.createPersonModel(self)
self.treeView.setModel(model)
proxy = HumanProxyModel(self)
proxy.setSourceModel(model)
self.treeView.setModel(proxy)
header.setFilterBoxes(model.columnCount())
header.filterActivated.connect(self.handleFilterActivated)
for rec_person in Person.select():
self.addPerson(model, rec_person.persId, rec_person.lastName, rec_person.firstName)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
# create a table for our model
db.create_tables([Person])
# create some sample data for our model
Person.create(persId='1001', lastName='Martin', firstName='Robert')
Person.create(persId='1002', lastName='Smith', firstName='Brad')
Person.create(persId='1003', lastName='Smith', firstName='Angelina')
window = winMain()
sys.exit(app.exec_())
I already use the QSortFilterProxyModel class for sorting the result by clicking into the column header.
You have to override the filterAcceptsRow method by implementing the filtering logic, in the following example it will be filtered using the following rules:
Only the columns that have a different text to the vacuum are considered.
The filtering criterion is that the text placed in the QLineEdit is contained in the corresponding row and column
class HumanProxyModel(QtCore.QSortFilterProxyModel):
# ...
#property
def filters(self):
if not hasattr(self, "_filters"):
self._filters = []
return self._filters
#filters.setter
def filters(self, filters):
self._filters = filters
self.invalidateFilter()
def filterAcceptsRow(self, sourceRow, sourceParent):
for i, text in self.filters:
if 0 <= i < self.columnCount():
ix = self.sourceModel().index(sourceRow, i, sourceParent)
data = ix.data()
if text not in data:
return False
return True
# ...
class winMain(QtWidgets.QMainWindow):
# ...
def handleFilterActivated(self):
header = self.treeView.header()
filters = []
for i in range(header.count()):
text = header.filterText(i)
if text:
filters.append((i, text))
proxy = self.treeView.model()
proxy.filters = filters
I have below snippet to scrap the data from website and showing the result in table view. How to update or refresh the table for every 1 minute or 30 seconds. Is there anyway to do this in 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_()
Please help.
Used Qtimer to to change the model for Table view.
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_()