目录
一、文件普通读写和流式读写
二、目录遍历和文件属性读写
三、进程
四、线程
五、线程同步
六、线程互斥
#ifndef WIDGET_H
#define WIDGET_H#include
#include
#include
#include
#include
#include class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();
public slots:void showfile(){QString filename = QFileDialog::getOpenFileName();le->setText(filename);
#if 0//普通写法QFile file(filename);if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return;while (!file.atEnd()) {QByteArray line = file.readLine();// process_line(line);te->append(line);}
#endif//适配器写法用流来操作流支持输出运算符QFile file(filename);if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return;QTextStream in(&file);while (!in.atEnd()) {QString line = in.readLine();// process_line(line);te->append(line);}}private:QTextEdit *te;QLineEdit *le;QPushButton *pb;
};#endif // WIDGET_H
#include "widget.h"
#include Widget::Widget(QWidget *parent): QWidget(parent)
{te = new QTextEdit;te->setMinimumSize(640, 480);le = new QLineEdit;pb = new QPushButton("showtxt");QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(te);vbox->addWidget(le);vbox->addWidget(pb);setLayout(vbox);connect(pb, SIGNAL(clicked(bool)), this, SLOT(showfile()));
}Widget::~Widget()
{}
用两种方式来读文件,都在代码里。最好的学习方式就是根据代码学
之前教我进程线程的老师是这样说的,我也很赞同这个观点,所以自己看代码吧
新建一个控制台程序
只有一个主函数,没有GUI
运行起来也只是一个黑框框
我们写个程序来试试
#include
#include
#include
#include int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QDir dir;QFileInfoList list = dir.entryInfoList();std::cout << "Bytes Filename" << std::endl;for (int i = 0; i < list.size(); ++i) {QFileInfo fileInfo = list.at(i);std::cout << fileInfo.size()<< "\t"<
选着文件打开后会自动开启一个进程用windows自带的文本阅读器打开选择的文件
#ifndef WIDGET_H
#define WIDGET_H#include
#include
#include
#include
#include
#include class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();
public slots:void showfile(){QString filename = QFileDialog::getOpenFileName();le->setText(filename);QStringList arg = {filename};QProcess ppp;ppp.execute("notepad", arg);}private:QLineEdit *le;QPushButton *pb;
};#endif // WIDGET_H
#include "widget.h"
#include Widget::Widget(QWidget *parent): QWidget(parent)
{le = new QLineEdit;pb = new QPushButton("showtxt");QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(le);vbox->addWidget(pb);setLayout(vbox);connect(pb, SIGNAL(clicked(bool)), this, SLOT(showfile()));
}Widget::~Widget()
{}
QT是一个异步驱动,或者事件驱动的开发,他真正开始执行其实是下面这个return才开始。
上面的show只是产生一个事件,一定要让程序运行return才会真正的开始执行
我们模拟两个进度条
工程文件比较多放到上传资源里
Linux中有一个信号量的概念,QT中也引用了这一概念
一个线程可以限制另一个线程配合它,这个程序是要求必须打印一个hello才打印一个world。
不是一个hello一个world就是这俩不一定是挨着的,而且hello出来以后一定会跟着一个world什么时候打印出来不一定。
和线程程序一起打包上传,文件有点多粘过来太麻烦了
就是对两个线程加上互斥锁其实和Linux的思想差不多
最后这三个程序全打包了,文件多要粘好久,需要的自己下载看看,不要积分。