Results 1 to 5 of 5

Thread: Why cant i use QFileSystem, treeView inside mainWindow?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default 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...
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtGui>
    6. #include <QtCore>
    7.  
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18.  
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22. private slots:
    23.  
    24. void on_treeView_clicked(const QModelIndex &index);
    25.  
    26. private:
    27.  
    28. Ui::MainWindow *ui;
    29. QFileSystemModel *dirmodel;
    30. QFileSystemModel *filemodel;
    31.  
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    mainwindow.ui
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>621</width>
    10. <height>591</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <widget class="QTreeView" name="treeView">
    18. <property name="geometry">
    19. <rect>
    20. <x>30</x>
    21. <y>150</y>
    22. <width>311</width>
    23. <height>331</height>
    24. </rect>
    25. </property>
    26. </widget>
    27. <widget class="QListView" name="listView">
    28. <property name="geometry">
    29. <rect>
    30. <x>365</x>
    31. <y>160</y>
    32. <width>231</width>
    33. <height>361</height>
    34. </rect>
    35. </property>
    36. </widget>
    37. </widget>
    38. <widget class="QMenuBar" name="menuBar">
    39. <property name="geometry">
    40. <rect>
    41. <x>0</x>
    42. <y>0</y>
    43. <width>621</width>
    44. <height>21</height>
    45. </rect>
    46. </property>
    47. </widget>
    48. <widget class="QToolBar" name="mainToolBar">
    49. <attribute name="toolBarArea">
    50. <enum>TopToolBarArea</enum>
    51. </attribute>
    52. <attribute name="toolBarBreak">
    53. <bool>false</bool>
    54. </attribute>
    55. </widget>
    56. <widget class="QStatusBar" name="statusBar"/>
    57. </widget>
    58. <layoutdefault spacing="6" margin="11"/>
    59. <resources/>
    60. <connections/>
    61. </ui>
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QtCore>
    5.  
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. QString sPath ="C:/";
    14.  
    15. dirmodel =new QFileSystemModel(this);
    16. dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    17. dirmodel->setRootPath(sPath);
    18. ui->treeView->setModel(dirmodel);
    19.  
    20.  
    21. filemodel =new QFileSystemModel(this);
    22. filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
    23. filemodel->setRootPath(sPath);
    24. ui->listView->setModel(filemodel);
    25.  
    26. }
    27.  
    28. MainWindow::~MainWindow()
    29. {
    30. delete ui;
    31. }
    32.  
    33. void MainWindow::on_treeView_clicked(const QModelIndex &index)
    34. {
    35. QString sPath=dirmodel->fileInfo(index).absoluteFilePath();
    36. ui->listView->setRootIndex(filemodel->setRootPath(sPath));
    37. }
    To copy to clipboard, switch view to plain text mode 


    Please tell me whats the wrong here..?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why cant i use QFileSystem, treeView inside mainWindow?

    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.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why cant i use QFileSystem, treeView inside mainWindow?

    I posted all the all the code here.....
    and main.cpp as shown below...

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 



    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?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why cant i use QFileSystem, treeView inside mainWindow?

    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.

  5. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why cant i use QFileSystem, treeView inside mainWindow?

    ok thank u Crish...
    Last edited by aurora; 10th February 2012 at 08:29.

Similar Threads

  1. Replies: 7
    Last Post: 25th July 2016, 14:42
  2. Replies: 1
    Last Post: 15th November 2011, 23:10
  3. Replies: 0
    Last Post: 6th November 2011, 09:22
  4. QFileSystem include problem
    By daujeroti in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 10:10
  5. Replies: 6
    Last Post: 20th July 2009, 16:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.