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;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel;
};
#endif // MAINWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
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>
<?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>
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtCore>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
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));
}
#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));
}
To copy to clipboard, switch view to plain text mode
Please tell me whats the wrong here..?
Bookmarks