Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess with pythonQt
QtWS25 Last Chance

QProcess with pythonQt

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 472 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sogo
    wrote on 17 Jul 2020, 09:09 last edited by
    #1

    Hi,
    I am trying to show the output of a python script that I am running in my c++ application using pythonQt and show it on QTextBrowser. I was looking into QProcess but I'm unable to run it at present. My actual code looks like this without QProcess:

    PythonQt::init();
    PythonQtObjectPtr context = PythonQt::self()->getMainModule();
    context.addVariable("puid", variable);
    context.evalScript(QString("import os,sys\n"
    "from somelib import this\n"
    "this.load(puid)"));
    

    Now I'm trying to wrap it with QProcess and using QProcess's signal SIGNAL(readyRead()), trying to read and append output to QTextBrowser. This is the code I'm trying to wrap

    PythonQt::init();
    PythonQtObjectPtr context = PythonQt::self()->getMainModule();
    context.addVariable("puid",variable);
    QString command = QString("import os,sys\n"
    "from lib import this\n"
    "this.load(puid)");
     QString e = "context.evalScript(command)";
     process->start(e);  //process is instance of QProcess here
    

    This works when I use any one single line command but when I do something like above it does not work and I think I'm doing it wrong so just want to know is it possible to do something like this, to append output of this command to QTextBrowser using QProcess. In short, I want to run context.evalScript command using QProcess and read the output and show on QTextBrowser. Thanks

    J 1 Reply Last reply 17 Jul 2020, 09:13
    0
    • S sogo
      17 Jul 2020, 09:09

      Hi,
      I am trying to show the output of a python script that I am running in my c++ application using pythonQt and show it on QTextBrowser. I was looking into QProcess but I'm unable to run it at present. My actual code looks like this without QProcess:

      PythonQt::init();
      PythonQtObjectPtr context = PythonQt::self()->getMainModule();
      context.addVariable("puid", variable);
      context.evalScript(QString("import os,sys\n"
      "from somelib import this\n"
      "this.load(puid)"));
      

      Now I'm trying to wrap it with QProcess and using QProcess's signal SIGNAL(readyRead()), trying to read and append output to QTextBrowser. This is the code I'm trying to wrap

      PythonQt::init();
      PythonQtObjectPtr context = PythonQt::self()->getMainModule();
      context.addVariable("puid",variable);
      QString command = QString("import os,sys\n"
      "from lib import this\n"
      "this.load(puid)");
       QString e = "context.evalScript(command)";
       process->start(e);  //process is instance of QProcess here
      

      This works when I use any one single line command but when I do something like above it does not work and I think I'm doing it wrong so just want to know is it possible to do something like this, to append output of this command to QTextBrowser using QProcess. In short, I want to run context.evalScript command using QProcess and read the output and show on QTextBrowser. Thanks

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Jul 2020, 09:13 last edited by
      #2

      @sogo said in QProcess with pythonQt:

      context.evalScript(command)

      I fail to understand how this should work?
      This is neither an executable nor a script, so how can it be executed as a process?
      QProcess starts a process to execute an executable or script.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sogo
        wrote on 17 Jul 2020, 12:39 last edited by
        #3

        Hi @jsulm,
        What I thought and correct me if I'm wrong is that context.evalScript("some python script") is a command which executes process for running python scripts and QProcess can start this. It is just like another command that executes something.

        J 1 Reply Last reply 17 Jul 2020, 12:44
        0
        • S sogo
          17 Jul 2020, 12:39

          Hi @jsulm,
          What I thought and correct me if I'm wrong is that context.evalScript("some python script") is a command which executes process for running python scripts and QProcess can start this. It is just like another command that executes something.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Jul 2020, 12:44 last edited by
          #4

          @sogo said in QProcess with pythonQt:

          and QProcess can start this

          How?
          Just try to enter "context.evalScript(command)" in a terminal and see what happens. As I said QProcess starts binaries and scripts in a new process.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sogo
            wrote on 17 Jul 2020, 12:57 last edited by sogo
            #5

            I think now I understand what you mean. Would it need < from line "pythonQt::init"> to <last line of eval.Script> as one script and "variable" as argument to pass to that script. Would that work.
            Edit: Sorry, It will not work. Is there any way I can get the terminal output to show on text browser for when it executes this context.evalScript?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 Jul 2020, 15:45 last edited by
              #6

              Hi,

              Based on a quick look at the official example, you need to use the call method to actually do something and get a result back.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 17 Jul 2020, 22:58
              0
              • S SGaist
                17 Jul 2020, 15:45

                Hi,

                Based on a quick look at the official example, you need to use the call method to actually do something and get a result back.

                S Offline
                S Offline
                sogo
                wrote on 17 Jul 2020, 22:58 last edited by
                #7

                Thanks for the help, I'll look into it more. Closing this thread

                1 Reply Last reply
                0

                5/7

                17 Jul 2020, 12:57

                • Login

                • Login or register to search.
                5 out of 7
                • First post
                  5/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved