QT implements clicking the button to call up another exe program, and clicking the close button to close the main window.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QString>
#include <QColor>
#include <QGraphicsDropShadowEffect>
#include <QApplication>
#include <QSettings>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    isMainWindowClosed(false)
{

    // 读取配置文件中的可执行文件路径
    QSettings settings("config.ini", QSettings::IniFormat);
    QString exePath = settings.value("ExecutableFilePath").toString();

    // 存储可执行文件路径到成员变量
    m_executableFilePath = exePath;

    // 将按钮的颜色更改为蓝色
    ui->setupUi(this);

    //设置字体颜色
    QColor skyBlue(96, 155, 222);  // 天蓝色的RGB值
    QString styleSheet = QString("color: %1;").arg(skyBlue.name());

    //ui->pushButton->setStyleSheet(styleSheet);
    ui->lb->setStyleSheet(styleSheet);
    setWindowTitle("测试网络构建子系统");

    // 设置按钮的样式表
    QString buttonStyleSheet =
                         "    QPushButton {"
                         "    background-color: #f2f2f2;"
                         "    border: 2px solid rgb(96,155,222);"
                         "    border-radius: 5px;"
                         "    padding: 5px 10px;"
                         "    color: rgb(96,155,222);"
                         "}"
                         "    QPushButton:hover {"
                         "    border-color: #007acc;"
                         "    color: #007acc;"
                         "}";

    ui->pushButton->setStyleSheet(buttonStyleSheet);
    ui->pushButton_2->setStyleSheet(buttonStyleSheet);
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(8); // 阴影模糊半径
    shadowEffect->setColor(QColor(0, 122, 204)); // 阴影颜色
    shadowEffect->setOffset(0, 0); // 阴影偏移量
    ui->pushButton->setGraphicsEffect(shadowEffect);
    QGraphicsDropShadowEffect *shadowEffect1 = new QGraphicsDropShadowEffect(this);
    shadowEffect1->setBlurRadius(8); // 阴影模糊半径
    shadowEffect1->setColor(QColor(0, 122, 204)); // 阴影颜色
    shadowEffect1->setOffset(0, 0); // 阴影偏移量
    ui->pushButton_2->setGraphicsEffect(shadowEffect1);

    connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    // 创建一个进程对象
    QProcess *process = new QProcess(this);

    // 要调用的可执行文件路径
    //QString exePath = "  ";

    // 获取成员变量中存储的可执行文件路径
    QString exePath = m_executableFilePath;

    // 启动可执行文件
    process->start(exePath);

    // 等待进程完成
    process->waitForFinished();

    // 删除进程对象
    delete process;

    // 主窗口并标记为关闭
    isMainWindowClosed = true;
    //close();
}

void MainWindow::on_pushButton_2_clicked()
{

    // 关闭主窗口并标记为已关闭
    isMainWindowClosed = true;
    close();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    // 如果主窗口已关闭,则不再执行打开文件的操作
    if (isMainWindowClosed)
        return;

    // 其他关闭事件的处理...

    QMainWindow::closeEvent(event);
}

This article is an original note and is for reference only.

Guess you like

Origin blog.csdn.net/weixin_68583698/article/details/132187440