First I don't think you need to set an executed query to the model - since you're working on a complete table setTable() should be enough.
Then you have added a lot of constraints (NOT NULL) so only setting related_words will not work.
First I don't think you need to set an executed query to the model - since you're working on a complete table setTable() should be enough.
Then you have added a lot of constraints (NOT NULL) so only setting related_words will not work.
Alright, for understanding the principle I stripped my model class to this:
Qt Code:
def __init__(self, db_file, mode): super().__init__() print(db_file) self.db.setDatabaseName("start.db") self.setTable("VOCABULARY") self.select() print(self.rowCount())To copy to clipboard, switch view to plain text mode
I also removed all the constraints from the database.
But The Row Count is showing 0, and the treeview is still empty. There should be exactly 1 row in the table.
The problem might be that you are trying to use a hierarchical view (a tree) to display a non-hierarchical table. In principle, a tree view should display one top-level entry for each row in the table, but that's it. There's no drill-down, because a table model's rows by definition have no children.
Also, did you set the model on your view? (QAbstractItemView::setModel() in C++)
You might also want to add some code to check to see if your database is actually open. Your code assumes the DB file is in your current working directory, but what you think and what python thinks that location is might be two different places.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Ah, alright, thanks for the reply!
So firstly: at the beginning it actually worked to insert something in the treeview with model.setquery("SELECT * FROM VOCABULARY"). It inserted exactly what i wanted. But then it wasn't possible to do anything else. But through that (and from tkinter treeview) I know, that it only turns hirarchical if you want it to e.g. set parent items and give them child items.
Secondly, my controller does this to start the model:
Qt Code:
def start_mode(self): self.start_vocab = model("data/start.db", "load") self.main_win.vocab_tv.setModel(self.start_vocab)To copy to clipboard, switch view to plain text mode
(vocab_tv is the treeview)
And thirdly, I also checked the location of the file - tested it with the sqlite3 module simultaneously, if it works. Sqlite3 returns everything as plannend!
This is the craziest thing!
So here the logic broken down:
1.) Create and instantiate a model, inherited from QSqlTableModel.
2.) Add the Database QSQLITE (In this case)
3.) Set the DatabaseName: the database File
4.) Optional: Set EditStrategy
5.) Set the Table - which is the one it reads from the file
6.) Make it select()
7.) Create a Treeview or Listview
8.) Set the model for the Treeview or Listview
That should be it, right!?
I would move step 6 to the end. select() results in signals being issued by the model that the view uses to update itself. If you haven't connected the model to the view yet, no one is listening to the model's signals at the time you do the select().
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
So I called the select() method at the very last... moved it on different positions, but I think the very problem seems to lay with loading the data in the model, since the row count keeps being 0!
Don't know how else to help. Maybe use "DB Browser for SQLite" to make sure that your empty select actually does something there and then try to map that to python. I thought you would at least have to issue some kind of real SELECT statement - "SELECT * from MyTable" to get a populated result.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks