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

View File

@ -1,5 +1,6 @@
#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 :
#https://stackoverflow.com/questions/49752388/editable-qtableview-of-complex-sql-query
@ -32,6 +33,9 @@ class SqlQueryModel_editable(QSqlQueryModel):
result = q.exec_()
if result:
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:
print(self.query().lastError().text())
return result