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. Presenting nested database data in TreeView
QtWS25 Last Chance

Presenting nested database data in TreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeviewqtsql
3 Posts 2 Posters 1.9k Views
  • 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.
  • 8 Offline
    8 Offline
    8105
    wrote on 23 Mar 2017, 19:20 last edited by
    #1

    Good evening everyone.

    I would like to get some suggestions on my little situation.

    Here's the portion of the relational database that I'm reading from (and eventually writing to):

    floors:
    number: varchar(5)
    capacity: varchar(5)

    rooms:
    roomNumber: varchar(5)
    #floorNumber: varchar(5)
    beds: varchar(3)

    My aim is to present this data in a tree like layout. The first level presents the floor number and the number of rooms on that floor. The second level contains the rooms (under every floor tree node we present rooms' properties).

    I was able to present the first level using a TreeView, and also able to put rooms in TreeView's grouped by floors but I failed to figure how to insert those individual TreeView's under the correct main TreeView node.

    Here's a screenshot of my current incomplete results:
    alt text

    Here's the piece of code that allowed me to do this:

    //"Hotel" is the QSqlDatabase identifier
        QSqlQueryModel* floorsModel = new QSqlQueryModel;
        QSqlQuery floorsQuery(Hotel);
        QString q = "select floors.number, count(rooms.floornumber) from floors, rooms where floors.number=rooms.floornumber group by floors.number, rooms.floornumber;";
        floorsQuery.prepare(q);
        floorsQuery.exec();
        floorsModel->setQuery(floorsQuery);
        ui->floorsAndRooms->setModel(floorsModel);
    //"floorsAndRooms" is the designer QTreeView
        floorsModel->setHeaderData(0, Qt::Horizontal, "Floor #");
        floorsModel->setHeaderData(1, Qt::Horizontal, "Rooms");
    
        int nFloors = floorsModel->rowCount();
        QString floorNumber;
        for(int i = 0; i != nFloors; i++)
        {
             floorNumber = floorsModel->record(i).value("number").toString();
             QSqlQuery roomsQuery(Hotel);
             roomsQuery.prepare("select * from rooms where floorNumber=:fNumber");
             roomsQuery.bindValue(":fNumber",floorNumber);
             roomsQuery.exec();
             QSqlQueryModel* roomsModel = new QSqlQueryModel;
             roomsModel->setQuery(roomsQuery);
             QTreeView* rooms = new QTreeView;
             rooms->setModel(roomsModel);
             rooms->show();
        }
    

    I hope you understand my aim and welcome any radical changes I may need to implement to better present the needed data.

    Thank you for your time.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 23 Mar 2017, 20:05 last edited by
      #2

      would you be ok with something like this (imagine a tree with floor 1 branch open :

      Floor# Rooms Room Number Beds
      1 5
      0 2
      1 4
      2 1
      3 2
      4 4
      2 7
      3 5
      4 3

      or do you need the actual TableView with separate headers to appear when you expand the branch? (spoiler alert the first is very easy, the second relatively more complicated)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • 8 Offline
        8 Offline
        8105
        wrote on 23 Mar 2017, 20:11 last edited by
        #3

        Thank you for your suggestion. I think it's what I'll eventually settle for, fairly easy to indeed, using just one SQL query.
        Although, I'm starting to think ... wouldn't my original aim be easier to fulfil using TreeWidget and not TreeView?

        1 Reply Last reply
        0

        3/3

        23 Mar 2017, 20:11

        • Login

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