Need help creating a model view with a TreeView and an (QSqlRelationalTableModel?) ?
-
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! -
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!@Dizarc I haven't used
QSqlRelationalTableModelbut from the documentation it does not look like it can be used directly with aTreeView. It inherits fromQAbstractTableModeland the documentation for that states:Since the model provides a more specialized interface than
QAbstractItemModel, it is not suitable for use with tree viewsYou would need to find some way of adapting it to a
QAbstractItemModelsub-class that is suitable for use as aTreeViewmodel. 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. -
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.