QProcess.write文件复制python脚本失败

2024-09-29 22:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用qt4qprocess在新的QThread中运行一个python3.4脚本。我想在脚本仍在运行时输入一些内容。但每次我写的时候,程序都出人意料地完成了,python脚本还在后台运行。不知道为什么 开始部分:

process = new QProcess;
QObject::connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(outlogs()));
process->setProcessChannelMode(QProcess::MergedChannels);
process->start("python3 -i -u /home/circleone/Desktop/test.py",QProcess::Unbuffered | QProcess::ReadWrite);
if (!process->waitForStarted())
    exit(1);

写入部分:

QByteArray a=str.toAscii();
int b=strlen(a);
process->write(a,b);          //it failed in this line
process->closeWriteChannel();
process->waitForBytesWritten();
process->waitForReadyRead();

读取输出部分:

QString resultstr = process->readAllStandardOutput();
emit returnResult(resultstr);

python脚本:

import threading
import time
import sys
import os

class Test(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global strtest
    while True:
        test = input()
        print(test)
        strtest=test
        time.sleep(0.5)

if __name__ == '__main__':
    global strtest
    strtest=''
    thread = Test()
    thread.start()
    while True:
        if(strtest == 'exit'):
            print('quit')
            os.abort()
        time.sleep(0.5)

Tags: testimportself脚本iftimeosexit

热门问题