文件管理系统 (关于QT的一些使用方法)
这个很早我就想写了,一直拖啊拖,这篇博客是为了保留QT的一些使用方法,以免以后在使用QT的时候没有参照的代码
先贴一份QT的文件结构
然后大概解释一下 allhead代表所有的头文件,creat_d好像创建文件夹,_f应该是文件(反正一个文件夹一个文件),然后就是打开文件夹啊,打开文件,写入文件,返回上一级这些操作。
(Ps:可能有些文件被我修改以后变的没有意义了 ,由于时间太长,我也记不清楚用了多少,但是test开头的肯定是用于测试一些方法的)
这里是头文件
allhead.h
#ifndef ALLHEAD_H #define ALLHEAD_H #include<QString> #include<QMessageBox> #include <stdio.h> #include <QVector> extern QString noww; extern int pridee; extern int flag; struct File { QString name; char kind; int pride; int id; int fa; QString file; }; extern File q; extern int sz,nowh; extern QVector<File> v; extern QVector<int>mp[1005]; extern QVector<File>op; #endif // ALLHEAD_H
closefile.h
#ifndef CLOSEFILE_H #define CLOSEFILE_H #define QT4_SUPPORT #include <QWidget> #include<testfunction.h> #include<qdebug.h> #include "allhead.h" namespace Ui { class closefile; } class closefile : public QWidget { Q_OBJECT public: explicit closefile(QWidget *parent = 0); ~closefile(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_pushButton_clicked(); private: Ui::closefile *ui; }; #endif // CLOSEFILE_H
creat_d.h
#ifndef CREAT_D_H #define CREAT_D_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class creat_d; } class creat_d : public QWidget { Q_OBJECT public: explicit creat_d(QWidget *parent = 0); ~creat_d(); private slots: void on_lineEdit_cursorPositionChanged(); void on_lineEdit_2_cursorPositionChanged(); void on_pushButton_clicked(); private: Ui::creat_d *ui; }; #endif // CREAT_D_H
creat_f.h
#ifndef CREAT_F_H #define CREAT_F_H #include<QObject> #include <QWidget> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" struct node { QString name; int pride; }; namespace Ui { class creat_f; } class creat_f : public QWidget { Q_OBJECT public: explicit creat_f(QWidget *parent = 0); ~creat_f(); QString name; int pride; private slots: void on_lineEdit_cursorPositionChanged(); void on_lineEdit_2_cursorPositionChanged(); void on_ppp_clicked(); private: Ui::creat_f *ui; }; #endif // CREAT_F_H
del.h
#ifndef DEL_H #define DEL_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class del; } class del : public QWidget { Q_OBJECT public: explicit del(QWidget *parent = 0); ~del(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_pushButton_clicked(); private: Ui::del *ui; }; #endif // DEL_H
intopath.h
#ifndef INTOPATH_H #define INTOPATH_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class intopath; } class intopath : public QWidget { Q_OBJECT public: explicit intopath(QWidget *parent = 0); ~intopath(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_nnn_clicked(); private: Ui::intopath *ui; }; #endif // INTOPATH_H
isfile.h
#ifndef ISFILE_H #define ISFILE_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class Isfile; } class Isfile : public QWidget { Q_OBJECT public: explicit Isfile(QWidget *parent = 0); ~Isfile(); private: Ui::Isfile *ui; }; #endif // ISFILE_H
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QObject> #include <QMainWindow> #include "creat_f.h" #include "intopath.h" #include "del.h" #include <QVector> #include <QString> #include "openfile.h" #include "closefile.h" #include "rfile.h" #include "wfile.h" #include "wwfile.h" #include "allhead.h" #include <stdio.h> #include "creat_d.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); creat_f *z; creat_d *d; intopath *path; del *DEL; openfile *open; closefile *close; rfile *rf; wfile *wf; wwfile *wwf; private slots: void on_creat_clicked(); void on_creat_ff_clicked(); void on_pushButton_clicked(); void on_index_clicked(); void on_Back_clicked(); void on_Del_clicked(); void on_openfile_clicked(); void on_closefile_clicked(); void on_rfile_clicked(); void on_wfile_clicked(); void on_index_s_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
openfile.h
#ifndef OPENFILE_H #define OPENFILE_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class openfile; } class openfile : public QWidget { Q_OBJECT public: explicit openfile(QWidget *parent = 0); ~openfile(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_pushButton_clicked(); private: Ui::openfile *ui; }; #endif // OPENFILE_H
rfile.h
#ifndef RFILE_H #define RFILE_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class rfile; } class rfile : public QWidget { Q_OBJECT public: explicit rfile(QWidget *parent = 0); ~rfile(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_pushButton_clicked(); private: Ui::rfile *ui; }; #endif // RFILE_H
wfile.h
#ifndef WFILE_H #define WFILE_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class wfile; } class wfile : public QWidget { Q_OBJECT public: explicit wfile(QWidget *parent = 0); ~wfile(); QString name; private slots: void on_lineEdit_cursorPositionChanged(); void on_pushButton_clicked(); void on_textEdit_destroyed(); private: Ui::wfile *ui; }; #endif // WFILE_H
wwfile.h
#ifndef WWFILE_H #define WWFILE_H #include <QWidget> #include<QObject> #include <QVector> #include <QString> #include <QApplication> #include "allhead.h" namespace Ui { class wwfile; } class wwfile : public QWidget { Q_OBJECT public: explicit wwfile(QWidget *parent = 0); ~wwfile(); QString name; private slots: void on_pushButton_clicked(); void on_textEdit_destroyed(); private: Ui::wwfile *ui; }; #endif // WWFILE_H
下面时候Sources
allhead.cpp
#include<iostream> #include <stdio.h> #include<allhead.h> QString noww; int pridee; int flag; File q; int sz,nowh; QVector<File> v; QVector<int>mp[1005]; QVector<File>op;
closefile.cpp
#include "closefile.h" #include "ui_closefile.h" closefile::closefile(QWidget *parent) : QWidget(parent), ui(new Ui::closefile) { ui->setupUi(this); } closefile::~closefile() { delete ui; } void closefile::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void closefile::on_pushButton_clicked() { bool ok=true; for(int i=0;i<op.size();i++){ if(op[i].name==noww&&op[i].fa==nowh){ ok=false;break; } } QString tmp=""; qDebug()<<"asd "<<ok<<endl; if(ok){ QMessageBox::about(this,"关闭文件","该文件未打开"); this->close(); return ; } QVector<File>::iterator it; for(it=op.begin();it!=op.end();it++){ if(it->name==noww&&it->fa==nowh){ op.erase(it); break; } } QMessageBox::about(this,"关闭文件","文件已关闭"); this->close(); return ; }
creat_d.cpp
#include "creat_d.h" #include "ui_creat_d.h" #include <qDebug> creat_d::creat_d(QWidget *parent) : QWidget(parent), ui(new Ui::creat_d) { ui->setupUi(this); } creat_d::~creat_d() { delete ui; } void creat_d::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void creat_d::on_lineEdit_2_cursorPositionChanged() { } void creat_d::on_pushButton_clicked() { bool ok=true; for(int i=0;i<mp[nowh].size();i++){ int as=mp[nowh][i]; for(int j=0;j<v.size();j++){ if(v[j].name==noww&&v[j].id==as){ ok=false; break; } } if(!ok)break; } QString tmp=""; if(!ok){ QMessageBox::about(this,"新建文件","此文件或者目录已经存在"); this->close(); return ; } q.name=noww; pridee=ui->lineEdit_2->text().toInt(); q.id=++sz; q.kind='f'; q.pride=pridee; qDebug()<<"sss "<<pridee<<endl; q.fa=nowh; mp[nowh].push_back(q.id); v.push_back(q); QMessageBox::about(this,"新建文件","创建成功"); this->close(); }
creat_f.cpp
#include "creat_f.h" #include "ui_creat_f.h" #include"mainwindow.h" #include <qDebug> #include "allhead.h" #include <QString> creat_f::creat_f(QWidget *parent) : QWidget(parent), ui(new Ui::creat_f) { ui->setupUi(this); } creat_f::~creat_f() { delete ui; } void creat_f::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void creat_f::on_lineEdit_2_cursorPositionChanged() { } void creat_f::on_ppp_clicked() { bool ok=true; for(int i=0;i<mp[nowh].size();i++){ int as=mp[nowh][i]; for(int j=0;j<v.size();j++){ if(v[j].name==noww&&v[j].id==as){ ok=false; break; } } if(!ok)break; } QString tmp=""; if(!ok){ QMessageBox::about(this,"新建文件夹","此文件或者目录已经存在"); this->close(); return ; } q.name=noww; pridee=ui->lineEdit_2->text().toInt(); q.id=++sz; q.kind='d'; q.pride=pridee; q.fa=nowh; mp[nowh].push_back(q.id); v.push_back(q); QMessageBox::about(this,"新建文件夹","创建成功"); this->close(); }
del.cpp
#include "del.h" #include "ui_del.h" del::del(QWidget *parent) : QWidget(parent), ui(new Ui::del) { ui->setupUi(this); } del::~del() { delete ui; } void del::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void dfs(int x) { for(int i=0;i<mp[x].size();i++){ int as=mp[x][i]; int k=-1; for(int j=0;j<v.size();j++){ if(v[j].id==as&&v[j].kind=='d'){ k=as; break; } } if(k!=-1)dfs(k); } mp[x].clear(); } void del::on_pushButton_clicked() { int k=-1; for(int i=0;i<v.size();i++){ if(v[i].name==noww&&v[i].fa==nowh){ k=v[i].id;break; } } QString tmp=""; if(k==-1){ QMessageBox::about(this,"删除文件","没有此文件"); this->close(); return ; } dfs(k); QVector<int>::iterator it; for(it=mp[nowh].begin();it!=mp[nowh].end();it++){ int as=*it; bool ok=true; for(int j=0;j<v.size();j++){ if(v[j].id==as&&v[j].name==noww){ ok=false; break; } } if(!ok){ mp[nowh].erase(it); return ; } } QMessageBox::about(this,"删除文件","删除完毕"); this->close(); }
intopath.cpp
#include "intopath.h" #include "ui_intopath.h" intopath::intopath(QWidget *parent) : QWidget(parent), ui(new Ui::intopath) { ui->setupUi(this); } intopath::~intopath() { delete ui; } void intopath::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void intopath::on_nnn_clicked() { bool ok=true; int k; for(int i=0;i<mp[nowh].size();i++){ int as=mp[nowh][i]; for(int j=0;j<v.size();j++){ if(v[j].name==noww&&v[j].id==as){ ok=false;k=as;break; } } if(!ok)break; } QString tmp=""; if(ok){ QMessageBox::about(this,"打开文件夹","路径输入错误,请核对"); this->close(); } else{ nowh=k; QMessageBox::about(this,"打开文件夹","以打开"); this->close(); } }
isfile.cpp
#include "isfile.h" #include "ui_isfile.h" Isfile::Isfile(QWidget *parent) : QWidget(parent), ui(new Ui::Isfile) { ui->setupUi(this); } Isfile::~Isfile() { delete ui; }
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "creat_f.h" #include "intopath.h" #include "del.h" #include "openfile.h" #include "closefile.h" #include "rfile.h" #include "wfile.h" #include "wwfile.h" #include "iostream" #include <QDebug> #include "allhead.h" using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); sz=0;nowh=0; v.clear(); op.clear(); for(int i=0;i<1005;i++)mp[i].clear(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_creat_clicked() { z = new creat_f; z->setWindowTitle(tr("新建文件夹")); z->show(); } void MainWindow::on_creat_ff_clicked() { d = new creat_d; d->setWindowTitle(tr("新建文件")); d->show(); } void MainWindow::on_pushButton_clicked() { path=new intopath; path->setWindowTitle(tr("打开文件夹")); path->show(); } void MainWindow::on_index_clicked() { QString tmp=""; tmp+="Name\tPride\tKind\tParent\n"; for(int i=0;i<mp[nowh].size();i++){ int as=mp[nowh][i]; for(int j=0;j<v.size();j++){ if(v[j].id==as){ tmp+=v[j].name;tmp+="\t"; tmp+=QString::number(v[j].pride);tmp+="\t"; tmp+=v[j].kind;tmp+="\t"; tmp+=QString::number(v[j].fa);tmp+="\n"; break; } } } ui->com->setText(tmp); } void MainWindow::on_Back_clicked() { QString ans=""; if(nowh==0){ ans+="Error:当前已为用户根目录,无法再回退\n"; ui->com->setText(ans); return ; } int k; for(int i=0;i<v.size();i++){ if(v[i].id==nowh){ k=v[i].fa;break; } } nowh=k; ans+="返回完成"; ui->com->setText(ans); } void MainWindow::on_Del_clicked() { DEL=new del; DEL->setWindowTitle(tr("删除文件")); DEL->show(); } void MainWindow::on_openfile_clicked() { open=new openfile; open->setWindowTitle(tr("打开文件")); open->show(); } void MainWindow::on_closefile_clicked() { close=new closefile; close->setWindowTitle(tr("关闭文件")); close->show(); } void MainWindow::on_rfile_clicked() { rf=new rfile; rf->setWindowTitle(tr("读文件")); rf->show(); } void MainWindow::on_wfile_clicked() { wf=new wfile; wf->setWindowTitle(tr("写文件")); wf->show(); } void MainWindow::on_index_s_clicked() { QString ans=""; int cnt=0; QVector<File> xx; xx.clear(); for(int i=0;i<v.size();i++){ xx.push_back(v[i]); } // printf("文件名 索引号\n"); ans+="文件名\t索引号\n"; for(int i=0;i<xx.size();i++){ // cout<<index[i].first.name; // printf(" %d\n",index[i].second); ans+=xx[i].name;ans+='\t'; ans+=QString::number(i+1); ans+='\n'; } ui->com->setText(ans); }
openfile.cpp
#include "openfile.h" #include "ui_openfile.h" openfile::openfile(QWidget *parent) : QWidget(parent), ui(new Ui::openfile) { ui->setupUi(this); } openfile::~openfile() { delete ui; } void openfile::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void openfile::on_pushButton_clicked() { int ok=true; for(int i=0;i<op.size();i++){ if(op[i].name==noww&&op[i].id==nowh){ ok=false; return ; } } QString tmp=""; if(!ok){ QMessageBox::about(this,"打开文件","该文件已被打开"); this->close(); return ; } int k=-1; for(int i=0;i<v.size();i++){ if(v[i].name==noww&&v[i].fa==nowh){ k=v[i].id; op.push_back(v[i]); break; } } if(k==-1){ QMessageBox::about(this,"打开文件","没有此文件"); this->close(); } else{ QMessageBox::about(this,"打开文件","文件打开"); this->close(); } }
rfile.cpp
#include "rfile.h" #include "ui_rfile.h" rfile::rfile(QWidget *parent) : QWidget(parent), ui(new Ui::rfile) { ui->setupUi(this); } rfile::~rfile() { delete ui; } void rfile::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void rfile::on_pushButton_clicked() { bool ok=true; int s; for(int i=0;i<op.size();i++){ if(op[i].name==noww&&op[i].fa==nowh){ ok=false;s=op[i].pride;break; } } QString tmp=""; if(ok){ // tmp+="该文件尚未打开"; // ui->com->setText(tmp); QMessageBox::about(this,"读文件","该文件尚未打开"); this->close(); return; } int k; for(int i=0;i<v.size();i++){ if(v[i].name==noww&&v[i].fa==nowh){ k=i; break; } } // ui->com->setText(v[k].file); QMessageBox::about(this,v[k].name,v[k].file); this->close(); }
wfile.cpp
#include "wfile.h" #include "ui_wfile.h" #include <qDebug> wfile::wfile(QWidget *parent) : QWidget(parent), ui(new Ui::wfile) { ui->setupUi(this); } wfile::~wfile() { delete ui; } void wfile::on_lineEdit_cursorPositionChanged() { noww=ui->lineEdit->text(); } void wfile::on_pushButton_clicked() { bool ok=true; int s; for(int i=0;i<op.size();i++){ if(op[i].name==noww&&op[i].fa==nowh){ ok=false;s=op[i].pride;break; } } if(ok){ QMessageBox::about(this,"写文件","该文件未被打开"); this->close(); return; } QString la; la=ui->textEdit->toPlainText(); // qDebug()<<"qwe "<<noww<<endl; int k; // int zzz=1; for(int i=0;i<v.size();i++){ if(v[i].name==noww&&v[i].fa==nowh){ k=i; break; } } qDebug()<<"aaa "<<s<<endl; if(s==0){ QMessageBox::about(this,"写文件","文件没有写权限"); this->close(); return ; } for(int i=0;i<v.size();i++){ if(v[i].name==noww&&v[i].fa==nowh){ v[i].file+=la; } } // v[k].file+=noww; QMessageBox::about(this,"写文件","写入完成"); this->close(); } void wfile::on_textEdit_destroyed() { }
wwfile.cpp
#include "wwfile.h" #include "ui_wwfile.h" wwfile::wwfile(QWidget *parent) : QWidget(parent), ui(new Ui::wwfile) { ui->setupUi(this); } wwfile::~wwfile() { delete ui; } void wwfile::on_pushButton_clicked() { // int k; // for(int i=0;i<v.size();i++){ // if(v[i].name==noww&&v[i].fa==nowh){ // k=i; // break; // } // } // v[k].file+=noww; // QMessageBox::about(this,"写文件","写入完成"); // this->close(); } void wwfile::on_textEdit_destroyed() { // noww=ui->textEdit->toPlainText(); }