QTreeView
-
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.