天天看點

Qt6-網絡關機助手(開機自啟版)實作設定程式圖示 實作程式在隐藏标題欄情況下可移動 實作系統托盤顯示功能 實作擷取計算機IP位址和名稱功能

源碼連接配接: https://download.csdn.net/download/z609932088/15422254 程式截圖如下

Qt6-網絡關機助手(開機自啟版)實作設定程式圖示 實作程式在隐藏标題欄情況下可移動 實作系統托盤顯示功能 實作擷取計算機IP位址和名稱功能
目前這個小工具已經開發完成,歡迎小夥伴們一起交流。

設定程式圖示

Qt6這裡設定程式運作的圖示又回到了以前的版本了。首先在工程檔案夾下面建立一個rc檔案,輸入以下内容

Qt6-網絡關機助手(開機自啟版)實作設定程式圖示 實作程式在隐藏标題欄情況下可移動 實作系統托盤顯示功能 實作擷取計算機IP位址和名稱功能

準備一個ico格式的圖檔和這個檔案放在一起。

Qt6-網絡關機助手(開機自啟版)實作設定程式圖示 實作程式在隐藏标題欄情況下可移動 實作系統托盤顯示功能 實作擷取計算機IP位址和名稱功能

在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));
}      

繼續閱讀