Implement setData for QSqltable to be editable

This commit is contained in:
ccppi 2024-08-06 09:55:48 +02:00
parent 6e55415282
commit 100cb75c09
2 changed files with 21 additions and 13 deletions

View File

@ -7,6 +7,7 @@ from db import addFineFilter
import sysparse import sysparse
import sys import sys
import db as db import db as db
from qsqlmod import SqlQueryModel_editable
DBFILE = "../db/sqlite3.db" DBFILE = "../db/sqlite3.db"
@ -18,7 +19,6 @@ class ColorDelegate(QStyledItemDelegate):
def initStyleOption(self,option,index): def initStyleOption(self,option,index):
super().initStyleOption(option,index) super().initStyleOption(option,index)
data = index.data() data = index.data()
try: try:
value = float(data) value = float(data)
except: except:
@ -39,15 +39,13 @@ class ColorDelegate(QStyledItemDelegate):
else: else:
option.backgroundBrush = QtGui.QColor("white") option.backgroundBrush = QtGui.QColor("white")
class ValidationWorker(QObject): class ValidationWorker(QObject):
finished = Signal() finished = Signal()
def run(self): def run(self):
ret = db.isStillValid(DBFILE,0) ret = db.isStillValid(DBFILE,0)
if ret == 0: if ret == 0:
self.finished.emit() self.finished.emit()
class Worker(QObject): class Worker(QObject):
pwprompt = Signal() pwprompt = Signal()
pw = Signal(str) pw = Signal(str)
@ -85,10 +83,15 @@ class MainWindow(QMainWindow):
self.EditQuery = QLineEdit() self.EditQuery = QLineEdit()
self.EditQuery.returnPressed.connect(self.queryEditLine) self.EditQuery.returnPressed.connect(self.queryEditLine)
self.model = QSqlTableModel(self) editables = {0 : ("UPDATE jobs SET star = '{}' WHERE hash = '{}'",7)}
self.model.setTable("jobs") self.model = SqlQueryModel_editable(editables)
self.model.setEditStrategy(QSqlTableModel.OnFieldChange) #self.model.setTable("jobs")
self.model.select() #self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
#self.model.select()
#self.proxymodel2 = QSortFilterProxyModel(self)
#self.proxymodel2.setSourceModel(self.model)
#self.proxymodel2.setTable("jobs")
self.view = QTableView() self.view = QTableView()
self.view.setModel(self.model) self.view.setModel(self.model)
@ -216,14 +219,15 @@ class MainWindow(QMainWindow):
def customSQL(self,cmd): def customSQL(self,cmd):
print("Run SQL Query",cmd) print("Run SQL Query",cmd)
self.model.setTable("") #self.model.setTable("")
self.model.setQuery(cmd +" ;") self.model.setQuery(cmd +" ;")
#self.model.setTable("jobs") #self.model.setTable("jobs")
#self.proxymodel2 = QSortFilterProxyModel(self) self.proxymodel2 = QSortFilterProxyModel(self)
#self.proxymodel2.setSourceModel(self.model) self.proxymodel2.setSourceModel(self.model)
self.view.setModel(self.model) self.view.setModel(self.proxymodel2)
self.setProxyViewSettings() self.setProxyViewSettings()
self.view.show()
class PWPrompt(QDialog): class PWPrompt(QDialog):
def __init__(self): def __init__(self):
super().__init__() super().__init__()

View File

@ -1,5 +1,6 @@
#modifie the QSqlQueryModel to be editable and sets them to the database #modifie the QSqlQueryModel to be editable and sets them to the database
from PySide6.QtSql import QSqlQueryModel, QSqlQuery
from PySide6.QtCore import Qt
#credits to : #credits to :
#https://stackoverflow.com/questions/49752388/editable-qtableview-of-complex-sql-query #https://stackoverflow.com/questions/49752388/editable-qtableview-of-complex-sql-query
@ -32,6 +33,9 @@ class SqlQueryModel_editable(QSqlQueryModel):
result = q.exec_() result = q.exec_()
if result: if result:
self.query().exec_() self.query().exec_()
# print("filter_value:",filter_value)
print("setdata query: ",query.format(value, filter_value))
print("index.row:",index.row(), "filter_col:",filter_col)
else: else:
print(self.query().lastError().text()) print(self.query().lastError().text())
return result return result