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.

Qt Code:
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtSql import *
  4.  
  5. class model(QSqlTableModel):
  6.  
  7. def __init__(self, db_file, mode):
  8. super().__init__()
  9. self.db = QSqlDatabase.addDatabase('QSQLITE')
  10. self.db.setDatabaseName(db_file)
  11. self.setEditStrategy(QSqlRelationalTableModel.OnFieldChange)
  12. self.setTable("VOCABULARY")
  13. self.select()
  14.  
  15. if mode == "create":
  16. self.create_db()
  17. elif mode == "load":
  18. self.load_db()
  19.  
  20.  
  21. def create_db(self):
  22.  
  23. sql_create_vocab = '''CREATE TABLE VOCABULARY
  24. ([generated_id] INTEGER PRIMARY KEY,
  25. [word] TEXT NOT NULL,
  26. [translation] TEXT NOT NULL,
  27. [pos] TEXT NOT NULL,
  28. [example_sentence] TEXT NOT NULL,
  29. [example_translation] TEXT NOT NULL,
  30. [description] TEXT NOT NULL,
  31. [related_words] TEXT NOT NULL,
  32. [related_image] BLOB NOT NULL)'''
  33.  
  34.  
  35.  
  36. query = QSqlQuery(self.db)
  37. query.prepare(sql_create_vocab)
  38. query.exec()
  39. self.setQuery(query)
  40.  
  41. return True
  42.  
  43.  
  44. def load_db(self):
  45.  
  46. if self.db.open():
  47. log.debug('MODEL: connect to SQL Server successfully')
  48. # return True
  49. else:
  50. log.error('MODEL: connection failed')
  51. # return False
  52.  
  53. qry = QSqlQuery(self.db)
  54. log.info('MODEL: Processing Query')
  55. qry.prepare('SELECT * FROM VOCABULARY')
  56. qry.exec()
  57. self.setQuery(qry)
  58.  
  59.  
  60. def save_data(self):
  61. print("BEFORE " + self.record(0).value("related_words"))
  62. try:
  63. self.setData(self.index(0, 7), "XXXXX")
  64. self.submitAll()
  65. print("SUCCESS")
  66. except:
  67. print("FAIL")
  68.  
  69. print("AFTER " + self.record(0).value("related_words"))
To copy to clipboard, switch view to plain text mode 

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