Results 1 to 10 of 10

Thread: File browser

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.