当前位置: 动力学知识库 > 问答 > 编程问答 >

qt - QProcess exits before complete

问题描述:

I've got some code that starts mencoder in a QProcess, converts a video while displaying a progress bar, then exits. The problem is, mencoder always exits before it's actually finished. The loop runs through a few times, and then closes. If I comment out the line that updates the progress bar (progress.setValue()), mencoder runs to completion and exits happily.

Been at this for a day, and can't figure it out! Also, I should mention I'm on a Mac.

Any ideas?

Thanks

Marlon

void MainWindow::convertVideo()

{

QString input_filename = "/var/input.avi";

QString output_filename = "/var/output.264";

QStringList arguments;

arguments << input_filename << "-nosound" << "-of" << "rawvideo" << "-ofps" << "30" << "-vf" << "harddup" << "-ovc" << "x264" << "-x264encopts" << "bframes=0" << "-o" << output_filename;

QProcess* myProcess = new QProcess(this);

myProcess->setReadChannel(QProcess::StandardOutput);

myProcess->start("/opt/local/bin/mencoder", arguments);

QString output_string;

QStringList output_pieces;

QProgressDialog progress("Converting video...", "Abort", 0, 100, this);

progress.setWindowModality(Qt::WindowModal);

progress.setValue(0);

progress.show();

while(myProcess->state() != QProcess::NotRunning)

{

output_string = myProcess->readAllStandardOutput();

output_pieces = output_string.split(" ");

QStringList width_string_list = output_pieces.filter("%)");

if(width_string_list.length() > 0)

{

width_string_list = width_string_list[width_string_list.length() - 1].split("(");

if(width_string_list.length() > 1)

{

width_string_list = width_string_list[1].split("%");

}

else

{

width_string_list = width_string_list[0].split("%");

}

progress.setValue(width_string_list[0].toInt());

qDebug() << width_string_list[0].toInt();

}

myProcess->waitForReadyRead();

}

return;

}

网友答案:

I think the QProcess timeouts before it finishes its job. Adding a myProcess->waitForFinished(-1) might help. Then your process wont timeout.

分享给朋友:
您可能感兴趣的文章:
随机阅读: