Results 1 to 10 of 10

Thread: File browser

  1. #1
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1

    Default File browser

    I am using Qt4.1.4 under WinXp
    I made a little File browser for didactical reasons and to override certain extensions Windows Explorer runs by default.
    Everything works to the extent that I navigate directories and drives and launch programs the way I want but so far all attempts to incorporate a given typed content of LineEdit to the File browser have eluded me.
    Could anyone be so kind to illuminate me with some pointers?

    Qt Code:
    1. #include <QtGui>
    2.  
    3. //#include <QDebug>
    4.  
    5. class CListBox: public QListWidget {
    6. Q_OBJECT
    7. public:
    8. CListBox(QWidget *pParent);
    9. signals:
    10. void currentDir(const QString&);
    11. protected slots:
    12. void onActivation(QListWidgetItem *item);
    13. };
    14.  
    15. CListBox::CListBox(QWidget *pParent): QListWidget(pParent) {
    16. clear();
    17. QDir dir;
    18. QStringList list;
    19. QStringList arg = QCoreApplication::arguments();
    20. if (arg.size() == 2) dir.setCurrent(arg.at(1));
    21. dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System);
    22. dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
    23. list = dir.entryList();
    24. for (int i = 0; i < list.size(); ++i) {
    25. insertItem(i, list.at(i));
    26. }
    27. dir = dir.canonicalPath();
    28. if (dir.isRoot()) {
    29. //QFileInfoList roots = QDir::drives();
    30. QList<QFileInfo> roots = QDir::drives();
    31. QString string = QDir::currentPath();
    32. string.truncate(3);
    33. int n = 0;
    34. for (int i = 0; i < roots.size(); ++i) {
    35. QFileInfo info = roots.at(i);
    36. if (info.absoluteFilePath() != string) {
    37. insertItem(n, info.absoluteFilePath());
    38. ++n;
    39. }
    40. }
    41. } else takeItem(0);
    42. connect(this, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(onActivation(QListWidgetItem*)));
    43. };
    44.  
    45. void CListBox::onActivation(QListWidgetItem *item) {
    46. QDir dir;
    47. int flag = 0;
    48. QStringList list;
    49. QString str = item->text();
    50. QFileInfo fiche(str);
    51. QString ext = fiche.suffix().toLower();
    52. QStringList ext1, ext2;
    53. ext1 << "" << "txt" << "bak" <<"bat" <<"ini" <<"log" << "dat" << "sys";
    54. ext2 <<"h"<<"cpp"<<"pro"<<"lsp"<<"frm"<<"java"<<"vb"<<"bas"<<"js"<<"awk"<<"xul";
    55. if (fiche.isDir()) {
    56. clear();
    57. dir.setCurrent(str);
    58. emit currentDir(dir.currentPath());
    59. dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System);
    60. dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
    61. list = dir.entryList();
    62. for (int i = 0; i < list.size(); ++i) {
    63. insertItem(i, list.at(i));
    64. }
    65. dir = dir.canonicalPath();
    66. if (dir.isRoot()) {
    67. clear();
    68. dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System);
    69. dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase| QDir::Name);
    70. list = dir.entryList();
    71. for (int i = 0; i < list.size(); ++i) {
    72. insertItem(i, list.at(i));
    73. }
    74. //QFileInfoList roots = QDir::drives();
    75. QList<QFileInfo> roots = QDir::drives();
    76. QString string = QDir::currentPath();
    77. string.truncate(3);
    78. int n = 0;
    79. for (int i = 0; i < roots.size(); ++i) {
    80. QFileInfo info = roots.at(i);
    81. if (info.absoluteFilePath() != string) {
    82. insertItem(n, info.absoluteFilePath());
    83. ++n;
    84. }
    85. }
    86. } else takeItem(0);
    87. } else {
    88. emit currentDir(dir.currentPath());
    89. QString cmd = "c:/dos/ed /p ";
    90. if (ext1.contains(ext)) flag = 1;
    91. if (ext2.contains(ext)) flag = 2;
    92. switch(flag) {
    93. case 1: QProcess::startDetached(cmd.append(str)); break;
    94. case 2: QProcess::startDetached("led", QStringList() << str); break;
    95. default: QProcess::startDetached("start", QStringList() << str);
    96. }
    97. }
    98. };
    99.  
    100. // --------------------
    101.  
    102. class CLineEdit: public QLineEdit {
    103. Q_OBJECT
    104. public:
    105. CLineEdit(QWidget *pParent);
    106. signals:
    107. void setDir(const QString&);
    108. protected slots:
    109. void onReturnPressed();
    110. };
    111.  
    112. CLineEdit::CLineEdit(QWidget *pParent): QLineEdit(pParent) {
    113. setText(QDir::currentPath());
    114. connect( this, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
    115. };
    116.  
    117. void CLineEdit::onReturnPressed() {
    118. QDir dir;
    119. QString string = displayText();
    120. dir.setCurrent(string);
    121. emit setDir(dir.currentPath());
    122. //qDebug() << QDir::currentPath();
    123. };
    124.  
    125. // --------------------
    126.  
    127. int main(int argc, char *argv[]) {
    128. QApplication app(argc, argv);
    129. QFrame wnd;
    130. QVBoxLayout layout(&wnd);
    131. CListBox lbox(&wnd);
    132. CLineEdit ledit(&wnd);
    133. layout.addWidget(&lbox, Qt::AlignHCenter | Qt::AlignVCenter);
    134. layout.addWidget(&ledit, Qt::AlignHCenter | Qt::AlignVCenter);
    135. wnd.setWindowTitle("File browser");
    136. wnd.show();
    137. lbox.setFocus();
    138.  
    139. QPalette p1 = lbox.palette();
    140. p1.setColor(QPalette::Base, QColor(255, 248, 220));
    141. lbox.setPalette(p1);
    142. QPalette p2 = ledit.palette();
    143. p2.setColor(QPalette::Base, QColor(255, 248, 220));
    144. ledit.setPalette(p2);
    145.  
    146. QObject::connect(&lbox, SIGNAL(currentDir(const QString&)), &ledit, SLOT(setText(const QString&)));
    147. QObject::connect(&ledit, SIGNAL(setDir(const QString&)), &lbox, SLOT(update()));
    148.  
    149. return app.exec();
    150. }
    151.  
    152. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    All codes packed together as a single file to make it synoptic!
    Last edited by wysota; 17th July 2006 at 09:33. Reason: Added [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: File browser

    Check this thread out. It could be useful. Using QDirModel + any QAbstractItemView descendant is rather simple.

    For launching executables you might consider using an combination of:


    For opening other than executable files:
    • There is no platform independant way in current version of Qt. You will have to use some platform specific blocks and ShellExecute() for windows and something corresponding for linux. As a hint, try searching this forum for "ShellExecute" and you might find something useful.
    • Something worth checking out is QDS's launcher service.
    • Upcoming Qt 4.2 will have also some kind of desktop services.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    jp (17th July 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1

    Default Re: File browser

    Actually the launching of files is not an issue by itself since the File browser acknowledges file extensions and acts accordingly. Also one can always put the explorer as the default switch case to take care of the non-filtered extensions and as you mentioned future Qt versions will not fail to act more discriminately.
    What puzzles me the most is when one enters a path name in the LineEdit it becomes the new current directory and should trigger a rerun of the directory reading in the ListBox.
    • QObject::connect(&ledit, SIGNAL(setDir(const QString&)), &lbox, SLOT(update()));
    But the update is not taking place, should it be considered as bug?
    Last edited by jp; 17th July 2006 at 17:54.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: File browser

    Quote Originally Posted by jp
    What puzzle me the most is when one enters a path name in the LineEdit it becomes the new current directory and should trigger a rerun of the directory reading in the ListBox.
    • QObject::connect(&ledit, SIGNAL(setDir(const QString&)), &lbox, SLOT(update()));
    But the update is not taking place, should it be considered as bug?
    That does only schedule a new paint event for the listbox. As the content hasn't changed anyhow, nothing happens. You haven't made the listbox to react anyhow. Maybe you should add a custom slot which takes the changed dir as a parameter and change the contents of the listbox?
    J-P Nurmi

  6. #5
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1

    Default Re: File browser

    • QObject::connect(&ledit, SIGNAL(setDir(const QString&)), &lbox, SLOT(close()));

    Even without dwelling too much into the semantic of what update is supposed to mean, the close command as expected triggers the ListBox to close down and equally for call to an update one will expect the ListBox to do an update with a rerun of its reading.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: File browser

    Quote Originally Posted by jp
    ..and equally for call to an update one will expect the ListBox to do an update with a rerun of its reading.
    It will redraw its contents. The contents just haven't magically changed to anything. You are only changing the listbox contents in constructor and when an item is activated. You are not changing the listbox contents when a paint event occurs.
    J-P Nurmi

  8. #7
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1

    Default Re: File browser

    Quote Originally Posted by jpn
    ... You are only changing the listbox contents in constructor and when an item is activated. You are not changing the listbox contents when a paint event occurs.
    OK then, if update is not updating and unless Qt is capable of faking events, Qt promises to be a fairly unwieldy scripting language!
    Last edited by jp; 18th July 2006 at 17:15.

  9. #8
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File browser

    Quote Originally Posted by jp
    OK then, if update is not updating and unless Qt is capable of faking events, Qt promises to be a fairly unwieldy scripting language!
    Hmmm, two things:
    1. Qt is capable of faking certain events.
    2. Qt is not a scripting language.
    Save yourself some pain. Learn C++ before learning Qt.

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: File browser

    Quote Originally Posted by jp
    OK then, if update is not updating and unless Qt is capable of faking events, Qt promises to be a fairly unwieldy scripting language!
    No offense, but the problem is not at all within Qt. The problem lies in your code structure. Let's make this clear. How do you expect the list box to change it's contents after calling update()? It will just redraw earlierly inserted items. You need to pass the current dir from the line edit to the list box and change the contents.

    To make this work, your code should look more like this:

    Qt Code:
    1. void CListBox::onActivation(QListWidgetItem *item)
    2. {
    3. setDir(item->text());
    4. }
    5.  
    6. void CListBox::setDir(const QString& dir)
    7. {
    8. QFileInfo fiche(dir);
    9. ...
    10. for (...)
    11. insertItem(...)
    12. ...
    13. emit dirChanged(dir);
    14. }
    15.  
    16. void CLineEdit::onReturnPressed()
    17. {
    18. emit dirChanged(text());
    19. }
    20.  
    21. QObject::connect(&lbox, SIGNAL(dirChanged(const QString&)), &ledit, SLOT(setText(const QString&)));
    22. QObject::connect(&ledit, SIGNAL(dirChanged(const QString&)), &lbox, SLOT(setDir(const QString&)));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #10
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1

    Default Re: File browser

    Thanks jpn and Chicken Blood Machine.
    I was simply under the misguided impression that one could write compact code without the benefit of using the QDirModel class.

Similar Threads

  1. Suggestions/Ideas about a file browser
    By SkripT in forum Qt Programming
    Replies: 31
    Last Post: 6th April 2011, 23:17
  2. Draging a non-existing file to the Windows Desktop
    By klaus1111 in forum Qt Programming
    Replies: 13
    Last Post: 20th September 2007, 11:47
  3. Replies: 11
    Last Post: 4th July 2006, 15:09
  4. File permission QFile::WriteOther on Win Dos
    By patrik08 in forum Newbie
    Replies: 1
    Last Post: 13th June 2006, 14:16
  5. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33

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.