2012-03-29 3 views
0

IP 주소로 ping을 수행하고 있고 ping 작업이 진행 중임을 QMessageBox에 표시하려고합니다. 그 후, 응답이 수신되거나 1 초의 시간 초과가 발생하면 QMessageBox를 닫으려고합니다.ping 명령 응답을 기다리는 중 QMessage

코드 :

int status; 
QByteArray command; 
QMessageBox myBox(QMessageBox::Information, QString("Info"), QString("Checking connection"), QMessageBox::NoButton, this); 

command.append("ping -w 1 172.22.1.1"); 
status=system(command); 
myBox.setStandardButtons(0); 
myBox.exec(); 
if (0==status){ // Response received 
    // Some stuff here... 
    myeBox.setVisible(false); 
} 
else { // Timeout 
    // Some other stuff here... 
    myBox.setVisible(false); 
} 

내 생각 엔 내가이 작업 스레드를 사용해야 할 수도 있습니다,하지만 난 Qt는 초보자입니다 때문에 어쩌면 문제는 다른 곳이다.

편집 :

private: 
QProcess *process; 
//... 

     QMessageBox myBox(QMessageBox::Information, QString("Info"), QString("Checking connection"), QMessageBox::NoButton, this); 
    QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &myBox, SLOT(close())); 
    command.append("ping -w 1 172.22.1.1"); 
    process.start(comdand); 
     myBox.setStandardButtons(0); 
     myBox.exec(); 
: @atamanroman으로 신호 무효 QProcess를 사용하여 :: Qt는 참조 말한대로 종료 (INT의 ExitCode를, QProcess :: ExitStatus exitStatus) 신호를, 내가 QProcess를 사용하려고했습니다 제안

그리고 작동하지 않습니다. myBox가 절대로 닫히지 않습니다. 뭐가 문제 야?

답변

0

당신은 그들이 Qt는의 일부이며 핑이 완료 할 때 신호 수 있기 때문에 QProcess이 나 QTcpSocket (핑 자신을) 대신 system() (출력을 PING.EXE 시작하고 구문 분석)을 사용한다. 해당 신호에 연결하여 QMessageBox을 숨 깁니다. 첫째 : 당신의 편집에

+0

메시지 상자 (또는 더 나은 QProgressDialog)는 스택에 로컬로 생성되지 않고 QProcess :: finished() 및 :: error() 슬롯에 연결되는 대신 클래스 멤버 여야합니다. –

+0

QDialog에서 상속받은 done()으로 닫아야합니까? –

0

다음 완성 된 신호를하지 않습니다, 멈추지 않을 것입니다 리눅스 핑을 사용 :

QProcess *process; // This is a pointer, you don't need to add "&" in connect 
        // You should have called "process = new QProcess" before... 
QMessageBox myBox; // This is an object, you need to add the "&" to connect; 

우리는 두 번째

QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), &myBox, SLOT(close())); 

첫 &을 꺼내. 카운트 또는 대기 시간과 같은 매개 변수를 ping에 제공 할 수 있습니다. 또는 타이머를 시작하여 프로세스를 중지하십시오.

세 번째는 : 내가 로컬 "processfinished (INT, QProcess :: ExitStatus)"SLOT을 만들 수 있습니다 sugest 한 다음 myBox에 전화 등 을 경고를 방지하기 위해 신호와 슬롯 사이의 매개 변수와 일치해야합니다. 닫기(),하지만 "myBox"당신이 그것을 호출하는 방법을 끝내고 이것에 대한 참조를 가지고 클래스에서 있어야합니다.