QT文件操作
QFile读写文本
提供读取写入文件的接口
文件名通常在构造函数中传递
1 2 3 4 5 6
| QIDDevice::Readonly:以只读的方式打开文件,用于载入文件 QIDDevice::Writeonly:以只写方式打开文件,用于保存文件 QIDDevice::ReadWrite:以读写方式打开 QIDDevice::Append:以添加模式打开,新写入文件的数据添加到文件末尾 QIDDevice::Truncate:以裁取的方式打开文件,文件原有内容会被删除 QIDDevice::Text:以文本打开文件
|
流程:文件–>打开方式–>关闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include<QTextEdit> #include<QFile> #include<QVBoxLayout> #include<QHBoxLayout> #include<QPushButton>
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private: Ui::MainWindow *ui; private: QTextEdit *testEdit; QFile file; QHBoxLayout *hBoxLayout; QVBoxLayout *vBoxLayout; QWidget *hwidget; QWidget *vwideget; QPushButton *openPushButton; QPushButton *closePushButton; private slots: bool openFile(); void closeFile(); }; #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setGeometry(0,0,800,480); textEdit = new QTextEdit(); vBoxLayout = new QVBoxLayout(); hBoxLayout = new QHBoxLayout(); vwideget = new QWidget (); hwidget = new QWidget (); openPushButton = new QPushButton (); closePushButton = new QPushButton(); openPushButton->setMinimumHeight(50); openPushButton->setMaximumWidth(120); closePushButton->setMinimumHeight(50); closePushButton->setMaximumWidth(120); openPushButton->setText("open"); closePushButton->setText("close");
closePushButton->setEnabled(false);
hBoxLayout->addWidget(openPushButton); hBoxLayout->addWidget(closePushButton); hwidget->setLayout(hBoxLayout);
vBoxLayout->addWidget(textEdit); vBoxLayout->addWidget(hwidget); vwideget->setLayout(vBoxLayout);
setCentralWidget(vwideget);
connect(openPushButton,SIGNAL(clicked()), this,SLOT(openFile())); connect(closePushButton,SIGNAL(clicked()), this,SLOT(openFile()));
}
MainWindow::~MainWindow() { delete ui; }
bool MainWindow::openFile() { QString fileName = QFileDialog::getOpenFileName(this); file.setFileName(fileName); if(!file.exists()) return false; if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false; textEdit->setPlainText(file.readAll()); openPushButton->setEnabled(false); closePushButton->setEnabled(true);
file.close(); return true; } void MainWindow::closeFile() { if(!openPushButton->isEnabled()){
QString str = textEdit->toPlainText(); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) return;
QByteArray strBytes = str.toUtf8(); file.write(strBytes,strBytes.length()); textEdit->clear(); openPushButton->setEnabled(true); closePushButton->setEnabled(false); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setGeometry(0,0,800,480);
textEdit = new QTextEdit(); vBoxLayout = new QVBoxLayout(); hBoxLayout = new QHBoxLayout(); vwideget = new QWidget (); hwidget = new QWidget (); openPushButton = new QPushButton (); closePushButton = new QPushButton(); openPushButton->setMinimumHeight(50); openPushButton->setMaximumWidth(120); closePushButton->setMinimumHeight(50); closePushButton->setMaximumWidth(120);
openPushButton->setText("打开"); closePushButton->setText("关闭");
closePushButton->setEnabled(false);
hBoxLayout->addWidget(openPushButton); hBoxLayout->addWidget(closePushButton); hwidget->setLayout(hBoxLayout);
vBoxLayout->addWidget(textEdit); vBoxLayout->addWidget(hwidget); vwideget->setLayout(vBoxLayout);
setCentralWidget(vwideget);
connect(openPushButton,SIGNAL(clicked()), this,SLOT(openFile())); connect(closePushButton,SIGNAL(clicked()), this,SLOT(openFile()));
}
MainWindow::~MainWindow() { delete ui; }
bool MainWindow::openFile() { QString fileName = QFileDialog::getOpenFileName(this); file.setFileName(fileName); if(!file.exists()) return false; if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false; textEdit->setPlainText(file.readAll()); openPushButton->setEnabled(false); closePushButton->setEnabled(true);
file.close(); return true; } void MainWindow::closeFile() { if(!openPushButton->isEnabled()){ QString str = textEdit->toPlainText(); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) return;
QByteArray strBytes = str.toUtf8(); file.write(strBytes,strBytes.length());
textEdit->clear(); openPushButton->setEnabled(true); closePushButton->setEnabled(false); } }
|
2.QTextStream读写文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setGeometry(0,0,800,480); textEdit = new QTextEdit(); vBoxLayout = new QVBoxLayout(); hBoxLayout = new QHBoxLayout(); vwideget = new QWidget (); hwidget = new QWidget (); openPushButton = new QPushButton (); closePushButton = new QPushButton(); openPushButton->setMinimumHeight(50); openPushButton->setMaximumWidth(120); closePushButton->setMinimumHeight(50); closePushButton->setMaximumWidth(120); openPushButton->setText("open"); closePushButton->setText("close");
closePushButton->setEnabled(false);
hBoxLayout->addWidget(openPushButton); hBoxLayout->addWidget(closePushButton); hwidget->setLayout(hBoxLayout);
vBoxLayout->addWidget(textEdit); vBoxLayout->addWidget(hwidget); vwideget->setLayout(vBoxLayout);
setCentralWidget(vwideget);
connect(openPushButton,SIGNAL(clicked()), this,SLOT(openFile())); connect(closePushButton,SIGNAL(clicked()), this,SLOT(openFile()));
}
MainWindow::~MainWindow() { delete ui; }
bool MainWindow::openFile() { QString fileName = QFileDialog::getOpenFileName(this); file.setFileName(fileName); if(!file.exists()) return false; if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false; QTextStream stream(&file); textEdit->setPlainText(file.readAll()); openPushButton->setEnabled(false); closePushButton->setEnabled(true);
file.close(); return true; } void MainWindow::closeFile() { if(!openPushButton->isEnabled()){
QString str = textEdit->toPlainText(); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream stream(&file); QString str_1 = textEdit->toPlainText(); stream <<str_1; textEdit->clear(); openPushButton->setEnabled(true); closePushButton->setEnabled(false); } }
|
QT多线程
QThread类提供一种独立于平台的方法管理线程。
继承QThread的线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#ifndef MAINWINDOW_H #define MAINWINDOW_H
#include<QMainWindow> #include<QThread> #include<QDebug> #include<QPushButton>
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class WorkerThread;
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
private: Ui::MainWindow *ui; private: WorkerThread *workThread; QPushButton *pushButton; private slots: void headleResults(const QString &result); void pushButtonClicked(); };
class WorkerThread : public QThread { Q_OBJECT public: WorkerThread(QWidget *parent=nullptr){ Q_UNUSED(parent); } void run() override{ QString result="线程开启成功"; sleep(2); emit resultReady(result); } signals: void resultReady(const QString &s); }; #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include "mainwindow.h" #include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setGeometry(0,0,800,480); pushButton=new QPushButton(this); workerThread = new WorkerThread(this);
pushButton->resize(100,40); pushButton->setText("圆神,启动");
connect(workerThread,SIGNAL(resultReady(QString)),this,SLOT(handleResults(QString))); connect(pushButton,SIGNAL(clicked()),this,SLOT(pushButtonClicked())); }
MainWindow::~MainWindow() { workerThread->quit(); if(workerThread->wait(2000)){
qDebug()<<"Thread over!"<<endl; } delete ui; } void MainWindow::handleResults(const QString &result){ qDebug()<<result<<endl; } void MainWindow::pushButtonClicked() { if(!workerThread->isRunning()) workerThread->start(); }
|