通過本項目了解Qt的多視窗操作和檔案操作類的熟悉以及資料庫(SQLite)的熟悉(增删改查)
先上效果圖
多圖預警(8張)
通過左上角QAction彈窗
通過QFileDialog::getOpenFileName()打開檔案選擇視窗并還返還路徑
檔案拖動讀取路徑(打開支援拖動 setAcceptDrops(true))
QMessageBox類實作提示彈窗
先看主視窗代碼:
secondQt::secondQt(QWidget *parent)
: QMainWindow(parent)
{
setFixedSize(830,790);
ui = new Ui::secondQtClass();
ui->setupUi(this);
this->setWindowTitle("檔案屬性解析");
this->setWindowIcon(QIcon("file.jpg"));
ui->P_outInformation->setReadOnly(true);
fcn = new myFunction();
this->setAcceptDrops(true);
connect(ui->B_selectFile, SIGNAL(clicked()), this, SLOT(on_selectFile_click()));
connect(ui->B_entering, SIGNAL(clicked()), this, SLOT(on_entering_click()));
connect(ui->B_cancel, SIGNAL(clicked()), this, SLOT(on_cancel_click()));
connect(ui->B_initialize, SIGNAL(clicked()), this, SLOT(on_initialize_click()));
connect(ui->B_refresh, SIGNAL(clicked()), this, SLOT(on_refresh_click()));
connect(ui->B_clearTable, SIGNAL(clicked()), this, SLOT(on_clearTable_click()));
connect(ui->B_constent, SIGNAL(clicked()), this, SLOT(on_openButton_click()));
connect(ui->A_dispose, SIGNAL(triggered()), this, SLOT(on_enteringInterface_trigger()));
ui->L_intoInformation->setText("請同意協定");
}
//拖放操作
void secondQt::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void secondQt::dropEvent(QDropEvent *event)
{
QString name = event->mimeData()->urls().first().toString();
name.remove(0,8);
ui->L_intoInformation->setText(name);
}
//菜單切換頁面
void secondQt::on_enteringInterface_trigger()
{
QFormDoc *formDoc = new QFormDoc(); //不指定父視窗,用show()顯示
formDoc->setFun(fcn);
formDoc->setAttribute(Qt::WA_DeleteOnClose); //關閉時自動删除
formDoc->setWindowTitle("操作界面");
formDoc->show(); //在單獨的視窗中顯示
ui->A_dispose->setChecked(false);
}
//打開按鈕
void secondQt::on_openButton_click()
{
if (ui->B_constent->isChecked()!=NULL)
{
QString str = "本協定免除作者一切責任!";
QString title = "協定說明";
QMessageBox::information(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
ui->B_selectFile->setDisabled(false);
ui->B_entering->setDisabled(false);
ui->B_cancel->setDisabled(false);
ui->L_intoInformation->clear();
}
else
{
ui->B_selectFile->setEnabled(false);
ui->B_entering->setEnabled(false);
ui->B_cancel->setEnabled(false);
ui->L_intoInformation->setText("請同意協定");
}
}
//檔案選擇
void secondQt::on_selectFile_click()
{
QString fileAddress = fcn->selectFile();
if ("" != fileAddress)
{
ui->L_intoInformation->setText(fileAddress);
}
}
//錄入
void secondQt::on_entering_click()
{
if (ui->L_intoInformation->text()!= "")
{
fcn->entering(ui->L_intoInformation->text());
QString str = "錄入完成!";
QString title = "錄入";
QMessageBox::information(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
}
else
{
QString str = "請選擇檔案或輸入正确的路徑";
QString title = "警告";
QMessageBox::warning(this,title,str,QMessageBox::Ok, QMessageBox::NoButton );
}
}
//取消
void secondQt::on_cancel_click()
{
ui->L_intoInformation->setText("");
}
//初始化
void secondQt::on_initialize_click()
{
fcn->establishDatabase();
QString str = "初始化完成\n錯誤的初始化将導緻崩潰";
QString title = "初始化";
QMessageBox::information(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
}
//顯示重新整理
void secondQt::on_refresh_click()
{
ui->P_outInformation->clear();
ui->P_outInformation->appendPlainText(fcn->refresh());
}
//格式化清單
void secondQt::on_clearTable_click()
{
fcn->emptyDatabase();
QString str = "完成";
QString title = "清空清單";
QMessageBox::information(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
}
資料庫的操作視窗:
- QFormDoc :操作的主視窗
QFormDoc::QFormDoc(QWidget *parent) :
QWidget(parent),
ui(new Ui::QFormDoc)
{
ui->setupUi(this);
ui->P_out->setReadOnly(true);
this->setWindowTitle("操作界面");
this->setFixedSize(1130,780);
connect(ui->B_modification, SIGNAL(clicked()), this, SLOT(on_modification_click()));
connect(ui->B_delete, SIGNAL(clicked()), this, SLOT(on_delete_click()));
connect(ui->B_find, SIGNAL(clicked()), this, SLOT(on_find_click()));
connect(ui->B_refresh, SIGNAL(clicked()), this, SLOT(on_refresh_click()));
}
void QFormDoc::setFun(myFunction * fcn_c)
{
fcn = fcn_c;
}
//重新整理顯示
void QFormDoc::on_refresh_click()
{
ui->P_out->clear();
ui->P_out->setPlainText(fcn->refresh());
}
//打開修改視窗
void QFormDoc::on_modification_click()
{
QFormDocM *formDocM = new QFormDocM();
formDocM->setFun(fcn);
formDocM->setAttribute(Qt::WA_DeleteOnClose); //關閉時自動删除
formDocM->setWindowTitle("修改");
formDocM->show();
}
//打開查詢視窗
void QFormDoc::on_find_click()
{
QFormDocF *formDocF = new QFormDocF();
formDocF->setFun(fcn);
formDocF->setAttribute(Qt::WA_DeleteOnClose); //關閉時自動删除
formDocF->setWindowTitle("查找");
formDocF->show();
}
//打開删除視窗
void QFormDoc::on_delete_click()
{
QFormDocD *formDocD = new QFormDocD();
formDocD->setFun(fcn);
formDocD->setAttribute(Qt::WA_DeleteOnClose); //關閉時自動删除
formDocD->setWindowTitle("删除");
formDocD->show();
}
QFormDoc::~QFormDoc()
{
delete ui;
}
- QFormDocM:修改視窗
QFormDocM::QFormDocM(QWidget *parent) :
QWidget(parent),
ui(new Ui::QFormDocM)
{
ui->setupUi(this);
this->setFixedSize(520,240);
connect(ui->B_sure, SIGNAL(clicked()), this, SLOT(on_sure_click()));
}
void QFormDocM::setFun(myFunction * fcn_m)
{
fcn = fcn_m;
}
void QFormDocM::on_sure_click()
{
if (ui->B_fileName->isChecked() == NULL&&ui->B_fileSize->isChecked() == NULL)
{
QString str = "請勾選您需要檢索的字段!";
QString title = "警告";
QMessageBox::warning(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
}
else if (ui->B_fileName->isChecked() != NULL&&ui->B_fileSize->isChecked() != NULL)
{
QString str = "僅支援檢索單個字段!";
QString title = "警告";
QMessageBox::warning(this, title, str, QMessageBox::Ok, QMessageBox::NoButton);
}
else
{
QString str0 = "";
if (ui->B_fileName->isChecked()!=NULL)
{
str0 = "0";
}
else
{
str0 = "1";
}
QString str1 = ui->L_condition->text();
QString str2 = ui->L_replace->text();
m_datas.append(str0);
m_datas.append(str1);
m_datas.append(str2);
bool a = fcn->modification(m_datas);
m_datas.clear();
if (a)
{
QMessageBox::information(this, "提示", "修改完成", QMessageBox::Ok, QMessageBox::NoButton);
}
else
{
QMessageBox::warning(this, "警告", "無符合條件的資料", QMessageBox::Ok, QMessageBox::NoButton);
}
}
}
QFormDocM::~QFormDocM()
{
delete ui;
}
- QFormDocF:查詢視窗
QFormDocF::QFormDocF(QWidget *parent) :
QWidget(parent),
ui(new Ui::QFormDocF)
{
ui->setupUi(this);
this->setFixedSize(520, 365);
connect(ui->B_sure, SIGNAL(clicked()), this, SLOT(on_sure_click()));
}
void QFormDocF::setFun(myFunction *fcn_f)
{
fcn = fcn_f;
}
void QFormDocF::on_sure_click()
{
if (ui->L_fileName->text() == "")
{
QMessageBox::warning(this, "未輸入警告", "請輸入檔案名稱", QMessageBox::Ok, QMessageBox::NoButton);
}
else
{
QString nowDatas = fcn->find(ui->L_fileName->text());
if (nowDatas != "1")
{
ui->P_out->setPlainText(nowDatas);
}
else
{
QMessageBox::warning(this, "警告", "未檢索出資料", QMessageBox::Ok, QMessageBox::NoButton);
}
}
}
QFormDocF::~QFormDocF()
{
delete ui;
}
- QFormDocD:删除視窗
QFormDocD::QFormDocD(QWidget *parent) :
QWidget(parent),
ui(new Ui::QFormDocD)
{
ui->setupUi(this);
this->setFixedSize(520, 240);
connect(ui->B_sure, SIGNAL(clicked()), this, SLOT(on_sure_click()));
}
void QFormDocD::setFun(myFunction *fcn_d)
{
fcn = fcn_d;
}
void QFormDocD::on_sure_click()
{
if (ui->L_fileName->text() == "")
{
QMessageBox::warning(this, "未輸入警告請", "請輸入檔案名稱", QMessageBox::Ok, QMessageBox::NoButton);
}
bool a = fcn->deleteAll(ui->L_fileName->text());
if (a)
{
QMessageBox::information(this, "删除", "删除成功", QMessageBox::Ok, QMessageBox::NoButton);
}
else
{
QMessageBox::warning(this, "删除", "未檢索到比對資料", QMessageBox::Ok, QMessageBox::NoButton);
}
}
QFormDocD::~QFormDocD()
{
delete ui;
}
以上代碼都是一些視窗的初始化和信号與槽函數的綁定,以及槽函數的定義
下面來看功能類:
bool myFunction::InitQuery()
{
m_query = NULL;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("H:/Document/VS/secondQt/database/fileAttribute.db");
bool bret = db.open();
if (!bret)
{
return false;
}
QSqlDatabase db_operation = QSqlDatabase::database("sqlite3"); //打開并連接配接資料庫
m_query = new QSqlQuery(db_operation);
return m_query;
}
- InitQuery():打開并連接配接資料庫将資料庫位址傳回,此函數在myFunction的構造函數中調用。因為主視窗和操作主視窗都要通過myFunction類連結并操作資料庫,這樣初始化,可保證先連接配接過資料庫的視窗不會因為下一個視窗連接配接後無法在操作而導緻報錯。
void myFunction::establishDatabase()
{
m_query->exec("CREATE TABLE tableOne(fileName VARCHAR(100) ,filePath VARCHAR(10000) ,fileCreationTime VARCHAR(30),fileLastTime VARCHAR(30),fileSize TEXT)");
}
- entering(QString filePath):初始化資料庫表單
InitQuery()函數隻會建立和連結資料庫,首次使用前需要初始化表單
錄入檔案
void myFunction::entering(QString filePath)
{
QFileInfo finfo(filePath);
qint64 fileSize = finfo.size(); //檔案大小
QDateTime cTime = finfo.created(); // 建立時間
QString createnTime = cTime.toString("yyyy-MM-dd hh:mm:ss");
QDateTime lTime = finfo.lastModified(); //最後操作時間
QString lastAmendTime = lTime.toString("yyyy-MM-dd hh:mm:ss");
QString fileNameOne = finfo.baseName();
QString fileFormat = finfo.suffix();
QString fileName = fileNameOne + "." + fileFormat;//檔案名
QString sql = QString("INSERT INTO tableOne(fileName, filePath, fileCreationTime, fileLastTime, fileSize)VALUES('%1', '%2', '%3', '%4', '%5')")
.arg(fileName)
.arg(filePath)
.arg(createnTime)
.arg(lastAmendTime)
.arg(fileSize);
m_query->exec(sql);
}
- entering(QString filePath):錄入操作 QFileInfo類的熟悉與資料庫的增
//顯示更新
QString myFunction::refresh()
{
QString nowDatas = "";
QSqlQuery query = *m_query;
query.exec("SELECT *FROM tableOne");
while (query.next())
{
QString fileName = query.value(0).toString();
QString filePath = query.value(1).toString();
QString fileCreationTime = query.value(2).toString();
QString fileLastTime = query.value(3).toString();
QString fileSize = query.value(4).toString();
nowDatas.append("檔案名: ");
nowDatas.append(fileName);
nowDatas.append("\n檔案路徑: ");
nowDatas.append(filePath);
nowDatas.append("\n檔案建立時間: ");
nowDatas.append(fileCreationTime);
nowDatas.append("\n檔案最後操作時間: ");
nowDatas.append(fileLastTime);
nowDatas.append("\n檔案大小: ");
nowDatas.append(fileSize);
nowDatas.append("B\n\n");
}
return nowDatas;
}
- refresh():顯示資料庫資料
//修改
bool myFunction::modification(QList<QString> str)
{
QSqlQuery query = *m_query;
QString strOne = str.at(0);
QString name = str.at(1);
QString modificationDatas = str.at(2);
if (strOne == "0")
{
strOne = "fileName";
}
else
{
strOne = "fileSize";
}
query.exec("SELECT *FROM tableOne");
while (query.next())
{
QString strName = query.value(0).toString();
if (strName == name)
{
QString sql = QString("UPDATE tableOne SET '%1' ='%2' WHERE fileName = '%3' ")
.arg(strOne)
.arg(modificationDatas)
.arg(name);
query.exec(sql);
str.clear();
return true;
}
}
return false;
}
//删除
bool myFunction::deleteAll(QString name)
{
QSqlQuery query = *m_query;
query.exec("SELECT *FROM tableOne");
while (query.next())
{
QString fileName = query.value(0).toString();
if (fileName == name)
{
QString sql = QString("DELETE FROM tableOne WHERE fileName = '%1'")
.arg(name);
query.exec(sql);
return true;
}
}
return false;
}
//查找
QString myFunction::find(QString name)
{
QSqlQuery query = *m_query;
QString nowDatas = "";
query.exec("SELECT *FROM tableOne");
while (query.next())
{
QString fileName = query.value(0).toString();
if (fileName == name)
{
QString sql = QString("SELECT *FROM tableOne WHERE fileName = '%1'")
.arg(name);
query.exec(sql);
while (query.next())
{
QString fileName = query.value(0).toString();
QString filePath = query.value(1).toString();
QString fileCreationTime = query.value(2).toString();
QString fileLastTime = query.value(3).toString();
QString fileSize = query.value(4).toString();
nowDatas.append("檔案名: ");
nowDatas.append(fileName);
nowDatas.append("\n檔案路徑: ");
nowDatas.append(filePath);
nowDatas.append("\n檔案建立時間: ");
nowDatas.append(fileCreationTime);
nowDatas.append("\n檔案最後操作時間: ");
nowDatas.append(fileLastTime);
nowDatas.append("\n檔案大小: ");
nowDatas.append(fileSize);
nowDatas.append("B\n\n");
return nowDatas;
}
}
}
return "1";
}
- 資料庫操作(删改查)
以上就是本項目的主要功能與代碼,有問題請在評論區咨詢。
如需源碼請轉至:資源下載下傳中心