PDA

View Full Version : Why cant i use QFileSystem, treeView inside mainWindow?



aurora
10th February 2012, 04:28
I am using tree view and list view to display the contents of QFilesystem model...
When i used Dialog to display the Tree View and List view , it works properly....
But when i used QMainWindow instead of QDialog, i'm getting run time error saying
"The program has unexpectedly finished."

my code as below...

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QtCore>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:

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

private slots:

void on_treeView_clicked(const QModelIndex &index);

private:

Ui::MainWindow *ui;
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel;

};

#endif // MAINWINDOW_H



mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>621</width>
<height>591</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTreeView" name="treeView">
<property name="geometry">
<rect>
<x>30</x>
<y>150</y>
<width>311</width>
<height>331</height>
</rect>
</property>
</widget>
<widget class="QListView" name="listView">
<property name="geometry">
<rect>
<x>365</x>
<y>160</y>
<width>231</width>
<height>361</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>621</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtCore>


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

QString sPath ="C:/";

dirmodel =new QFileSystemModel(this);
dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirmodel->setRootPath(sPath);
ui->treeView->setModel(dirmodel);


filemodel =new QFileSystemModel(this);
filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
filemodel->setRootPath(sPath);
ui->listView->setModel(filemodel);

}

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

void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
QString sPath=dirmodel->fileInfo(index).absoluteFilePath();
ui->listView->setRootIndex(filemodel->setRootPath(sPath));
}



Please tell me whats the wrong here..?

ChrisW67
10th February 2012, 05:32
There's no error I can see in the code you posted (except that you are not using a layout in your UI, but that is not fatal). I, of course, cannot see the rest of your program.

Why don't you run the program in your debugger and look at the backtrace after it fails. That will tell you exactly where it crashed and neither of us will have to guess.

aurora
10th February 2012, 06:00
I posted all the all the code here.....
and main.cpp as shown below...



#include <QtGui/QApplication>
#include "mainwindow.h"

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

return a.exec();
}




when i declared
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel; inside the mainwindow.cpp file, inside a function it was working...but i want it to be in class defination itself as i'm using it in two diffrent function....
So wats this problem?

ChrisW67
10th February 2012, 06:52
The problem is that you are not trying to address the problem. Get your debugger out and get it to tell you exactly where it is dying. Your code compiles and runs just fine here.

aurora
10th February 2012, 08:25
ok thank u Crish...:)