在前面已经预告了,要写一个网络关机助手了。这里先看下我的成果吧。
源码连接: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));
}


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

被折叠的 条评论
为什么被折叠?



