Why cant i use QFileSystem, treeView inside mainWindow?



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

    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
    

    @

    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));
    }
    

    @



  • Your code is working without any error. Try to make a complete rebuild of your app (if build files are in separate directory just remove this directory. Otherwise remove .o and moc_*.cpp files).



  • ok thank u


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.