码迷,mamicode.com
首页 > 其他好文 > 详细

一个简单的Qt词典程序

时间:2018-04-28 15:46:17      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:内容   let   词典   ota   sig   line   pen   html   void   

      C语言程序以低层系统编程见长,因此常用于嵌入式系统和操作系统编程,而C++则以GUI程序见长(兼容C程序是它的独特优点)。说实话从使用C语言编写非GUI程序到使用C++编写GUI程序对很多初学者来说都是一个挑战,一个小小的飞跃,使用Qt来编写一个简单的词典翻译程序可以说是一个很好的实例。

    算法设计:使用C++ STL中的map关联容器,map的用法请参阅https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html,对应到Qt中就是QMap类,英文输入作为键,中文翻译作为值,这样组成了一个键值对。首先从文件中读取翻译词条到map容器中,再根据输入的词语查询(回车查询),如果查询到了则输出中文翻译,没有查询到则输出"Not found".

   界面设计:设计一个继承QWidget的类,使用QLineEdit控件作为输入框,使用QLabel控件作为翻译显示区,使用QLabel显示一幅图片以美化界面,三者通过QVboxLayout垂直排列并自动定位。

   整个项目由三个Qt文件构成,分别为widget.h, widget.cpp, main.cpp,具体代码如下:

  widget.h

#ifndef WIDGET_H
#define WIDGET_H
//widget.h

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMap>
#include <QString>

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void translate();
    
private:
    QMap<QString, QString> mapDict;
    QString query;
    QLabel *label_output;
    QLabel *label_image;
    QLineEdit *lineEdit;
    void CreateDict(QMap<QString, QString>  *myDict);
};
#endif // WIDGET_H

widget.cpp

// widget.cpp

#include "widget.h"
#include <QTextCodec>
#include <QVBoxLayout>
#include <QMessageBox>

//打开字典文件,并读取文件内容
void Widget::CreateDict(QMap<QString, QString>  *myDict) {
    FILE *fp;
    char word[300], inter[300];
    size_t wordNumber = 0;
    fp = fopen("raw-dict", "r");
    if (!fp) {
        QMessageBox::information(this,
            tr("打开词库失败"), 
            tr("打开词库失败!"));
        fclose(fp);
        return;
    } 
    while (fgets(word, sizeof(word), fp) && fgets(inter, sizeof(word), fp)) {
        /*
         * 插入到字典中。
         */
        word[strlen(word) - 1] = ‘\0‘;
        inter[strlen(inter) - 1] = ‘\0‘;
        wordNumber++;                                             
        (*myDict)[word]=inter;
    }
    fclose(fp);
    label_output->setText("*****  Total number of words is "+QString::number(wordNumber)+"  *****");
}

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    /*QString filename("Tulips.jpg");
    QImage *img = new QImage;
    if (!(img->load(filename)))         // 加载图像
    {
        QMessageBox::information(this,
            tr("打开图像失败"), 
            tr("打开图像失败!"));
        delete img;
        return;
    }*/
    
    QPixmap img("Tulips.jpg");
    
    label_output = new QLabel; 
    label_output->setWordWrap(true);
    lineEdit = new QLineEdit;
    label_image = new QLabel;
    label_image->setAlignment(Qt::AlignCenter);
    //label_image->setPixmap(QPixmap::fromImage(img));
    label_image->setPixmap(img);
    
    
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(lineEdit);
    layout->addWidget(label_output);
    layout->addWidget(label_image);
    setLayout(layout);
    
    connect(lineEdit,SIGNAL(returnPressed()),
        this,SLOT(translate()));
    
    CreateDict(&mapDict);
}

Widget::~Widget()
{
}

void Widget::translate()
{
   query = lineEdit->text();
   if(mapDict.find(query) != mapDict.end())
   {
       label_output->setText(mapDict[query]);
   }
   else
   {
      label_output ->setText("Not found");
   }
}

main.cpp

//main.cpp

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.setFixedSize(320,240);
    w.show();
    return a.exec();
}

  下面为程序截图,源文件和程序参见https://pan.baidu.com/s/1Hw2o9bQGQHRm_1tJlEGkfg

技术分享图片

 

一个简单的Qt词典程序

标签:内容   let   词典   ota   sig   line   pen   html   void   

原文地址:https://www.cnblogs.com/yangjd/p/8967679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!