基于Qt实现的算法可视化(棋盘覆盖、汉诺塔、旅行商)

学校开设了相关的课程,要做算法可视化。由于之前只接触过C系列和一点点Python,就选了Qt+C来实现。实际上,Qt的ui界面设计是非常方便的,但是课设时间短,任务重,直接自己手敲了,没有对ui设计进行深入的学习。自己根据难易程度选择了棋盘覆盖、汉诺塔和旅行商三个问题,实现的算法分别是分治、递归还有贪心。

棋盘覆盖篇:基于Qt实现的算法可视化(棋盘覆盖)
汉诺塔篇:基于Qt实现的算法可视化(汉诺塔)
旅行商篇:基于Qt实现的算法可视化(旅行商)

此篇给出主界面的实现:

直接给出代码,思路见注释。

头文件:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QPushButton>
#include "hanoi.h"
#include "tsp.h"
#include "chesscover.h"

class mainwidget : public QWidget
{
    Q_OBJECT

public:
    mainwidget(QWidget *parent = 0);
    ~mainwidget();

    //槽函数
    void change_to_hanoi();     //进入汉诺塔问题窗口
    void return_from_hanoi();   //从汉诺塔问题窗口返回
    void change_to_tsp();       //进入旅行商问题窗口
    void return_from_tsp();     //从旅行商问题窗口返回
    void change_to_cover();     //进入棋盘覆盖问题
    void return_from_cover();   //从棋盘覆盖问题窗口返回
private:
    //b1为“汉诺塔问题”,b2为“旅行商问题”,b3为“棋盘覆盖问题”,b4为“退出程序”
    QPushButton b1,b2,b3,b4;

    //汉诺塔、旅行商、棋盘覆盖三个子窗口
    hanoi han;
    tsp vis;
    chesscover chess;
};

#endif // MAINWIDGET_H

cpp文件:

#include "mainwidget.h"
#include <QFont>

mainwidget::mainwidget(QWidget *parent)
    : QWidget(parent)
{
    //窗口设置
    setWindowTitle("算法可视化程序");  //设置窗口名字
    resize(1000,550);                  //设置窗口大小

    //按钮设置
    b1.setParent(this); b2.setParent(this); b3.setParent(this); b4.setParent(this);     //设置父类
    QFont font("楷体", 20, QFont::Medium);                                              //设置字体
    b1.setFont(font); b2.setFont(font); b3.setFont(font); b4.setFont(font);
    b1.setText("汉诺塔问题"); b2.setText("旅行商问题");                                 //设置按钮内容
    b3.setText("棋盘覆盖问题"); b4.setText("退出程序");
    b1.move(120, 150); b2.move(410, 150); b3.move(700, 150); b4.move(410, 400);         //设置按钮位置
    b1.resize(180, 180); b2.resize(180, 180); b3.resize(180, 180); b4.resize(180, 60);  //设置按钮大小
    //设置按钮样式
    b1.setStyleSheet("QPushButton{border:2px groove gray;border-radius:90px;padding:2px 4px;font:25px;border-style:outset;}" // 按键本色
                     "QPushButton:hover{background-color:white;color:black;}"  // 鼠标停放时的色彩
                     "QPushButton:pressed{background-color:rgba(255,255,255,30); border-style: inset; }");//鼠标按下
    b2.setStyleSheet("QPushButton{border:2px groove gray;border-radius:90px;padding:2px 4px;font:25px;border-style:outset;}" // 按键本色
                     "QPushButton:hover{background-color:white;color:black;}"  // 鼠标停放时的色彩
                     "QPushButton:pressed{background-color:rgba(255,255,255,30); border-style: inset; }");//鼠标按下
    b3.setStyleSheet("QPushButton{border:2px groove gray;border-radius:90px;padding:2px 4px;font:25px;border-style:outset;}" // 按键本色
                     "QPushButton:hover{background-color:white;color:black;}"  // 鼠标停放时的色彩
                     "QPushButton:pressed{background-color:rgba(255,255,255,30); border-style: inset; }");//鼠标按下
    b4.setStyleSheet("QPushButton{border:2px groove gray;border-radius:20px;padding:2px 4px;font:15px;border-style:outset;}" // 按键本色
                     "QPushButton:hover{background-color:white;color:black;}"  // 鼠标停放时的色彩
                     "QPushButton:pressed{background-color:rgba(255,255,255,30); border-style: inset; }");//鼠标按下
    b1.show();b2.show();b3.show();b4.show();                                        //设置按钮在父类窗口显示

    //连接信号和槽函数
    connect(&b1, &QPushButton::clicked, this, &mainwidget::change_to_hanoi);           //进入汉诺塔问题窗口
    connect(&han, &hanoi::return_sign, this, &mainwidget::return_from_hanoi);          //从汉诺塔问题窗口返回
    connect(&b2, &QPushButton::clicked, this, &mainwidget::change_to_tsp);             //进入旅行商问题窗口
    connect(&vis, &tsp::return_sign, this, &mainwidget::return_from_tsp);              //从旅行商问题窗口放回
    connect(&b3, &QPushButton::clicked, this, &mainwidget::change_to_cover);           //进入棋盘覆盖问题窗口
    connect(&chess, &chesscover::return_sign, this, &mainwidget::return_from_cover);   //从棋盘覆盖问题窗口返回
    connect(&b4, &QPushButton::clicked, this, &mainwidget::close);                     //退出程序
}

//槽函数实现
void mainwidget::change_to_hanoi() {han.show();hide();}     //汉诺塔显示,主窗口隐藏
void mainwidget::change_to_tsp() {vis.show();hide();}       //旅行商显示,主窗口隐藏
void mainwidget::change_to_cover() {chess.show();hide();}   //棋盘覆盖显示,主窗口隐藏
void mainwidget::return_from_hanoi() {han.hide();show();}   //汉诺塔隐藏,主窗口显示
void mainwidget::return_from_tsp() {vis.hide();show();}     //旅行商隐藏,主窗口显示
void mainwidget::return_from_cover() {chess.hide();show();} //棋盘覆盖隐藏,主窗口显示

mainwidget::~mainwidget()
{

}

最终效果:

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值