Analysis of the source code of the network editor for the configuration network IP address tool under Qt Linux

Under Linux, Linux here refers to various Ubuntu systems, etc., so you can test it individually.
The key code is as follows: The
main thing is to use two processes to set the IP first, and then set the gateway.
A single QProcess object seems to be unable to do two things.
For the complete ui code
,
please see here: networkEditor.zip: Please add your own code to experiment, if you don’t, you can check my previous articles or Baidu.

//添加IP
    QProcess *process = new QProcess;
    QStringList args;
    args << "-c" << QString("echo %1 | sudo -S ifconfig %2 %3 netmask %4").arg(mPassword).arg(mInterfaceName).arg(mIpAddress).arg(mNetmask);
    process->execute("/bin/sh",args);
    int exitCode = process->exitCode();
    if( exitCode!= QProcess::NormalExit ){
    
    
        qDebug()<<"set ip failed";
        return;
    }else{
    
    
        qDebug()<<"set ip ok";

    }
    process->waitForFinished();
    delete process;
    //添加网关
    QProcess *process1 = new QProcess;
    QStringList args1;
    args1 << "-c" << QString("echo %1 | sudo -S route add default gw %2").arg(mPassword).arg(mGateway);
    process1->execute("/bin/sh",args1);
    if( exitCode!= QProcess::NormalExit ){
    
    
        qDebug()<<"set gw failed";
        return;
    }else{
    
    
        qDebug()<<"set gw ok";
        emit sigUpdateIP();
        QMessageBox::information(0,"提示","IP设置成功");

    }
    process1->waitForFinished();
    delete process1;
    close();

Guess you like

Origin blog.csdn.net/poolooloo/article/details/108461799