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. QTreeView
Qt 6.11 is out! See what's new in the release blog

QTreeView

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 9.0k 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.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #8

    [quote author="MKSPulok" date="1338363879"]Actually I want corresponding main.cpp file and MainWindow.h file coz I'm not able to manupulate it .there are several error.[/quote]

    I didnt get that , For your MainWindow.cpp you must be having a header file MainWindow.h where you need to provide the declarations of your class (data and functions) and in the MainWindow.cpp file you write the implementation. Also you main.cpp will create an instance of MainWindow and call its show() function to display.

    So you need to provide what errors you are getting. Also you must declare your member data and functions in "MainWindow.h".

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MKSPulok
      wrote on last edited by
      #9

      I'm getting the following error..

      in the mai void MainWindow::selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/)
      1.'treeVieew' was not declared in this scope.
      2.'standaredModel' was not declared in this scope

      void MainWindow::selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/)
      3.'tree view was not declared in this scope.'

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #10

        Do you have a header file MainWindow.h ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MKSPulok
          wrote on last edited by
          #11

          my MainWindow.h file is following

          @#ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>

          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = 0);
          void selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/);
          ~MainWindow();

          private:
          Ui::MainWindow *ui;
          };

          #endif // MAINWINDOW_H
          @

          And My main.cpp file is following

          @#include <QtGui/QApplication>
          #include <QTreeView>
          #include "MainWindow.h"

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();

          return a.exec&#40;&#41;;
          

          }
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #12

            okay thats good,

            You need to add the following to the private: section of your mainwindow.h

            @private:
            QTreeView *treeView;
            QStandardItemModel *standardModel;@

            Also selectionChangedSlot(....) should be declared under private slots: eg

            @private slots:
            void selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/);@

            Add these things and check.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MKSPulok
              wrote on last edited by
              #13

              now showing the following error..
              1.ISO C++ forbid deceleration of QStandardItemModel with no type
              2.expected ';' before '*' tocken for the same line.. now what to do?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #14

                you need to include QStandardItemModel header file in you mainwindow.h

                @#include <QStandardItemModel>@

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MKSPulok
                  wrote on last edited by
                  #15

                  Sorry it's not working..

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on last edited by
                    #16

                    Ok i got the code working.

                    here is the code.

                    mainwindow.h

                    @#ifndef MAINWINDOW_H
                    #define MAINWINDOW_H

                    #include <QMainWindow>
                    #include <QStandardItemModel>
                    #include <QTreeView>
                    #include <QObject>
                    #include <QItemSelection>

                    namespace Ui {
                    class MainWindow;
                    }

                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();

                    private slots:
                    void selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/);

                    private:
                    Ui::MainWindow *ui;
                    QStandardItemModel *standardModel;
                    QTreeView *treeView;
                    };

                    #endif // MAINWINDOW_H@

                    mainwindow.cpp

                    @#include "MainWindow.h"
                    #include "ui_MainWindow.h"

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow) /// <----This is missing in your code
                    {
                    ui->setupUi(this); // <-- you need to add this
                    treeView = new QTreeView(this);
                    setCentralWidget(treeView);
                    standardModel = new QStandardItemModel ;
                    QStandardItem *rootNode = standardModel->invisibleRootItem();

                    //defining a couple of items
                    QStandardItem *americaItem = new QStandardItem("America");
                    QStandardItem *mexicoItem =  new QStandardItem("Canada");
                    QStandardItem *usaItem =     new QStandardItem("USA");
                    QStandardItem *bostonItem =  new QStandardItem("Boston");
                    QStandardItem *europeItem =  new QStandardItem("Europe");
                    QStandardItem *italyItem =   new QStandardItem("Italy");
                    QStandardItem *romeItem =    new QStandardItem("Rome");
                    QStandardItem *veronaItem =  new QStandardItem("Verona");
                    
                    //building up the hierarchy
                    rootNode->    appendRow(americaItem);
                    rootNode->    appendRow(europeItem);
                    americaItem-> appendRow(mexicoItem);
                    americaItem-> appendRow(usaItem);
                    usaItem->     appendRow(bostonItem);
                    europeItem->  appendRow(italyItem);
                    italyItem->   appendRow(romeItem);
                    italyItem->   appendRow(veronaItem);
                    
                    //register the model
                    treeView->setModel(standardModel);
                    treeView->expandAll();
                    
                    //selection changes shall trigger a slot
                    
                    QItemSelectionModel *selectionModel= treeView->selectionModel();
                    connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
                    

                    }

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

                    void MainWindow::selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/)
                    {
                    //get the text of the selected item
                    const QModelIndex index = treeView->selectionModel()->currentIndex();
                    QString selectedText = index.data(Qt::DisplayRole).toString();

                    //find out the hierarchy level of the selected item
                    int hierarchyLevel=1;
                    QModelIndex seekRoot = index;
                    
                    while(seekRoot.parent() != QModelIndex())
                    {
                        seekRoot = seekRoot.parent();
                        hierarchyLevel++;
                    }
                    
                    QString showString = QString("%1, Level %2").arg(selectedText).arg(hierarchyLevel);
                    setWindowTitle(showString);
                    

                    }@

                    main.cpp

                    @#include <QtGui/QApplication>
                    #include "MainWindow.h"

                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    MainWindow w;
                    w.show();

                    return a.exec(&#41;;
                    

                    }@

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MKSPulok
                      wrote on last edited by
                      #17

                      Thank u so much boss I got the problem of my code

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sam
                        wrote on last edited by
                        #18

                        You are welcome, Kindly edit your first post and add[Solved] to the title.

                        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