zoukankan      html  css  js  c++  java
  • 一个简单的Qt词典程序

          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 "stdio.h"
    #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] = '';
            inter[strlen(inter) - 1] = '';
            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/1xFurnfisySfM9SnhyPwf0w 提取码: c9qq

  • 相关阅读:
    Redhat6.4安装MongoDBv3.6.3
    windows模糊查询指定进程是否存在
    Linux普通用户不能使用TAB键、上下键
    零基础Python爬虫实现(百度贴吧)
    零基础Python爬虫实现(爬取最新电影排行)
    让bat批处理后台运行,不显示cmd窗口(完全静化)
    根据进程名监控进程(邮件提醒)
    android 开发中,必须要注意的知识点. (持续更新)
    Android上传文件至服务器
    为应用添加多个Activity与参数传递
  • 原文地址:https://www.cnblogs.com/yangjd/p/8967679.html
Copyright © 2011-2022 走看看