QT通过QProcess与Notepad.exe进行通信

时间:2016-10-20 14:12:37

标签: qt communication qprocess

我想从QT启动notepad.exe然后例如将“Hello Notepad”写入QT记事本的输入区域!

enter image description here

我设法做的是从QT开始记事本。但是,我尝试过什么,我无法在Notepad-textedit区域填写文本。

.H-文件:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<div class="col-sm-4">
  <div name="anotherdiv">
    <input type="range" min="12" max="54" id="fontsize">
  </div>
</div>
<div class="col-sm-8">
  <div class="parent-canvas">
    <div class="text-canvas" id="text-canvas" contenteditable="true">
      my text
    </div>
    <div class="image-canvas">
      <div class="imageupload" onclick="submit_button()">
        <img src="img.png">
      </div>

    </div>
  </div>
</div>

因为我想从qml中做这些东西,所以我在main.cpp中注册了这个类

    #ifndef MATLABPROCESS
    #define MATLABPROCESS

    #include "QObject"
    #include <QProcess>
    #include <QDebug>

    class MatlabProcess : public QObject{
        Q_OBJECT
    public:

        // enum State {IDLE, ERRORX, WRITING};
        //State myState;
        QProcess* myProcess;

        explicit MatlabProcess (QObject* parent = 0) : QObject(parent) {
            myProcess = new QProcess(this);
            // myState = IDLE;
            QObject::connect(myProcess, SIGNAL(readyReadStandardOutput()), SLOT(writing_stream()));
        }

        Q_INVOKABLE bool startMatlab(){

            // QString program = "\"C:/Program Files (x86)/MATLAB/R2012a/bin/matlab.exe\"";
            QString program = "\"C:/FlorianK/notepad.exe\"";

            myProcess->start(program);
            if (!myProcess->waitForStarted(-1)) return false;

            qDebug() << "program started";

            return true;
        }

        Q_INVOKABLE bool stopMatlab(){
            //TODO
            return false;
        }

        Q_INVOKABLE bool writeSomething(){

            QByteArray script = "Hello Notepad\n";

            qDebug() << myProcess->write(script);
            qDebug() << myProcess->waitForFinished();
            QByteArray result = myProcess->readAll();

            qDebug() << "result: " + result;
        }

    private slots:
        void writing_stream() {

            if (!myProcess) return;

            QTextStream out(stdout);
            out << myProcess->readAllStandardOutput() << endl;
        }
    };

    #endif // MATLABPROCESS

从qml我调用startMatlab()而不是writeSomething()。 “writomething”的qt输出将是

qmlRegisterType<MatlabProcess>("com.myself", 1, 0, "MatlabProcess");

超过30秒将不会发生任何事情并且程序冻结而不是跟随行将在Qt输出中打印

14

记事本文本区域保持空白。

0 个答案:

没有答案