Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Need help creating a model view with a TreeView and an (QSqlRelationalTableModel?) ?

Need help creating a model view with a TreeView and an (QSqlRelationalTableModel?) ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 432 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dizarc
    wrote on last edited by
    #1

    Hello, I am trying to create a QML Clothing application that uses an SQL database. What I am trying to do is have different clothing types like pants or shirts that when clicked open up each clothing item that is associated with that type. An example is when pants is clicked it expands and shows up Jeans.
    The two tables in SQL:

    CREATE TABLE ClothesTypes(typeId INTEGER PRIMARY KEY AUTOINCREMENT, typeName TEXT NOT NULL);
    
    CREATE TABLE Clothes(clothingId INTEGER PRIMARY KEY AUTOINCREMENT, clothingName TEXT NOT NULL, typeId INTEGER, 
    FOREIGN KEY (typeId) REFERENCES ClothesTypes(typeId));
    

    For that reason I wanted to use a TreeView. But I cant seem to grasp how to integrate it using SQL. First I thought of using QSqlRelationalTableModel but it seems like its only use is if i want the Clothes table to show first so the foreign key actually has the name of the type and not the id like so:

    setTable("Clothes");
    setRelation(2, QSqlRelation("ClothesTypes", "typeId", "typeName"));
    

    Since its of no use to me I was thinking of using DelegateChooser with a TableView so when i click at a type it opens up another delegate. But that seems like a roundabout way of doing it.
    Is there something else I can use to achieve this?
    Thanks!

    B 1 Reply Last reply
    0
    • D Dizarc

      Hello, I am trying to create a QML Clothing application that uses an SQL database. What I am trying to do is have different clothing types like pants or shirts that when clicked open up each clothing item that is associated with that type. An example is when pants is clicked it expands and shows up Jeans.
      The two tables in SQL:

      CREATE TABLE ClothesTypes(typeId INTEGER PRIMARY KEY AUTOINCREMENT, typeName TEXT NOT NULL);
      
      CREATE TABLE Clothes(clothingId INTEGER PRIMARY KEY AUTOINCREMENT, clothingName TEXT NOT NULL, typeId INTEGER, 
      FOREIGN KEY (typeId) REFERENCES ClothesTypes(typeId));
      

      For that reason I wanted to use a TreeView. But I cant seem to grasp how to integrate it using SQL. First I thought of using QSqlRelationalTableModel but it seems like its only use is if i want the Clothes table to show first so the foreign key actually has the name of the type and not the id like so:

      setTable("Clothes");
      setRelation(2, QSqlRelation("ClothesTypes", "typeId", "typeName"));
      

      Since its of no use to me I was thinking of using DelegateChooser with a TableView so when i click at a type it opens up another delegate. But that seems like a roundabout way of doing it.
      Is there something else I can use to achieve this?
      Thanks!

      B Offline
      B Offline
      Bob64
      wrote on last edited by Bob64
      #2

      @Dizarc I haven't used QSqlRelationalTableModel but from the documentation it does not look like it can be used directly with a TreeView. It inherits from QAbstractTableModel and the documentation for that states:

      Since the model provides a more specialized interface than QAbstractItemModel, it is not suitable for use with tree views

      You would need to find some way of adapting it to a QAbstractItemModel sub-class that is suitable for use as a TreeView model. Conceptually, this seems like it should be possible if you have already identified a tree-like sub-structure that you want to expose from your data.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What you can do is write a proxy model that will sit on top of your QSqlTableModel and re-arrange things to be suitable for a QTreeView.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved