QTreeView
-
I want to write a simple programe using QTreeView,How can I start.Can anyone plz give an example..
-
Moved to general and desktop, as this one definitely does not need a C++ guru:-)
-
Actulaly I have this MainWindow.cpp file but this is not workin..
@#include <QTreeView>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
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 &)));
}
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);
}
@
but this is not working. Showing some error.. bt If u plz help me how to make this cpp file workable by providing sorresponding MainWindow.h and main.cpp file.. I'm just started to write program In Qt so I am not able to handle the system properly -
please mark up your code! Either put '@' around the code manually or use the rightmost button in the menu of the editor. Thanks!
-
yes I did it..now u can see
-
What errors you got during compilation ?
-
Actually I want corresponding main.cpp file and MainWindow.h file coz I'm not able to manupulate it .there are several error.
-
[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".
-
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 scopevoid MainWindow::selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/)
3.'tree view was not declared in this scope.' -
Do you have a header file MainWindow.h ?
-
my MainWindow.h file is following
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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();
}
@ -
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.
-
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? -
you need to include QStandardItemModel header file in you mainwindow.h
@#include <QStandardItemModel>@
-
Sorry it's not working..
-
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_OBJECTpublic:
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();
}@
-
Thank u so much boss I got the problem of my code
-
You are welcome, Kindly edit your first post and add[Solved] to the title.