QT下C语言开发快译通
dict.txt以及软件成品可在我的CSDN下载中获取
#-------------------------------------------------
#
# Project created by QtCreator 2019-11-18T12:56:52
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Dictionary
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
RC_ICONS += app.ico
RESOURCES +=
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QString>
#include <QDebug>
#include <QDir>
#include <QTextCodec>
#define WORDMAX 111104
struct dicts
{
char *word;
char *trans;
};
typedef struct dicts dic;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
dic *p = NULL;
};
#endif // MAINWINDOW_H
#include “mainwindow.h”
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QDir::setCurrent(a.applicationDirPath());
w.show();
return a.exec();
}
mainwindow.cpp
```c
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString pa = QDir::currentPath();
char *pat = codec->fromUnicode(pa).data();
const char *path = "/dict.txt";
strcat(pat, path);
p = (dic*)malloc(sizeof(dic) * WORDMAX);
FILE *fp = fopen(pat, "r");
if (!fp)
{
printf("文件打开失败!\n");
return ;
}
int i = 0;
char buf[1024];
while (!feof(fp))
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
buf[strlen(buf) - 1] = '\0';
p[i].word = (char*)malloc(strlen(buf) + 1);
memset(p[i].word, 0, strlen(buf) + 1);
strcpy(p[i].word, &buf[1]);
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
p[i].trans = (char*)malloc(strlen(buf) + 1);
memset(p[i].trans, 0, strlen(buf) + 1);
strcpy(p[i].trans, &buf[6]);
i++;
}
fclose(fp);
}
MainWindow::~MainWindow()
{
delete ui;
for (int i = 0; i < WORDMAX; i++)
{
free(p[i].word);
free(p[i].trans);
}
free(p);
p = NULL;
}
void MainWindow::on_pushButton_clicked()
{
QString content = ui->textEdit->toPlainText().trimmed();
if(content.isEmpty())
{
ui->labelShow->setText("请输入要查找的单词!\n");
return;
}
QTextCodec *codec = QTextCodec::codecForName("GBK");
char * con = codec->fromUnicode(content).data();
for(int i = 0; i < WORDMAX; i++)
{
if(!strcmp(con, p[i].word))
{
ui->labelShow->setText(codec->toUnicode(p[i].trans));
return;
}
}
ui->labelShow->setText("未找到该单词解释!\n");
}
mainwindow.ui
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString pa = QDir::currentPath();
char *pat = codec->fromUnicode(pa).data();
const char *path = "/dict.txt";
strcat(pat, path);
p = (dic*)malloc(sizeof(dic) * WORDMAX);
FILE *fp = fopen(pat, "r");
if (!fp)
{
printf("文件打开失败!\n");
return ;
}
int i = 0;
char buf[1024];
while (!feof(fp))
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
buf[strlen(buf) - 1] = '\0';
p[i].word = (char*)malloc(strlen(buf) + 1);
memset(p[i].word, 0, strlen(buf) + 1);
strcpy(p[i].word, &buf[1]);
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
p[i].trans = (char*)malloc(strlen(buf) + 1);
memset(p[i].trans, 0, strlen(buf) + 1);
strcpy(p[i].trans, &buf[6]);
i++;
}
fclose(fp);
}
MainWindow::~MainWindow()
{
delete ui;
for (int i = 0; i < WORDMAX; i++)
{
free(p[i].word);
free(p[i].trans);
}
free(p);
p = NULL;
}
void MainWindow::on_pushButton_clicked()
{
QString content = ui->textEdit->toPlainText().trimmed();
if(content.isEmpty())
{
ui->labelShow->setText("请输入要查找的单词!\n");
return;
}
QTextCodec *codec = QTextCodec::codecForName("GBK");
char * con = codec->fromUnicode(content).data();
for(int i = 0; i < WORDMAX; i++)
{
if(!strcmp(con, p[i].word))
{
ui->labelShow->setText(codec->toUnicode(p[i].trans));
return;
}
}
ui->labelShow->setText("未找到该单词解释!\n");
}