方式1
QProcess sh;
sh.start("sh", QStringList() << "-c" << "ifconfig eth0 | grep inet | awk \'{print $2}\'\n");
if (!sh.waitForFinished()) {
qDebug() << sh.errorString();
}
QString output(sh.readAllStandardOutput());
if(!output.isNull()){
QStringList qsl = output.split(":");
if(qsl.size()==2){
QString str = qsl.at(1);
str = str.left(str.length() - 1);
ui->deviceIpAddrV->setText(str);
}
}
sh.close();
方式2
QProcess sh;
sh.start("sh", QStringList() << "-c" << "ip addr | grep eth0 | grep \'inet\\s\' | awk \'{print $2}\' | cut -f1 -d\'/\' | sed -n '2p'");
if (!sh.waitForFinished()) {
qDebug() << sh.errorString();
}
QString output(sh.readAllStandardOutput());
qDebug()<<"ipaddr = "<<output;
if(!output.isNull()){
QString str = output;
str = str.left(str.length() - 1);
ui->deviceIpAddrV->setText(str);
}
sh.close();