fschober
21st March 2020, 04:50
Dear fellow Qtarians!
I am fairly new to the world of Qt/PyQt after, as a reasonable tkinter programmer, try to reach the next level of beautiful gui design. So far it is fantastic.
But since a few days I can't seem to wrap my head around qt's own model structure. The documentation for pyQt gets very fast very bad if you want to do more advanced stuff.
I am writing a program which uses sqlite3. So I read about these fantastic Sql Models. I tried it with QSqlTableModel (the others too, but for trying this will do),
but it won't work and the examples I found just confused me more than anything.
I want to display the vocabulary of a language in a treeview.
So here I have a little example for you, what I have so far.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
class model(QSqlTableModel):
def __init__(self, db_file, mode):
super().__init__()
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName(db_file)
self.setEditStrategy(QSqlRelationalTableModel.OnFi eldChange)
self.setTable("VOCABULARY")
self.select()
if mode == "create":
self.create_db()
elif mode == "load":
self.load_db()
def create_db(self):
sql_create_vocab = '''CREATE TABLE VOCABULARY
([generated_id] INTEGER PRIMARY KEY,
[word] TEXT NOT NULL,
[translation] TEXT NOT NULL,
[pos] TEXT NOT NULL,
[example_sentence] TEXT NOT NULL,
[example_translation] TEXT NOT NULL,
[description] TEXT NOT NULL,
[related_words] TEXT NOT NULL,
[related_image] BLOB NOT NULL)'''
query = QSqlQuery(self.db)
query.prepare(sql_create_vocab)
query.exec()
self.setQuery(query)
return True
def load_db(self):
if self.db.open():
log.debug('MODEL: connect to SQL Server successfully')
# return True
else:
log.error('MODEL: connection failed')
# return False
qry = QSqlQuery(self.db)
log.info('MODEL: Processing Query')
qry.prepare('SELECT * FROM VOCABULARY')
qry.exec()
self.setQuery(qry)
def save_data(self):
print("BEFORE " + self.record(0).value("related_words"))
try:
self.setData(self.index(0, 7), "XXXXX")
self.submitAll()
print("SUCCESS")
except:
print("FAIL")
print("AFTER " + self.record(0).value("related_words"))
Right here the last method is to check if it writes "XXXXX" to the db, but it won't.
And then I set the model for the treeview, but the treeview also refuses to display something, unless I query with "SELECT * FROM VOCABULARY". I know the code here won't work, but what I don't understand is,
if I have to query the database myself, or If I can use the high level model funtions - and if so, how does this work!
Any help would be greatly appreciated.
Cheers
Fabian
I am fairly new to the world of Qt/PyQt after, as a reasonable tkinter programmer, try to reach the next level of beautiful gui design. So far it is fantastic.
But since a few days I can't seem to wrap my head around qt's own model structure. The documentation for pyQt gets very fast very bad if you want to do more advanced stuff.
I am writing a program which uses sqlite3. So I read about these fantastic Sql Models. I tried it with QSqlTableModel (the others too, but for trying this will do),
but it won't work and the examples I found just confused me more than anything.
I want to display the vocabulary of a language in a treeview.
So here I have a little example for you, what I have so far.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
class model(QSqlTableModel):
def __init__(self, db_file, mode):
super().__init__()
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName(db_file)
self.setEditStrategy(QSqlRelationalTableModel.OnFi eldChange)
self.setTable("VOCABULARY")
self.select()
if mode == "create":
self.create_db()
elif mode == "load":
self.load_db()
def create_db(self):
sql_create_vocab = '''CREATE TABLE VOCABULARY
([generated_id] INTEGER PRIMARY KEY,
[word] TEXT NOT NULL,
[translation] TEXT NOT NULL,
[pos] TEXT NOT NULL,
[example_sentence] TEXT NOT NULL,
[example_translation] TEXT NOT NULL,
[description] TEXT NOT NULL,
[related_words] TEXT NOT NULL,
[related_image] BLOB NOT NULL)'''
query = QSqlQuery(self.db)
query.prepare(sql_create_vocab)
query.exec()
self.setQuery(query)
return True
def load_db(self):
if self.db.open():
log.debug('MODEL: connect to SQL Server successfully')
# return True
else:
log.error('MODEL: connection failed')
# return False
qry = QSqlQuery(self.db)
log.info('MODEL: Processing Query')
qry.prepare('SELECT * FROM VOCABULARY')
qry.exec()
self.setQuery(qry)
def save_data(self):
print("BEFORE " + self.record(0).value("related_words"))
try:
self.setData(self.index(0, 7), "XXXXX")
self.submitAll()
print("SUCCESS")
except:
print("FAIL")
print("AFTER " + self.record(0).value("related_words"))
Right here the last method is to check if it writes "XXXXX" to the db, but it won't.
And then I set the model for the treeview, but the treeview also refuses to display something, unless I query with "SELECT * FROM VOCABULARY". I know the code here won't work, but what I don't understand is,
if I have to query the database myself, or If I can use the high level model funtions - and if so, how does this work!
Any help would be greatly appreciated.
Cheers
Fabian