'QT Python: How to properly reimplement data method for QSqlTableModel for it to successfully update database?
I am using QSqlTableModel and QTableView to represent my database. In the last column, called "Validated" I have a value either 1 or 0 (this will be a checkbox, but for simplicity leaving it like this). If the value is 1, the whole row should turn green, otherwise left as normal colour.
The model works fine without reimplementing the data method. However, when implementing the data method as follows:
def data(self,index,role):
if (role == Qt.BackgroundRole) and (self.record(index.row()).value("VALIDATED") == 1):
return QColor(0,128,0)
return super().data(index, role)
The table is updated with the changes and the colouring works, but the database is now never updated with the changes. I have tried adding a datachanged signal as well, but no luck.
I have followed the format given by previous questions such as PyQt - trouble with reimplementing data method of QSqlTableModel. And no such luck.
Any help would be greatly appreciated.
See below for the whole simplified code:
import sys, logging, os
from PySide2.QtCore import Qt
from PySide2.QtSql import QSqlDatabase
from PySide2.QtWidgets import QApplication, QTabWidget, QVBoxLayout, QMainWindow, QAction, QPushButton, QWidget, QAbstractScrollArea, QTableView, QGridLayout
from PySide2.QtGui import QKeySequence, QColor
from PySide2.QtSql import QSqlTableModel
log = logging.getLogger("test-logger")
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
class SQLTableModel(QSqlTableModel):
def __init__(self, db_table):
QSqlTableModel.__init__(self)
self.setEditStrategy(QSqlTableModel.OnRowChange)
self.setTable(db_table)
self.select()
log.info(self.database())
def data(self,index,role):
if (role == Qt.BackgroundRole) and (self.record(index.row()).value("VALIDATED") == 1):
return QColor(0,128,0)
return super().data(index, role)
class TableWidget(QWidget):
def __init__(self, db_table, statusBar):
QWidget.__init__(self)
# Getting the Model
self.tabledb = db_table
self.model = SQLTableModel(db_table)
self.statusBar = statusBar
self.CreateUI()
def CreateUI(self):
# Creating a QTableView
self.table_view = QTableView()
self.table_view.setModel(self.model)
self.table_view.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
self.table_view.setColumnHidden(0, True)
self.main_layout = QGridLayout()
self.main_layout.addWidget(self.table_view)
self.setLayout(self.main_layout)
self.table_view.show()
class MainWindow(QMainWindow):
''' Sets up main window ui, with multiple tabed tables'''
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("test")
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("test")
self.db.open()
self.CreateUI()
self.show()
def CreateUI(self):
''' Create UI elements '''
##Setup database sheets
sheet_table = TableWidget("Product_Structure",self.statusBar)
self.setCentralWidget(sheet_table)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
