001_QT之呼叫cmd並執行ping命令
阿新 • • 發佈:2021-07-12
//mainwindow.cpp檔案,主介面實現
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <qprocess.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::on_btnPing_clicked(){
qDebug()<<QStringLiteral("開始點選");
ui->textBrowser->clear();
ui->textBrowser->setText(QStringLiteral("請等待"));
ui->textBrowser->update();
QProcess myProcess(this); QString program = "C:/Windows/System32/cmd.exe"; QStringList arguments; arguments <<"/c" <<"ping www.baidu.com"; myProcess.start(program,arguments); while (myProcess.waitForFinished(100) == false) { QByteArray qByteRead = myProcess.readAllStandardOutput(); if (!qByteRead.isEmpty()) { ui->textBrowser->append(QString::fromLocal8Bit(qByteRead)); repaint(); } } QByteArray qByteRead = myProcess.readAllStandardOutput(); ui->textBrowser->append(QString::fromLocal8Bit(qByteRead)); qDebug()<<QString::fromLocal8Bit(qByteRead); qDebug()<<"結束點選";
}