Qt中使用QProcess在一个进程中启动另一个进程

void MainWindow::startFunc()
{
	QProcess *process = new QProcess(this);	//this表示process 的父窗口是本程序中的主窗口,当主窗口关闭时,进程也会终止,
 											//如果不填加this,process 是独立的进程,主窗口关闭,进程依然运行
	QString path = QCoreApplication::applicationDirPath();
	path += "/myprocess.exe";
	QStringList arg;
	arg.append("hello");
	arg.append("world");
	process->start(path,arg);
}
void MainWindow::endFunc()
{
	if(process)
	{
		process->close();
		delete	process;
		process = NULL;
	}
}

说明:

  • 可以调用close()函数关闭外调进程;
  • 若没有调用close()函数,因为创建QProcess *process = new QProcess(this);使用了this,故当调用程序退出后,外调进程也会退出。

外调进程中参数的使用

int main(int argc, char *argv[])
{
	FILE *file;
	for(int i=1;i<argc-1;i++)
	{
		//qDebug()<
		file = fopen("1.dat","w+");
        fputs(argv[i],file);//将调用进程的函数QStringList中的数据,挨个取出,写入到文件中
	}
	fclose(file);
	return 0;
}

你可能感兴趣的:(Qt)