Results 1 to 6 of 6

Thread: QListView and QDir

  1. #1
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QListView and QDir

    Hi guys,

    I am trying to make a filebrowser application with the help of QListView and QDir classes of Qt2 designer with Qtopia 2.2.0. I have drawn a QListView widget named MyListView on UI.

    I declared a QDir dir globally;

    i wrote my progrm as follows...

    #include "browser.h" //my header file
    #include <qdir.h>
    #include <qlistview.h>
    #include <qstringlist.h>

    QDir dir=QDir::rootDirPath();

    Browser::Browser(QWidget *parent,const char *name, WFlags fl):
    BrowserForm(parent,name,fl)
    {
    QListViewItem *rootItem=new QListViewItem(MyListView);
    rootItem->setText(0,QDir::rootDirPath());
    MyListView->insertItem(rootItem);

    connect(MyListView,SIGNAL(doubleClicked(QListViewI tem*),this,SLOT(setDir(QListViewItem*)));
    connect(upButton,SIGNAL(clicked()),this,SLOT(upPat h());
    }

    Browser::~Browser()
    {
    }

    void Browser::setDir(QListViewItem* newItem)
    {
    MyListView->clear();
    QStringList dirList(dir.entryList());
    for(QStringList::Iterator it=dirList.begin();it!=dirList.end();++it)
    {
    QListViewItem *dirItem=new QListViewItem(MyListView);
    dirItem->setText(0,(*it));
    MyListView->insertItem(dirItem);
    }

    }

    void Browser::upPath()
    {
    MyListView->clear();
    QListViewItem *upitem=new QListViewItem(MyListView);
    upItem->setText(0,dir.path());
    MyListView->insertItem(upItem);
    }

    This code compiles fine. when i clicke the root folder set in the listview i get to see the subfolders and when i press the up button i again see the rootfolder. This works fine for the root and its subsequent folders. But i am unable to understand what i have to do if i have to click one of the subfolders randomly and see the subfolders of the subfolder selected. Also how to set the path for up function if i have to come back from the subfolders level by level....

    The Qtopia 2.2.0 doesn't have much API to manipulate random dir path. What to do???

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView and QDir

    What is the restriction for using Qt2?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListView and QDir

    I have a friendlyARM device MINI2440 which gives QT2 designer and qtopia2.2.0. It compiles Qt applications for the device...so i have no choice but to use that only!!!!:

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView and QDir

    Try this (I didn't test it!):

    Qt Code:
    1. void Browser::setDir(QListViewItem* newItem)
    2. {
    3. MyListView->clear();
    4. dir = QDir(newItem->text());
    5. QStringList dirList(dir.entryList());
    6. for(QStringList::Iterator it=dirList.begin();it!=dirList.end();++it)
    7. {
    8. QListViewItem *dirItem=new QListViewItem(MyListView);
    9. dirItem->setText(0,(*it));
    10. MyListView->insertItem(dirItem);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListView and QDir

    Thanks.....It worked a bit....but it doesn't traverse the level of subfolder of subfoldrs...I am trying to use stack, to push path and pop the path when up button is pressed. Lets see...i'll keep posting when i get something new...in this....

  6. #6
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListView and QDir

    This is the code which i have written according to my understanding!!!. I am unable to understand why the runtime errors are coming as the code gets compiled fine. Though its not at all complete coz functionality for files is yet to be written...my problem is persisting in case of directories only....for example my code can traverse "bin" folder but not "dev" folder...It can traverse one level and come back to root when Up Button is pressed..its a run time error which i am unable to understand...plz guide...

    This code is written using Qt2 designer/qtopia2.2.0.....

    #include "filebrowser.h"
    #include <qlistview.h>
    #include <qdir.h>
    #include <qfileinfo.h>
    #include <qlabel.h>
    #include <qpushbutton.h>

    #include <stdio.h>

    QDir dir=QDir::rootDirPath();

    int i=0;
    QString pathStack[100];

    FileBrowser::FileBrowser(QWidget *parent, const char *name, WFlags fl):
    FileBrowserForm(parent,name,fl)
    {
    QPixmap fp("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/FC.png");
    QPixmap file("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/File.png");

    push(dir.absPath());
    TextLabel->setText(dir.dirName());
    if(dir.exists())
    {
    QStringList dirList(dir.entryList());
    MyListView->clear();

    for(QStringList::Iterator it=dirList.begin();it!=dirList.end();++it)
    {
    QFileInfo d(dir,(const char*)(*it));
    QListViewItem *dirItem=new QListViewItem(MyListView);
    dirItem->setText(0,(const char*)(*it));
    if(d.isDir())
    {
    dirItem->setPixmap(0,fp);
    }
    else
    {
    dirItem->setPixmap(0,file);
    }
    MyListView->insertItem(dirItem);
    printf("\n%s",(const char*)(*it));

    }
    }
    else
    printf("Directroy does not exist");


    connect(MyListView,SIGNAL(doubleClicked(QListViewI tem*)),this,SLOT(set_dir(QListViewItem*)));
    connect(UpButton,SIGNAL(clicked()),this,SLOT(up_pa th()));

    }

    FileBrowser::~FileBrowser()
    {
    }

    void FileBrowser::set_dir(QListViewItem* nItem)
    {
    QPixmap fp("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/FC.png");
    QPixmap file("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/File.png");

    dir.cd(nItem->text(0));
    dir.convertToAbs();
    TextLabel->setText(dir.dirName());

    push(dir.absPath());

    QStringList dirList(dir.entryList());
    MyListView->clear();

    for(QStringList::Iterator it=dirList.begin();it!=dirList.end();++it)
    {
    QFileInfo fi(dir,(const char*)(*it));
    QListViewItem *dirItem=new QListViewItem(MyListView);
    dirItem->setText(0,(const char*)(*it));
    if(fi.isDir())
    {
    dirItem->setPixmap(0,fp);
    }
    else
    {
    dirItem->setPixmap(0,file);
    }
    MyListView->insertItem(dirItem);
    printf("\n%s",(const char*)(*it));
    }

    }

    void FileBrowser:: up_path()
    {
    QPixmap fp("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/FC.png");
    QPixmap file("/opt/FriendlyARM/mini2440/x86-qtopia/Browser1/File.png");

    dir.cdUp();
    dir.convertToAbs();
    TextLabel->setText(dir.dirName());
    QStringList dirList(dir.entryList());
    MyListView->clear();

    for(QStringList::Iterator it=dirList.begin();it!=dirList.end();++it)
    {
    QListViewItem *dirItem=new QListViewItem(MyListView);
    dirItem->setText(0,(const char*)(*it));
    dirItem->setPixmap(0,fp);
    MyListView->insertItem(dirItem);
    }
    }

    void FileBrowser:: push(QString path)
    {

    pathStack[i++]=path;

    }

    QString FileBrowser:: pop()
    {
    if(i>=0)
    {
    QString returnPath=pathStack[--i];
    return returnPath;
    }
    else
    {
    return("No such directory");
    }
    }

Similar Threads

  1. QDir::mkpath
    By drescherjm in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2009, 04:35
  2. Help with QDir model
    By allstar in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2007, 15:31
  3. QDir Subclassing
    By Oleg in forum Qt Programming
    Replies: 9
    Last Post: 31st October 2007, 12:03
  4. QDir and QFile
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2007, 13:43
  5. Is it me or QDir?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:06

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
  •  
Qt is a trademark of The Qt Company.