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. calling python from qt c++
Forum Updated to NodeBB v4.3 + New Features

calling python from qt c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.2k 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.
  • M Offline
    M Offline
    marwa ahmed
    wrote on 2 Jun 2020, 20:09 last edited by
    #1

    hi,

    i want to call a python code from qt c++ and give it some float parameters, how can i do this in qt and how should the python file which is call look like?

    J 1 Reply Last reply 2 Jun 2020, 20:18
    0
    • M marwa ahmed
      2 Jun 2020, 20:09

      hi,

      i want to call a python code from qt c++ and give it some float parameters, how can i do this in qt and how should the python file which is call look like?

      J Offline
      J Offline
      JonB
      wrote on 2 Jun 2020, 20:18 last edited by
      #2

      @marwa-ahmed
      Depends whether you intend the Python code to be an external script or an embedded interpreter in the Qt C++ app. Which probably depends on what you want it to do. Always assuming you don't want your Qt app to be written in Python + Qt itself.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        marwa ahmed
        wrote on 2 Jun 2020, 20:34 last edited by
        #3

        python code is external script not embedded in the qt and it takes parameters 4 float variables so i want to call this script which takes the input and do lots of work and algorithm depends on many libraries in python the return a result
        i tried first a code like this to ensure that qt can call a python file
        QStringList params;
        QString pythonPath = "C:/Python27/python.exe";
        QString pythonScript = "C:/Users/maboelamayem/PycharmProjects/testqt.py";

            params << pythonScript;
            p.start(pythonPath, params);
            p.waitForFinished(-1);
            QString p_stdout = p.readAll();
            QString p_stderr = p.readAllStandardError();
            if(!p_stderr.isEmpty())
               qDebug()<<"Python error:"<<p_stderr;
            qDebug()<<"Python result="<<p_stdout; 
        

        but it gives error : Python error: "C:\Python27\python.exe: can't open file 'C:/Users/maboelamayem/PycharmProjects/testqt.py': [Errno 2] No such file or directory\r\n"

        J 1 Reply Last reply 2 Jun 2020, 20:36
        0
        • M marwa ahmed
          2 Jun 2020, 20:34

          python code is external script not embedded in the qt and it takes parameters 4 float variables so i want to call this script which takes the input and do lots of work and algorithm depends on many libraries in python the return a result
          i tried first a code like this to ensure that qt can call a python file
          QStringList params;
          QString pythonPath = "C:/Python27/python.exe";
          QString pythonScript = "C:/Users/maboelamayem/PycharmProjects/testqt.py";

              params << pythonScript;
              p.start(pythonPath, params);
              p.waitForFinished(-1);
              QString p_stdout = p.readAll();
              QString p_stderr = p.readAllStandardError();
              if(!p_stderr.isEmpty())
                 qDebug()<<"Python error:"<<p_stderr;
              qDebug()<<"Python result="<<p_stdout; 
          

          but it gives error : Python error: "C:\Python27\python.exe: can't open file 'C:/Users/maboelamayem/PycharmProjects/testqt.py': [Errno 2] No such file or directory\r\n"

          J Offline
          J Offline
          JonB
          wrote on 2 Jun 2020, 20:36 last edited by
          #4

          @marwa-ahmed said in calling python from qt c++:

          can't open file 'C:/Users/maboelamayem/PycharmProjects/testqt.py': [Errno 2] No such file or directory\r\n"

          So that file does not exist. Nothing we can help with if it does not.

          1 Reply Last reply
          1
          • M Offline
            M Offline
            marwa ahmed
            wrote on 2 Jun 2020, 20:45 last edited by
            #5

            okay i missed main in the python code but now my python code is :
            if name == "main":
            print "marwa"

            but the error in qt is

            Python error: "C:\Python27\python.exe: can't find 'main' module in 'C:/Users/maboelamayem/PycharmProjects/testqt'\r\n"

            so am i writing the function in python code wrongly?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              marwa ahmed
              wrote on 2 Jun 2020, 21:23 last edited by
              #6

              hey, i solved this problem but i want now to send parameters to the main function in the python code but p.start() is not used for this so what is the other option to send parameters or arguments to the python function

              J 1 Reply Last reply 2 Jun 2020, 22:07
              0
              • M marwa ahmed
                2 Jun 2020, 21:23

                hey, i solved this problem but i want now to send parameters to the main function in the python code but p.start() is not used for this so what is the other option to send parameters or arguments to the python function

                J Offline
                J Offline
                JonB
                wrote on 2 Jun 2020, 22:07 last edited by
                #7

                @marwa-ahmed said in calling python from qt c++:

                but p.start() is not used for this

                Yes it is. Why not look at the docs: https://doc.qt.io/qt-5/qprocess.html#start and https://doc.qt.io/qt-5/qprocess.html#running-a-process

                1 Reply Last reply
                3
                • K Offline
                  K Offline
                  kaif
                  wrote on 2 Sept 2020, 12:43 last edited by
                  #8

                  please can I get your contact number I am working on an industrial robot and I am stuck with the same thing you did! It is urgent. Thank you

                  1 Reply Last reply
                  0

                  • Login

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