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. [SOLVED] QTreeView Basic understanding
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTreeView Basic understanding

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 2.6k 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.
  • H Offline
    H Offline
    hexenbrennen
    wrote on last edited by
    #1

    Hi

    I can't understand the QtreeView Model handling. Could someone please explain it with my example?

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QList<int> sample;
    sample<<10<<11<<12<<13;
    //11 is child of 10
    //13 is child of 12
    
    //build treeModel of QAbstractItemModel
    
    ui->treeView->setModel(tree);
    
    sample[0]=5;
    //change in TreeView??
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Did you look at QStandardItemModel and QStandardItem to build your tree model ? I'm sure you will good examples in Qt Assistant and google. Also what is the explanation you are expecting ? Any change in model will be reflected in View. I just made sample which is very trivial.

      @QTreeView view;
      QStringListModel list1;
      QStringList list;
      list<<"10"<<"11"<<"12"<<"13";
      list[0]="100";
      list1.setStringList(list);

      view.setModel(&list1);
      
      view.show();@
      

      Here whenever you change the list1, it should get reflected in view. Hope this helps.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hexenbrennen
        wrote on last edited by
        #3

        i have various types of data(strings doubles and int) and i thought you need to specify the type in the model.

        when i change your example it doesnt work with integers!

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          U can't set integers directly. There is no integer model like stringlistmodel. U need to list model with integers.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hexenbrennen
            wrote on last edited by
            #5

            How this is exactly my question?

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

              Hi,

              By using your model's setData method

              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
              • H Offline
                H Offline
                hexenbrennen
                wrote on last edited by
                #7

                I dont know how to create the model this was my first question!

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  I suggest you read the ModelView Programming in Qt Assistant. It clearly describes about different models, views. Try to understand how you can create different models. Qt Demo/Examples in your installation also contain many examples. Simple google hit also gave me many links with example. Enjoy model view programming.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    Here is an official tutorial for model/view programming: http://qt-project.org/doc/qt-5/modelview.html

                    Here is a simple example that uses QStandardItemModel with QTreeView to visualize integers:
                    @
                    // Create model
                    auto model = new QStandardItemModel;

                    // Add data to model
                    QList<QStandardItem*> list;
                    for (int i = 0; i < 10; ++i)
                    {
                    auto item = new QStandardItem;
                    item->setData(i); // Set integer data
                    list << item;
                    }
                    model->appendRow(list);

                    // Create view to visualize the model
                    auto view = new QTreeView;
                    view->setModel(model);
                    @

                    Note that there are many different models and many different views you can choose from. If you only want a list, you should use QListView instead of QTreeView.

                    Good luck.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hexenbrennen
                      wrote on last edited by
                      #10

                      thank you this will help me

                      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