之前写的哈夫曼压缩,想着加上界面,写了一天一个蹩脚的界面出来了。现在越来越喜欢Qt了,比MFC简单多了。
有些关于编译运行问题到文章末尾讲,先上源码。(推荐一个学Qt的网址点击打开链接)
这是定义hfcom的头文件
#ifndef HFCOM_H
#define HFCOM_H
#include <QtWidgets/QMainWindow>
#include "ui_hfcom.h"
#include <qpushbutton.h>
#include <qobject.h>
#include <Windows.h>
#include <qstring.h>
#include <qfiledialog.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qstatusbar.h>
#include <qlabel.h>
#include <qicon.h>
#include "hf.h"
class hfcom : public QMainWindow
{
Q_OBJECT
public:
hfcom(QWidget *parent = 0);
~hfcom();
private:
QPushButton *button_open1;
QLineEdit *path1;
QPushButton *button_compress;
QString *compressed_filename;
QStatusBar *ps;
void compress();
void open_compressed_file();
QPushButton *button_open2;
QLineEdit *path2;
QPushButton *button_decompress;
QString *decompressed_filename;
void decompress();
void open_decompressed_file();
};
#endif // HFCOM_H
这是hfcom.cpp界面的实现都写在这里面了
#include "hfcom.h"
hfcom::hfcom(QWidget *parent) : QMainWindow(parent)
{
this->resize(400, 247);
this->setMinimumSize(450, 278);
this->setMaximumSize(450, 278);
this->setWindowIcon(QIcon("hflog.ico"));
ps = new QStatusBar(this);
ps->setGeometry(0,241,450,37);
ps->showMessage(QString::fromLocal8Bit("ps:不支持中文路径和文件名"));
button_open1 = new QPushButton(QString::fromLocal8Bit("打开文件"), this);
button_open1->setGeometry(20, 40, 80, 37);
path1 = new QLineEdit(this);
path1->setReadOnly(true);
path1->setGeometry(110, 40, 200, 37);
button_compress = new QPushButton(QString::fromLocal8Bit("压缩"), this);
button_compress->setGeometry(350, 40, 80, 37);
button_open2 = new QPushButton(QString::fromLocal8Bit("打开文件"), this);
button_open2->setGeometry(20, 140, 80, 37);
path2 = new QLineEdit(this);
path2->setReadOnly(true);
path2->setGeometry(110, 140, 200, 37);
button_decompress = new QPushButton(QString::fromLocal8Bit("解压"), this);
button_decompress->setGeometry(350, 140, 80, 37);
QObject::connect(button_compress, &QPushButton::clicked, this, &hfcom::compress);
QObject::connect(button_decompress, &QPushButton::clicked, this, &hfcom::decompress);
QObject::connect(button_open1, &QPushButton::clicked, this, &hfcom::open_compressed_file);
QObject::connect(button_open2, &QPushButton::clicked, this, &hfcom::open_decompressed_file);
//ui.setupUi(this);
}
hfcom::~hfcom()
{
}
void hfcom::compress()
{
if( Compress((compressed_filename->toStdString()).c_str()) == true)
QMessageBox::information(this, tr("Information"), QString::fromLocal8Bit("压缩成功"));
else
QMessageBox::information(this, tr("Information"), QString::fromLocal8Bit("压缩失败"));
}
void hfcom::decomp