[领卓教育]使用QT实现一个简单的离线词典

效果展示

首先登录界面:

登录界面展示

查词界面:

查询

代码区

部分重要代码:

  1. 登陆(注册)部分:
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("用户名不存在     ")) ;
   	 }
}	
  1. 查词部分:
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")) ;
    }

到这里已经可以实现查词功能,具体美化可以自定义一个标题栏(
包括重写鼠标事件).

可以使用Miniblink的Web控件来加载百度离线地图,以下是示例代码: ```c++ #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <qt_windows.h> #include <Miniblink.h> #pragma comment(lib, "Miniblink.lib") class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { QWidget* centralWidget = new QWidget(this); QVBoxLayout* layout = new QVBoxLayout(centralWidget); layout->setMargin(0); layout->setSpacing(0); this->setCentralWidget(centralWidget); m_webView = mbCreateWebView(MB_WINDOW_TYPE_POPUP, centralWidget, 0, 0, 800, 600); mbSetHandle(m_webView, reinterpret_cast<void*>(centralWidget->winId())); mbLoadHtml(m_webView, "<html><body><div id='map' style='width:800px;height:600px;'></div><script type='text/javascript' src='http://api.map.baidu.com/getscript?v=2.0&ak=your_ak'></script><script type='text/javascript'>var map = new BMap.Map('map');map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);</script></body></html>", "http://localhost"); layout->addWidget(reinterpret_cast<QWidget*>(m_webView)); } ~MainWindow() { mbDestroyWebView(m_webView); } private: MbWebView* m_webView; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); mbInit(); MainWindow w; w.show(); int ret = a.exec(); mbUninit(); return ret; } #include "main.moc" ``` 其中,`MB_WINDOW_TYPE_POPUP`参数表示创建一个弹出式窗口,`mbSetHandle`函数用于设置Web控件的Win32句柄,`mbLoadHtml`函数用于加载百度离线地图的HTML代码,需要将`your_ak`替换为自己申请的百度地图AK。 注意:需要在Qt的.pro文件中添加以下内容: ```qmake LIBS += -L"path/to/Miniblink/lib" -lMiniblink INCLUDEPATH += "path/to/Miniblink/include" ``` 其中,`path/to`需要替换为实际的Miniblink库文件和头文件的路径。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值