Qt6-网络关机助手(开机自启版)实现

本文详细介绍了使用Qt6开发一个网络关机助手的过程,包括设置程序图标、开机自启动、隐藏标题栏可移动、调用外部bat脚本、系统托盘显示以及获取计算机IP地址和名称等功能的实现。开发者通过源码和截图展示了每个功能的实现细节,邀请读者一起交流。

在前面已经预告了,要写一个网络关机助手了。这里先看下我的成果吧。

 

源码连接:https://download.csdn.net/download/z609932088/15422254

程序截图如下

目前这个小工具已经开发完成,欢迎小伙伴们一起交流。

 

设置程序图标

Qt6这里设置程序运行的图标又回到了以前的版本了。首先在工程文件夹下面建立一个rc文件,输入以下内容

 

IDI_ICON1               ICON    DISCARDABLE     "icon.ico"

准备一个ico格式的图片和这个文件放在一起。

 

在CMakeList文件中加入以下代码

 

set(PROJECT_SOURCES
        icon.rc
)

这样编译以后的程序就有图标了。

 

设置程序开机自启动

设置程序开机自启动需要用到写注册表,这里需要使用到管理员权限,在运行Qt Creator 的时候也要使用管理员运行。

便携开机启动的注册表代码,这里声明一下,这部分代码是抄的网上的,但是连接找不到了,对贡献者说声抱歉。

 

/**
 * @brief autoStart
 * 开机自启动
 */
void autoStart()
{
    QString appName = QApplication::applicationName();
    QString appPath = QApplication::applicationFilePath();
    appPath = appPath.replace("/","\\");
    QSettings *reg=new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
    QString val = reg->value(appName).toString();
    if(val != appPath)
        reg->setValue(appName,appPath);
    reg->deleteLater();
}

剩下就是在main函数中调用即可,如下

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    autoStart();
    MainWindow w;
    return a.exec();
}

 实现程序在隐藏标题栏情况下可移动

还是重写鼠标事件,就有部分接口不一样了。不过旧的依旧可以使用

/**
     * @brief mousePressEvent
     * @param event
     * 鼠标按下
     */
    void mousePressEvent(QMouseEvent* event);
    /**
     * @brief mouseMoveEvent
     * @param event
     * 鼠标移动
     */
    void mouseMoveEvent(QMouseEvent* event);
    /**
     * @brief mouseReleaseEvent
     * @param event
     * 鼠标释放
     */
    void mouseReleaseEvent(QMouseEvent* event);

 实现如下

static QPoint last(0,0);        //保存坐标
const int TITLE_HEIGHT = 50;    //这里也可以使用宏定义,保存标题高度,也就是鼠标点击区域的高度
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->position().y()<TITLE_HEIGHT)
    {
        last = event->globalPosition().toPoint();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->position().y()<TITLE_HEIGHT)
    {
        int dx = event->globalPosition().x() - last.x();
        int dy = event->globalPosition().y() - last.y();
        last = event->globalPosition().toPoint();
        this->move(this->x()+dx,this->y()+dy);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->position().y()<TITLE_HEIGHT)
    {
        int dx = event->globalPosition().x() - last.x();
        int dy = event->globalPosition().y() - last.y();
        this->move(this->x()+dx,this->y()+dy);
    }
}

 实现调用外部bat脚本文件

这里有一个坑需要注意的就是“QCoreApplication::applicationDirPath();”与之对应的还有一个currentPath()函数,这个在一般使用没有啥问题,与开机自启动功能结合使用的时候就会出现找不到文件的情况。这个大问题也是网上找到的,但是还是一样,参考连接找不到了。

 

void MainWindow::powerOffBat()
{
    QString strinfo;
    QProcess p(NULL);
    QString curPath = QCoreApplication::applicationDirPath();
    QString toolPath;
    toolPath = "/BAT";
    curPath.append(toolPath);
    p.setWorkingDirectory(curPath);
    toolPath = "/poweroff.bat";
    curPath.append(toolPath);
    p.start(curPath);
    if(p.waitForFinished())
    {
        qDebug() << "成功";
    }
    else
    {
        QMessageBox::warning(this,"警告","执行关机脚本失败\r\n请检查程序根目录下BAT文件中是否存在‘poweroff.bat’");
    }
}

 实现系统托盘显示功能

void MainWindow::initMySystemTrayIcon()
{
    /*
     * 设置系统托盘内容
     */
    m_trayIcon = new QSystemTrayIcon(this);
    m_trayIcon->setIcon(QIcon(":/images/images/logo.ico"));
    m_trayIcon->setToolTip("关机助手");
    m_trayIcon->show();
    connect(m_trayIcon,&QSystemTrayIcon::activated,this,[=](QSystemTrayIcon::ActivationReason temp){
        switch (temp) {
        case QSystemTrayIcon::Trigger:
        {
            //单击图标时间
            break;
        }
        case QSystemTrayIcon::DoubleClick:
        {
            if(this->isHidden())
            {
                this->showNormal();
            }
            else
            {
                this->hide();
            }
            break;
        }
        }
    });
    initMySystemTrayIconAction();
    initMySystemTrayIconMenu();
//    m_trayIcon->showMessage("Tip","PowerControl is running",QSystemTrayIcon::MessageIcon::Information,3);
}

void MainWindow::initMySystemTrayIconAction()
{
    m_showWindowAction = new QAction(QIcon(":/images/images/logo.ico"),"显示界面",this);
    connect(m_showWindowAction,&QAction::triggered,this,[=](){this->show();});
    m_exitAppAction = new QAction(QIcon(":/images/images/exit.ico"),"退出程序",this);
    connect(m_exitAppAction,&QAction::triggered,this,[=](){this->close();});
    m_powerOffAppAction = new QAction(QIcon(":/images/images/logo.ico"),"一键关机",this);
    connect(m_powerOffAppAction,&QAction::triggered,this,&MainWindow::on_pushButton_poweroff_clicked);
}

void MainWindow::initMySystemTrayIconMenu()
{
    m_trayIconMenu = new QMenu(this);
    m_trayIconMenu->addAction(m_powerOffAppAction);
    m_trayIconMenu->addSeparator();
    m_trayIconMenu->addAction(m_showWindowAction);
    m_trayIconMenu->addAction(m_exitAppAction);
    m_trayIcon->setContextMenu(m_trayIconMenu);
}

 实现获取计算机IP地址和名称功能

void MainWindow::getSystemInfor()
{
    m_systemName = QHostInfo::localHostName();
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                ipAddressesList.at(i).toIPv4Address()) {
            m_systemIp = ipAddressesList.at(i).toString();
            break;
        }
    }
    if (m_systemIp.isEmpty())
        m_systemIp = QHostAddress(QHostAddress::LocalHost).toString();
    ui->label_sysInfor->setText(QString("设备名称:%1\r\nI P 地址:%2").arg(m_systemName).arg(m_systemIp));
}

 

 

 

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamLife.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值