Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQueryModel, trees and more
Qt 6.11 is out! See what's new in the release blog

QSqlQueryModel, trees and more

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k 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.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    I will examplify the problem using the following scenario:

    1. A database that contains:
      • manufacturer: id, description
      • model: id, manufacturerId, description
      • car: id, modelId, description

    So we have a tree-like structure in the db:

    @

    • manufacturer
      • model
        • car
          @

    My target is to make every path in the tree (here the paths are guaranteed to be three) to be listed in a QComboBox.
    So the final UI will be:

    @
    "Manufacturer:" QComboBox
    "Model:" QComboBox
    "Car:" QComboBox
    @

    So what I want is a tree view but displayed as a form with three rows each of which contains a combo box that contains path from the tree.

    How I am currently trying to solve this:

    1. Subclassed QSqlQueryModel and set its query to:
      @
      "SELECT "
      "manufacturers.id, "
      "manufacturers.description, "
      "models.id, "
      "models.description, "
      "cars.id, "
      "cars.description "
      "FROM "
      "cars "
      "INNER JOIN models "
      "ON cars.modelId = models.id "
      "INNER JOIN manufacturers "
      "ON models.manufacturerId = manufacturers.id "
      "ORDER BY "
      "manufacturers.description, "
      "models.description, "
      "cars.description";
      @

    In the model I override the data() member function and the columnCount() one, so that I have three model columns:
    Manufacturers
    Models
    Cars

    and for each column I have two roles:
    -> Qt::DisplayRole - returns the description,
    -> RoleId = Qt::UserRole + 1 - the id of the given entity

    So, at this point I have a tabular model of my tree-like data.

    1. Convert the QSqlQueryModel subclass to tree model:
      "This thread":http://www.qtcentre.org/threads/23698-how-to-display-QSqlQueryModel-in-QTreeView recommends using QAbstractProxyModel and implement the:
      -> mapToSource
      -> mapFromSource
      member functions.

    2. Make each QComboBox use the same instance of the tree model, but making it use different column from it.
      @
      TreeModel* const treeModel = new TreeModel( this ); // the QAbstractProxyModel subclass

    manufacturerComboBox.setModel( treeModel );
    manufacturerComboBox.setModelColumn( 0 );

    modelComboBox.setModel( treeModel );
    modelComboBox.setModelColumn( 1 );

    carComboBox.setModel( treeModel );
    carComboBox.setModelColumn( 2 );
    @

    I am also thinking of using the QDataWidgetMapper class if using a given column for a tree-view doesn't work.

    So my question is: is this the right direction e.g. create a QAbstractProxyModel, and if so how should I implement the mapToSource and mapFromSource? Do I need to implement the parent() member function?

    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