[領卓教育]使用QT實現一個簡單的離線詞典
阿新 • • 發佈:2018-11-01
效果展示
首先登入介面:
查詞介面:
程式碼區
部分重要程式碼:
- 登陸(註冊)部分:
1). 資料庫的開啟: ` //資料庫 db = QSqlDatabase::addDatabase("QSQLITE") ; db.setDatabaseName("user.db"); if(!db.open()) { QMessageBox::critical(this,tr("開啟錯誤"),tr("開啟資料庫失敗")); } QSqlQuery query ; QStringList table = db.tables(QSql::Tables) ; qDebug()<<"table:"<<table ; if( !table.contains("user") ) { QString sql = tr("create table user (name text primary key,passwd text);"); qDebug()<<"sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("User"),tr("Create Error")) ; } } if( !table.contains("record") ) { QString sql = tr("create table record (name text,word text,mean text);"); qDebug()<<"sql_pic :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("User"),tr("Create Error")) ; } }`:
2). 註冊與登陸: void app::on_pushButton_register_clicked()//註冊一個使用者 { //qDebug()<<ui->lineEdit_pwd->text(); /********從lineedit獲得使用者名稱和密碼***********/ this->name = ui->lineEdit_num->text(); this->passwd = ui->lineEdit_pwd->text(); /********在資料庫中檢索使用者名稱是否存在***********/ QString sql = tr("select * from user where name ='%0';").arg(name); QSqlQuery query; qDebug()<<"register sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("Register"),tr("Register select failed")) ; } /***************判斷使用者名稱是否存在************/ /*******不存在則儲存這個使用者資訊到資料庫*******/ if(query.next()) { QMessageBox::warning(NULL,tr("Register"),tr("\n 使用者名稱已存在 \n")) ; } else { sql = tr("insert into user values('%0','%1');").arg(name).arg(passwd); qDebug()<<"register sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("Register"),tr("Register insert failed")) ; } } } void app::on_pushButton_login_clicked()//登入 { /********從lineedit獲得使用者名稱和密碼***********/ this->name = ui->lineEdit_num->text(); this->passwd = ui->lineEdit_pwd->text(); /***********在資料庫中檢索使用者資訊***********/ QString sql = tr("select * from user where name ='%0' ;").arg(name); QSqlQuery query; qDebug()<<"register sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("Register"),tr("login select failed")) ; } if(query.next()) { /*****此時在資料庫中檢測到使用者名稱*****/ /********登入密碼是否正確**********/ /**********正確則登陸成功**********/ /**********錯誤則登陸失敗**********/ if(query.value("passwd").toString() == passwd) { qDebug()<<"true" ; QMessageBox::warning(NULL,tr("Login"),tr("登陸成功 ")) ; word_t * ww = new word_t; ww->name = this->name; ww->show(); this->hide(); db.close(); } else { qDebug()<<"false" ; QMessageBox::warning(NULL,tr("Login"),tr("密碼錯誤 ")) ; } } else { /********使用者不存在**********/ QMessageBox::warning(NULL,tr("Login"),tr("使用者名稱不存在 ")) ; } }
- 查詞部分:
void word_t::on_pushButton_Query_clicked() { //qDebug()<<ui->lineEdit_Word->text(); /*********每查詢一次自動清空顯示區**********/ ui->textEdit_mean->clear(); this->word = ui->lineEdit_Word->text(); /*********開啟詞典(dict)檔案**********/ QFile * fil ; fil = new QFile("./word/dict.txt"); fil->open(QIODevice::ReadOnly|QIODevice::Text); char buf[1024]; char newword[100]; strcpy(newword,word.toUtf8().data()); strcat(newword," "); /*********每次從dict檔案中讀一行資料與輸入單詞進行比較**********/ while(fil->readLine(buf,sizeof(buf))!=-1) { //qDebug()<<"mean :"<<buf ; /*********查到單詞則跳出迴圈**********/ if(strncmp(newword,buf,strlen(newword))==0) { ui->textEdit_mean->setText(QString(buf)); break; } } /*********讀到檔案末尾仍未檢索到單詞**********/ /******則單詞不存在,跳出此函式,不繼續執行******/ if(ui->textEdit_mean->toPlainText().isEmpty()) { QMessageBox::warning(NULL,tr("Query"),tr("未找到")) ; return; } /******單詞存在則儲存到資料庫(查詢歷史)******/ this->mean = ui->textEdit_mean->toPlainText(); QString sql = tr("select * from record;"); QSqlQuery query; qDebug()<<"query sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("Query"),tr("Query select failed")) ; } sql = tr("insert into record values('%0','%1','%2');").arg(name).arg(word).arg(mean); qDebug()<<"register sql :"<<sql ; if(! query.exec(sql)) { QMessageBox::warning(NULL,tr("Register"),tr("Register insert failed")) ; }
到這裡已經可以實現查詞功能,具體美化可以自定義一個標題欄(
包括重寫滑鼠事件).