Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why cant i use QFileSystem, treeView inside mainWindow?
Forum Updated to NodeBB v4.3 + New Features

Why cant i use QFileSystem, treeView inside mainWindow?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

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

    @

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aurora
        wrote on last edited by
        #3

        ok thank u

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved