import sys
from PyQt5 import Qt, QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import TableUpdate_ui
class TblUpd
(QtWidgets.
QDialog, TableUpdate_ui.
Ui_Dialog): def __init__(self, parent=None):
self.setupUi(self)
self.pushButton_update.clicked.connect(self.on_update_btn_click)
self.pushButton_add.clicked.connect(self.on_add_btn_click)
self.pushButton_insert.clicked.connect(self.on_insert_btn_click)
self.tabledata = []
# set the table model
self.tablemodel = MyTableModel(self.tabledata, ['Controls'], self)
self.tableView.horizontalHeader().setVisible(True)
self.tableView.setModel(self.tablemodel)
self.tableView.resizeColumnsToContents()
self.tableView.selectRow(0)
def on_update_btn_click(self):
print('Update button clicked.')
print('lineEdit.text = {0}'.format(self.lineEdit.text()))
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
selectedindex = self.tableView.currentIndex()
print(self.tablemodel.setData(selectedindex, value,
QtCore.Qt.EditRole))
self.tableView.resizeColumnsToContents()
pass
def on_add_btn_click(self):
print('Insert button clicked.')
print('lineEdit.text = {0}'.format(self.lineEdit.text()))
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
self.tablemodel.arraydata.extend([[value]])
self.tablemodel.layoutChanged.emit()
self.tableView.resizeColumnsToContents()
def on_insert_btn_click(self):
print('Insert clicked.')
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
selectedindex = self.tableView.currentIndex()
self.tablemodel.arraydata.insert(selectedindex.row(), [value])
self.tablemodel.layoutChanged.emit()
self.tableView.resizeColumnsToContents()
pass
def __init__(self, datain, headerdata, parent=None):
"""
Args:
datain: a list of lists\n
headerdata: a list of strings
"""
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
if parent.isValid():
return 0
return len(self.arraydata)
def columnCount(self, parent):
if parent.isValid():
return 0
else:
return 1
def data(self, index, role):
print('In data()')
if not index.isValid():
print('Invalid index in MyModel>data')
elif role != QtCore.Qt.DisplayRole:
else:
retval
= QtCore.
QVariant(self.
arraydata[index.
row()][index.
column()]) print(self.arraydata[index.row()][index.column()])
print(retval)
return retval
def setData(self, index, value, role):
if role == QtCore.Qt.EditRole and index.isValid():
print(index.row())
self.arraydata[index.row()] = [value]
print('Return from rowCount: {0}'.format(self.rowCount(index)))
self.dataChanged.emit(index, index, [QtCore.Qt.DisplayRole])
return True
return False
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.
QVariant(self.
headerdata[col
])
def main():
tblupd = TblUpd()
tblupd.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
import sys
from PyQt5 import Qt, QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import TableUpdate_ui
class TblUpd(QtWidgets.QDialog, TableUpdate_ui.Ui_Dialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)
self.pushButton_update.clicked.connect(self.on_update_btn_click)
self.pushButton_add.clicked.connect(self.on_add_btn_click)
self.pushButton_insert.clicked.connect(self.on_insert_btn_click)
self.tabledata = []
# set the table model
self.tablemodel = MyTableModel(self.tabledata, ['Controls'], self)
self.tableView.horizontalHeader().setVisible(True)
self.tableView.setModel(self.tablemodel)
self.tableView.resizeColumnsToContents()
self.tableView.selectRow(0)
def on_update_btn_click(self):
print('Update button clicked.')
print('lineEdit.text = {0}'.format(self.lineEdit.text()))
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
selectedindex = self.tableView.currentIndex()
print(self.tablemodel.setData(selectedindex, value,
QtCore.Qt.EditRole))
self.tableView.resizeColumnsToContents()
pass
def on_add_btn_click(self):
print('Insert button clicked.')
print('lineEdit.text = {0}'.format(self.lineEdit.text()))
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
self.tablemodel.arraydata.extend([[value]])
self.tablemodel.layoutChanged.emit()
self.tableView.resizeColumnsToContents()
def on_insert_btn_click(self):
print('Insert clicked.')
if self.lineEdit.text() == '':
value = 'blank'
else:
value = self.lineEdit.text()
print('value = {0}'.format(value))
selectedindex = self.tableView.currentIndex()
self.tablemodel.arraydata.insert(selectedindex.row(), [value])
self.tablemodel.layoutChanged.emit()
self.tableView.resizeColumnsToContents()
pass
class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None):
"""
Args:
datain: a list of lists\n
headerdata: a list of strings
"""
QtCore.QAbstractTableModel.__init__(self, parent)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
if parent.isValid():
return 0
return len(self.arraydata)
def columnCount(self, parent):
if parent.isValid():
return 0
else:
return 1
def data(self, index, role):
print('In data()')
if not index.isValid():
print('Invalid index in MyModel>data')
retval = QtCore.QVariant()
elif role != QtCore.Qt.DisplayRole:
retval = QtCore.QVariant()
else:
retval = QtCore.QVariant(self.arraydata[index.row()][index.column()])
print(self.arraydata[index.row()][index.column()])
print(retval)
return retval
def setData(self, index, value, role):
if role == QtCore.Qt.EditRole and index.isValid():
print(index.row())
self.arraydata[index.row()] = [value]
print('Return from rowCount: {0}'.format(self.rowCount(index)))
self.dataChanged.emit(index, index, [QtCore.Qt.DisplayRole])
return True
return False
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.headerdata[col])
return QtCore.QVariant()
def main():
app = QtWidgets.QApplication(sys.argv)
tblupd = TblUpd()
tblupd.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To copy to clipboard, switch view to plain text mode
Bookmarks