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_()
Related
This code I have taken from https://bravo.hatenablog.jp/entry/2016/01/18/093059 .
In this I want to add header "Quantity" at run time if needed.
Before creating table it should ask for how many headers? If user supply 4 then create table with "Quantity" headers also. Otherwise create default headers (Three).
I have tried it but not found any solution.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Model(QAbstractItemModel):
headers = "Toppings", "Udon/Soba", "Hot/Cold"
def __init__(self, parent=None):
super(Model, self).__init__(parent)
self.items = []
def index(self, row, column, parent=QModelIndex()):
return self.createIndex(row, column, None)
def parent(self, child):
return QModelIndex()
def rowCount(self, parent=QModelIndex()):
return len(self.items)
def columnCount(self, parent=QModelIndex()):
return len(self.headers)
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
try:
return self.items[index.row()][index.column()]
except:
return
return
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role != Qt.DisplayRole:
return
if orientation == Qt.Horizontal:
return self.headers[section]
def addRow(self, topping, menkind, hotcold):
self.beginInsertRows(QModelIndex(), len(self.items), 1)
self.items.append([topping, menkind, hotcold])
self.endInsertRows()
def removeRows(self, rowIndexes):
for row in sorted(rowIndexes, reverse=True):
self.beginRemoveRows(QModelIndex(), row, row + 1)
del self.items[row]
self.endRemoveRows()
def flags(self, index):
return super(Model, self).flags(index) | Qt.ItemIsEditable
def setData(self, index, value, role=Qt.EditRole):
if role == Qt.EditRole:
self.items[index.row()][index.column()] = value
return True
return False
class View(QTreeView):
def __init__(self, parent=None):
super(View, self).__init__(parent)
self.setItemsExpandable(False)
self.setIndentation(0)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
def drawBranches(self, painter, rect, index):
return
class InputWidget(QWidget):
def __init__(self, parent=None):
super(InputWidget, self).__init__(parent)
layout = QVBoxLayout()
toppings = ("Kitsune", "Tanuki", "Tempura", "Tsukimi", "Meat", "Curry")
self.toppingInput = QComboBox()
for topping in toppings:
self.toppingInput.addItem(topping)
layout.addWidget(self.toppingInput)
self.bgrp = QGroupBox()
udon = QRadioButton('Udon')
udon.setChecked(True)
soba = QRadioButton('Soba')
btnlayout = QHBoxLayout()
btnlayout.addWidget(udon)
btnlayout.addWidget(soba)
self.bgrp.setLayout(btnlayout)
layout.addWidget(self.bgrp)
self.udonsoba = udon, soba
self.bgrp_temp = QGroupBox()
hot = QRadioButton('Warm')
hot.setChecked(True)
cold = QRadioButton('Cold')
btnlayout_temp = QHBoxLayout()
btnlayout_temp.addWidget(hot)
btnlayout_temp.addWidget(cold)
self.bgrp_temp.setLayout(btnlayout_temp)
layout.addWidget(self.bgrp_temp)
self.hotcold = hot, cold
self.addButton = QPushButton('OK')
layout.addWidget(self.addButton)
layout.addStretch()
self.setLayout(layout)
def values(self):
topping = self.toppingInput.currentText()
udonsoba = '?'
for btn in self.udonsoba:
if btn.isChecked():
udonsoba = btn.text()
break
hotcold = '?'
for btn in self.hotcold:
if btn.isChecked():
hotcold = btn.text()
break
return topping, udonsoba, hotcold
class Delegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(Delegate, self).__init__(parent)
def createEditor(self, parent, option, index):
return QLineEdit(parent)
def setEditorData(self, editor, index):
value = index.model().data(index, Qt.DisplayRole)
editor.setText(value)
def setModelData(self, editor, model, index):
model.setData(index, editor.text())
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.view = View(self)
self.model = Model(self)
self.view.setModel(self.model)
self.view.setItemDelegate(Delegate())
self.setCentralWidget(self.view)
self.inputWidget = InputWidget()
self.inputWidget.addButton.clicked.connect(self.addItem)
self.addDock = QDockWidget('Input', self)
self.addDock.setWidget(self.inputWidget)
self.addDock.setAllowedAreas(Qt.AllDockWidgetAreas)
self.addDockWidget(Qt.RightDockWidgetArea, self.addDock)
self.addDock.hide()
toolBar = QToolBar()
self.addToolBar(toolBar)
delButton = QPushButton('Delete')
delButton.clicked.connect(self.removeItems)
toolBar.addWidget(delButton)
self.addButton = QPushButton('Add')
self.addButton.clicked.connect(self.addDock.show)
toolBar.addWidget(self.addButton)
def addItem(self):
self.model.addRow(*self.inputWidget.values())
def selectedRows(self):
rows = []
for index in self.view.selectedIndexes():
if index.column() == 0:
rows.append(index.row())
return rows
def removeItems(self):
self.model.removeRows(self.selectedRows())
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.show()
w.raise_()
app.exec_()
if __name__ == '__main__':
main()
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.
So i'm writing an application and using QTableView with QAbstractTableModel to display my data. In each row a checkbox is placed in the first column and when it is checked or unchecked, i want a pyqtsignal CheckBoxValue be emitted to MyTable. But it seems that the connected function self.checkboxchecked in MyTable is not called. Although I used to use pyqtSignal several times without any issue, i'm stuck here and couldn't solve it out.
Thank you all for spending time taking care of my question!
import operator # used for sorting
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
class MyTable(QWidget):
Checkboxcheckvalue = pyqtSignal(bool)
Rowselected = pyqtSignal(int)
doubleClicked = pyqtSignal(int)
def __init__(self, *args):
QWidget.__init__(self, *args)
# setGeometry(x_pos, y_pos, width, height)
# self.setGeometry(70, 150, 1326, 582)
self.dataList = []
self.header = []
self.currentrow = []
self.setWindowTitle("Click on the header to sort table")
self.table_model = MyTableModel(self, self.dataList, self.header)
self.table_view = QTableView()
#self.table_view.setSelectionMode(QAbstractItemView.SingleSelection)
self.table_view.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
self.table_view.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
# bind cell click to a method reference
self.table_view.clicked.connect(self.showSelection)
self.table_view.clicked.connect(self.selectRow)
self.table_view.doubleClicked.connect(self.doubleclickedaction)
self.table_model.CheckBoxValue.connect(self.checkboxchecked)
self.table_view.setModel(self.table_model)
# enable sorting
self.table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(self.table_view)
self.setLayout(layout)
def update_model(self):
self.table_model = MyTableModel(self, self.dataList, self.header)
self.table_view.setModel(self.table_model)
self.table_view.update()
def findLabelName(self,rowindex):
return self.dataList[rowindex][-1]
def doubleclickedaction(self,index):
self.doubleClicked.emit(index.row())
self.currentrow = index.row()
print ('doubleclicked',self.findLabelName(index.row()))
def checkboxchecked(self,value):
print ('table checkboxchecked',self.currentrow,value)
# self.currentrow = index.row()
if value == True:
self.Checkboxcheckvalue.emit(True)
else:
self.Checkboxcheckvalue.emit(False)
def selectedLabel(self,index):
return self.findLabelName(index.row())
def showSelection(self, item):
cellContent = item.data()
# print(cellContent) # test
sf = "You clicked on {}".format(cellContent)
# display in title bar for convenience
self.setWindowTitle(sf)
def selectRow(self, index):
self.Rowselected.emit(index.row())
self.currentrow = index.row()
print("current row is %d", index.row())
pass
class MyTableModel(QAbstractTableModel):
"""
keep the method names
they are an integral part of the model
"""
CheckBoxValue = pyqtSignal(bool)
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def setDataList(self, mylist):
self.mylist = mylist
self.layoutAboutToBeChanged.emit()
self.dataChanged.emit(self.createIndex(0, 0), self.createIndex(self.rowCount(0), self.columnCount(0)))
self.layoutChanged.emit()
def rowCount(self, parent):
if self.mylist != []:
return len(self.mylist)
else:
return 0
def columnCount(self, parent):
if self.mylist != []:
return len(self.mylist[0])-1
else:
return 0
def data(self, index, role):
if not index.isValid():
return None
if (index.column() == 0):
value = self.mylist[index.row()][index.column()].text()
else:
value = self.mylist[index.row()][index.column()]
if role == QtCore.Qt.EditRole:
return value
elif role == QtCore.Qt.DisplayRole:
return value
elif role == QtCore.Qt.CheckStateRole:
if index.column() == 0:
if self.mylist[index.row()][index.column()].isChecked():
return QtCore.Qt.Checked
else:
return QtCore.Qt.Unchecked
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"""
if col != 0:
pass
else:
col = -1
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist, key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))
print 'sorted'
def flags(self, index):
if not index.isValid():
return None
if index.column() == 0:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role):
if not index.isValid():
return False
if role == QtCore.Qt.CheckStateRole and index.column() == 0:
if value == QtCore.Qt.Checked:
self.mylist[index.row()][index.column()].setChecked(True)
print('checked',index.row())
self.CheckBoxValue.emit(True)
else:
self.mylist[index.row()][index.column()].setChecked(False)
print('unchecked',index.row())
self.CheckBoxValue.emit(False)
self.dataChanged.emit(index, index)
return True
if __name__ == '__main__':
app = QApplication([])
header = ['name', 'type', 'segment', 'exit', 'entry']
dataList = [
[QtGui.QCheckBox('line9'), 'line', '058176', '01', '1705','line9'],
[QtGui.QCheckBox('line3'), 'line', '058176', '02', '1705','line3'],
[QtGui.QCheckBox('line6'), 'line', '058176', '03', '1705','line6'],
[QtGui.QCheckBox('line1'), 'line', '058176', '04', '1705','line'],
[QtGui.QCheckBox('line4'), 'line', '058176', '01', '1705','line4'],
[QtGui.QCheckBox('area4'), 'area', '058176', '02', '1705','area4'],
[QtGui.QCheckBox('area2'), 'area', '058176', '02', '1705','area2'],
[QtGui.QCheckBox('area8'), 'area', '058176', '01', '1705','area8'],
]
win = MyTable()
win.dataList = dataList
win.header = header
win.update_model()
win.show()
app.exec_()
In your case you have created a table_model in the MyTable constructor, and with that object you have made the connection, but later in the update_model method you have created a new model so the previous model has been deleted and its connections as well.
def update_model(self):
self.table_model = MyTableModel(self, self.dataList, self.header)
self.table_view.setModel(self.table_model)
self.table_model.CheckBoxValue.connect(self.checkboxchecked)
self.table_view.update()
Although I prefer to update the model, instead of creating a new model for it, I would create a method to update the data:
class MyTableModel(QAbstractTableModel):
"""
keep the method names
they are an integral part of the model
"""
CheckBoxValue = pyqtSignal(bool)
def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header
def update_model(self, mylist, header):
self.beginResetModel()
self.mylist = mylist
self.header = header
self.endResetModel()
[...]
and in the update_model of the Table:
class MyTable(QWidget):
[...]
def update_model(self):
self.table_model.update_model(self.dataList, self.header)
Note: I don't believe this is a duplicate of How to programmatically change/update data in Python PyQt4 TableView? as I don't want to insert new rows. I want to edit the table data.
I have changed my code below. The table holds integers in columns titled "2" and "3". When changing the value of the QSpinBox, I want the numbers in the table to get multiplied by the value from the QSpinBox programatically.
Question: How can I programmatically multiply the values in these columns by the spinbox multiplier value?
Note: if the UI doesn't exist, the model still needs to work (without this multiplier feature). Meaning; I believe that the spinbox must initiate model data edit.
import sys
try:
from PySide2 import QtWidgets
except ImportError:
from PyQt5 import QtWidgets
try:
from PySide2 import QtCore
except ImportError:
from PyQt5 import QtCore
class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, table_data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.table_data = table_data
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return len(self.table_data[0])
def flags(self, index):
original_flags = super(MyTableModel, self).flags(index)
return original_flags | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
item = index.internalPointer()
if item is not None:
print(item)
value = self.table_data[row][column]
return value
def setData(self, index, value, role=QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
self.table_data[row][column] = value
self.dataChanged.emit(index, index)
return True
return QtCore.QAbstractTableModel.setData(self, index, value, role)
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.view = QtWidgets.QTableView()
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addWidget(self.view)
table_data = [['HD', '1920', '1080', 'other', 'stuff', 'here'], ['lowres', '640', '480', 'other', 'stuff', 'here']]
self.proxy_model = QtCore.QSortFilterProxyModel()
self.model = MyTableModel(table_data=table_data)
self.proxy_model.setSourceModel(self.model)
self.proxy_model.setDynamicSortFilter(True)
self.view.setModel(self.proxy_model)
self.proxy_model.dataChanged.connect(self.on_data_changed)
self.view.setSortingEnabled(True) # requires proxy model
self.view.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.view.horizontalHeader().setStretchLastSection(True)
self.view.horizontalHeader().setSectionsMovable(True)
def on_data_changed(self, _from, _to):
model = _from.model() # proxy model
model.blockSignals(True)
for index in self.view.selectionModel().selectedIndexes():
model.setData(index, _from.data())
model.blockSignals(False)
print('data was changed in table')
def change_value(self, col, multiplier):
print('Multiply the value (for all rows) in column %s with: %s' % (col, multiplier))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
m = QtWidgets.QWidget()
w = Widget()
e = QtWidgets.QSpinBox()
e.setValue(1)
e.valueChanged.connect(lambda: w.change_value(col=1, multiplier=e.value()))
e.valueChanged.connect(lambda: w.change_value(col=2, multiplier=e.value()))
l = QtWidgets.QHBoxLayout()
l.addWidget(w)
l.addWidget(e)
m.setLayout(l)
m.show()
sys.exit(app.exec_())
Am I supposed to execute self.proxy_mode.setData() and if so, how?
This is how I solved it. Not completely sure about getting the index via mapFromSource, but it works.
import sys
import copy
try:
from PySide2 import QtWidgets
except ImportError:
from PyQt5 import QtWidgets
try:
from PySide2 import QtCore
except ImportError:
from PyQt5 import QtCore
class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, table_data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.table_data = table_data
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return len(self.table_data[0])
def flags(self, index):
original_flags = super(MyTableModel, self).flags(index)
return original_flags | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
item = index.internalPointer()
if item is not None:
print(item)
value = self.table_data[row][column]
return value
def setData(self, index, value, role=QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
self.table_data[row][column] = value
self.dataChanged.emit(index, index)
return True
return QtCore.QAbstractTableModel.setData(self, index, value, role)
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.view = QtWidgets.QTableView()
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addWidget(self.view)
table_data = [['HD', '1920', '1080', 'other', 'stuff', 'here'], ['lowres', '640', '480', 'other', 'stuff', 'here']]
self._original_table_data = copy.deepcopy(table_data)
self.proxy_model = QtCore.QSortFilterProxyModel()
self.model = MyTableModel(table_data=table_data)
self.proxy_model.setSourceModel(self.model)
self.proxy_model.setDynamicSortFilter(True)
self.view.setModel(self.proxy_model)
self.proxy_model.dataChanged.connect(self.on_data_changed)
self.view.setSortingEnabled(True) # requires proxy model
self.view.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.view.horizontalHeader().setStretchLastSection(True)
self.view.horizontalHeader().setSectionsMovable(True)
def on_data_changed(self, _from, _to):
model = _from.model() # proxy model
model.blockSignals(True)
for index in self.view.selectionModel().selectedIndexes():
model.setData(index, _from.data())
model.blockSignals(False)
print('data was changed in table')
def change_value(self, col, multiplier):
print('Multiply the value (for all rows) in column %s with: %s' % (col, multiplier))
index = self.proxy_model.mapFromSource(QtCore.QModelIndex())
row_count = self.proxy_model.rowCount(index)
for row in range(0, row_count):
original_value = self._original_table_data[row][col]
index = self.proxy_model.index(row, col)
self.proxy_model.setData(index, int(original_value)*int(multiplier))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
m = QtWidgets.QWidget()
w = Widget()
q = QtWidgets.QLabel('Multiplier:')
e = QtWidgets.QSpinBox()
e.setValue(1)
e.valueChanged.connect(lambda: w.change_value(col=1, multiplier=e.value()))
e.valueChanged.connect(lambda: w.change_value(col=2, multiplier=e.value()))
l = QtWidgets.QHBoxLayout()
l.addWidget(w)
l.addWidget(q)
l.addWidget(e)
m.setLayout(l)
m.show()
sys.exit(app.exec_())
I want to make the first column with checkbox, and get the check status of that, how can I do that? I override the flag(), but it seems do not work, I am sure where is the problem?
And I got this links, but it does work for my code either. And I don't want to use delegate, because it is too complicated.
data = [['00','01','02'],
['10','11','12'],
['20','21','22']]
class MainWindow(QWidget):
def __init__(self, parent=None, *args):
super(MainWindow, self).__init__(parent)
clipTableWidget = QTableWidget()
self.model = TModel(data, self)
clipTableView = QTableView()
clipTableView.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(clipTableView)
self.setLayout(layout)
class TModel(QAbstractTableModel):
def __init__(self, datain, parent=None):
super(TModel, self).__init__(parent)
self.arraydata = datain
def rowCount(self, parent=QModelIndex()):
return len(self.arraydata)
def columnCount(self, parent=QModelIndex()):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
def flags(self, index):
if not index.isValid():
return QVariant()
elif index.column() == 1:
return Qt.ItemIsSelectable|Qt.ItemIsEnabled|Qt.ItemIsUserCheckable
return QVariant()
Ok here is the solution to your problem. The reason Why your flags weren't working is because those flags dont work on text , they are meant for QItems .
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MainWindow(QWidget):
def __init__(self, parent=None, *args):
super(MainWindow, self).__init__(parent)
data = [['00','01','02'],
['10','11','12'],
['20','21','22']]
clipTableWidget = QTableWidget()
self.model = QStandardItemModel(self)
clipTableView = QTableView()
count1=0
for row in data:
count2 = 0
for column in row:
if count2 == 0:
item = QStandardItem(column)
item.setCheckable(True)
item.setCheckState(False)
item.setFlags(Qt.ItemIsUserCheckable| Qt.ItemIsEnabled)
self.model.setItem(count1,count2,item)
count2+=1
else:
item = QStandardItem(column)
self.model.setItem(count1,count2,item)
count2+=1
count1+=1
clipTableView.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(clipTableView)
self.setLayout(layout)
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()