当前位置:   article > 正文

lv20 QT文件编程6

lv20 QT文件编程6

1 文件普通读写

2 流式操作

  1. QFile file("in.txt");
  2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3. return;
  4. QTextStream in(&file);
  5. while (!in.atEnd()) {
  6. QString line = in.readLine();
  7. process_line(line);
  8. }

示例

widegt.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QTextEdit>
  5. #include <QLineEdit>
  6. #include <QPushButton>
  7. #include <QFileDialog>
  8. #include <QTextStream>
  9. class Widget : public QWidget
  10. {
  11. Q_OBJECT
  12. public:
  13. Widget(QWidget *parent = 0);
  14. ~Widget();
  15. public slots:
  16. void showfile()
  17. {
  18. QString filename = QFileDialog::getOpenFileName();
  19. le->setText(filename);
  20. #if 0
  21. QFile file(filename);
  22. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  23. return;
  24. while (!file.atEnd()) {
  25. QByteArray line = file.readLine();
  26. // process_line(line);
  27. te->append(line);
  28. }
  29. #endif
  30. QFile file(filename);
  31. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  32. return;
  33. QTextStream in(&file);
  34. while (!in.atEnd()) {
  35. QString line = in.readLine();
  36. // process_line(line);
  37. te->append(line);
  38. }
  39. }
  40. private:
  41. QTextEdit *te;
  42. QLineEdit *le;
  43. QPushButton *pb;
  44. };
  45. #endif // WIDGET_H

wigdet.cpp

  1. #include "widget.h"
  2. #include <QVBoxLayout>
  3. Widget::Widget(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. te = new QTextEdit;
  7. te->setMinimumSize(640, 480);
  8. le = new QLineEdit;
  9. pb = new QPushButton("showtxt");
  10. QVBoxLayout *vbox = new QVBoxLayout;
  11. vbox->addWidget(te);
  12. vbox->addWidget(le);
  13. vbox->addWidget(pb);
  14. setLayout(vbox);
  15. connect(pb, SIGNAL(clicked(bool)), this, SLOT(showfile()));
  16. }
  17. Widget::~Widget()
  18. {
  19. }

 

3 文件属性

  1. QFileInfo info1("/home/bob/bin/untabify");
  2. info1.isSymLink(); // returns true
  3. info1.absoluteFilePath(); // returns "/home/bob/bin/untabify" info1.size(); // returns 56201
  4. info1.symLinkTarget(); // returns "/opt/pretty++/bin/untabify"

 文件夹遍历

  1. QDir dir;
  2. QFileInfoList list = dir.entryInfoList();
  3. for (int i = 0; i < list.size(); ++i) {
  4. QFileInfo fileInfo = list.at(i);
  5. }

main.cpp 

  1. #include <QCoreApplication>
  2. #include <QDir>
  3. #include <QFileInfo>
  4. #include <iostream>
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8. QDir dir;
  9. QFileInfoList list = dir.entryInfoList();
  10. std::cout << " Bytes Filename" << std::endl;
  11. for (int i = 0; i < list.size(); ++i) {
  12. QFileInfo fileInfo = list.at(i);
  13. std::cout << fileInfo.size()<< "\t"<<fileInfo.fileName().toStdString().c_str();
  14. std::cout << std::endl;
  15. }
  16. return a.exec();
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/184719
推荐阅读
相关标签
  

闽ICP备14008679号