Results 1 to 12 of 12

Thread: PyQT: The QSqlTableModel and how to wrap my head around it

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2020
    Location
    Graz, Austria
    Posts
    6
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    Alright, for understanding the principle I stripped my model class to this:

    Qt Code:
    1. class model(QSqlTableModel):
    2.  
    3. def __init__(self, db_file, mode):
    4. super().__init__()
    5. self.db = QSqlDatabase.addDatabase('QSQLITE')
    6. print(db_file)
    7. self.db.setDatabaseName("start.db")
    8. self.setEditStrategy(QSqlRelationalTableModel.OnFieldChange)
    9. self.setTable("VOCABULARY")
    10. self.select()
    11.  
    12. 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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    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.

  3. #3
    Join Date
    Mar 2020
    Location
    Graz, Austria
    Posts
    6
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    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:
    1. def start_mode(self):
    2. self.start_vocab = model("data/start.db", "load")
    3. 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!?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    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.

  5. #5
    Join Date
    Mar 2020
    Location
    Graz, Austria
    Posts
    6
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    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!

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQT: The QSqlTableModel and how to wrap my head around it

    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.

Similar Threads

  1. Build Qt-based app on Mac OS X, head file not found
    By leonardyu in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2015, 14:01
  2. Replies: 2
    Last Post: 9th April 2013, 06:26
  3. How to display picture from raw data without head?
    By luochen601 in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2010, 10:30
  4. QNetworkAccessManager::head not working
    By jonks in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 22:23
  5. Replies: 1
    Last Post: 15th March 2009, 20:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.