A simple Qt dictionary program

      C language programs are known for low-level system programming, so they are often used in embedded system and operating system programming, while C++ is known for GUI programs (compatible with C programs is its unique advantage). To be honest, writing non-GUI programs in C language to writing GUI programs in C++ is a challenge for many beginners, a small leap, using Qt to write a simple dictionary translation program can be said to be a good one instance.

    Algorithm design: use the map associative container in C++ STL. For the usage of map, please refer to https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html, which corresponds to the QMap class in Qt, with English input as the key, The Chinese translation is used as a value, thus forming a key-value pair. First, read the translation entry from the file into the map container, and then query according to the input word (enter query).

   Interface design: Design a class that inherits QWidget, use the QLineEdit control as the input box, use the QLabel control as the translation display area, and use the QLabel to display a picture to beautify the interface. The three are vertically arranged and automatically positioned through QVboxLayout.

   The whole project consists of three Qt files, widget.h, widget.cpp, main.cpp, the specific codes are as follows:

  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>
 //Open the dictionary file and read the contents of the file 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();
}

  The following is a screenshot of the program, the source file and program can be found at https://pan.baidu.com/s/1Hw2o9bQGQHRm_1tJlEGkfg

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324991018&siteId=291194637