Qt6 程序开机自启找不到配置文件及外部程序
【摘要】
欢迎来到我的博客,希望这篇文章对你有所帮助,如果觉得不错,请点赞搜藏哈。
1. 场景描述
还是用开机助手这个软件来举例,软件在编译打包完成后,正常启动后,可以正常读取到与其同级目录下BAT文件...
欢迎来到我的博客,希望这篇文章对你有所帮助,如果觉得不错,请点赞搜藏哈。
1. 场景描述
还是用开机助手这个软件来举例,软件在编译打包完成后,正常启动后,可以正常读取到与其同级目录下BAT文件中的关机脚本的,如下图
经过测试,是没有问题,但是呢,当我们把程序设置为开机启动,在程序开机自启动后,无法读取程序的关机脚本。
2. 查找原因
2.1 权限原因
在开始部署的时候,把程序安装到了C盘,开始怀疑是不是因为给的权利不够,在给了全部权限后,还是不可以。排除权限问题。
2.2 在部署目录运行
在部署目录手动启动,程序可以正常执行,可以读取到关机脚本。
2.3 文件路径问题
经过百度后,才明白,如果我们把程序设置为开机启动后,因为开机启动的指令其实也是CMD的指令,所以,如果我们在代码中使用以下代码获取程序路径,其实获取到的是CMD程序的运行路径,也就是“C:\Windows\System32”,所以会发现找不到文件
QString appPath = QApplication::applicationFilePath();
- 1
3. 解决方法
解决方案也很简单,就是把获取程序路径的代码修改一下,如下:
QString curPath = QCoreApplication::applicationDirPath();
- 1
问题解决
附上源码
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("网络关机助手");
this->setWindowFlag(Qt::FramelessWindowHint);
ui->label_mainBack->setStyleSheet("QLabel{border-image:url(:/images/images/mainBack.png);}");
ui->label_logo->setStyleSheet("QLabel{border-image:url(:/images/images/logo_W.png);}");
ui->pushButton_poweroff->setToolTip("关闭所有在线计算机");
ui->pushButton_close->setToolTip("最小化到任务栏");
ui->pushButton_poweroff->setStyleSheet("QPushButton{border-image:url(:/images/images/pw_n.png);}QPushButton:hover{border-image:url(:/images/images/pw_h.png);}QPushButton:pressed{border-image:url(:/images/images/pw_p.png);}");
ui->label_title->setText("网络关机助手");
readConfig();
initUDPSocket();
initMySystemTrayIcon();
initGetTimeTimer();
getSystemInfor();
}
MainWindow::~MainWindow()
{
if(m_getTimeTimer)
{
m_getTimeTimer->stop();
delete m_getTimeTimer;
m_getTimeTimer = nullptr;
}
delete ui;
}
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::sentConmad()
{
m_Socket->writeDatagram(QString::number(1,16).toLatin1(),QHostAddress::Broadcast,8088);
}
void MainWindow::on_pushButton_poweroff_clicked()
{
/*
* 关机指令需要测试,UDP广播应该是自己也可以收到,所以这里应该不需要单独执行关机指令
*/
sentConmad();
}
void MainWindow::getUDPDate()
{
while (m_Socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_Socket->pendingDatagramSize());
m_Socket->readDatagram(datagram.data(),datagram.size());
if(datagram.toInt() == 0x01)
{
if(this->isHidden())
{
this->show();
}
powerOffBat();
}
}
}
void MainWindow::on_pushButton_close_clicked()
{
// this->close(); //最后注释
this->hide();
}
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);
}
void MainWindow::initUDPSocket()
{
m_Socket = new QUdpSocket();
m_Socket->bind(8088,QUdpSocket::ShareAddress);
connect(m_Socket,&QUdpSocket::readyRead,this,&MainWindow::getUDPDate);
}
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);
}
}
QString MainWindow::getTime(QString format)
{
QTime mTime = QTime::currentTime();
if(ui->checkBox_isTimerOff->isChecked())
if(mTime.hour() == mTimeOff.hour() && mTime.minute() == mTimeOff.minute())
{
ui->checkBox_isTimerOff->setChecked(false);
QSettings *config = new QSettings("CONFIG.ini",QSettings::IniFormat);
config->setValue("TimerOff/HH",mTime.hour());
config->setValue("TimerOff/MM",mTime.minute());
sentConmad();
}
return mTime.toString(format);
}
void MainWindow::initGetTimeTimer()
{
if(!m_getTimeTimer)
{
m_getTimeTimer = new QTimer(this);
}
connect(m_getTimeTimer,&QTimer::timeout,this,[=](){
ui->pushButton_close->setText(getTime("hh:mm"));
});
m_getTimeTimer->start(500);
}
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));
}
void MainWindow::readConfig()
{
QString curPath = QCoreApplication::applicationDirPath();
curPath.append("/CONFIG.ini");
QSettings *config = new QSettings(curPath,QSettings::IniFormat);
ui->timeEdit_off->setTime(QTime(config->value("TimerOff/HH").toInt(),config->value("TimerOff/MM").toInt(),0,0));
if(config->value("TimerOff/FLAG").toInt())
{
ui->checkBox_isTimerOff->setChecked(true);
}
}
void MainWindow::on_timeEdit_off_userTimeChanged(const QTime &time)
{
mTimeOff = time;
}
void MainWindow::on_checkBox_isTimerOff_clicked(bool checked)
{
QString curPath = QCoreApplication::applicationDirPath();
curPath.append("/CONFIG.ini");
QSettings *config = new QSettings(curPath,QSettings::IniFormat);
if(checked)
{
config->setValue("TimerOff/FLAG",1);
config->setValue("TimerOff/HH",ui->timeEdit_off->time().hour());
config->setValue("TimerOff/MM",ui->timeEdit_off->time().minute());
}
else
config->setValue("TimerOff/FLAG",0);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
4. 总结
遇到问题不要慌,百度找找看,不行换Google。是在不行就甩锅给系统,又可以水一天。
文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。
原文链接:dreamlife.blog.csdn.net/article/details/115354654
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)