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. How to call specific Python functions from QT GUI
Forum Updated to NodeBB v4.3 + New Features

How to call specific Python functions from QT GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 3 Posters 2.9k Views 1 Watching
  • 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.
  • L lfreeman6490
    24 Aug 2021, 17:13

    @JonB Would you know how should I proceed with my original issue, that is calling specific Python functions. The path I've been trying to down isn't getting me very far. I've been using this so far.

    void f_pathloss::on_pb_connect_disconnect_clicked()
    {
        PyObject *pName, *pModule, *pDict, *pFunc;
        PyObject *pArgs, *pValue;
    
        Py_Initialize();
    
        pName = PyString_FromString((char*)"test_program");
        pModule = PyImport_Import(pName);
    
        pFunc = PyObject_GetAttrString(pModule, "test_function");
    
        PyXDECREF(pFunc);
        pValue = PyObject_CallObject(pFunc, pArgs);
    }
    

    where 'test_function' is in 'test_program'.py. All that test_function does is return a string that simply says "this is a c++ test". I have seen QProcess mentioned, but that seems to mainly work when you want to run the entire script, not just a single function.

    For the code I've pasted above, once the user hits the connect_disconnect_clicked() button, I want to call a function.

    J Offline
    J Offline
    JonB
    wrote on 24 Aug 2021, 17:26 last edited by
    #21

    @lfreeman6490
    I have never done any of this. You show code, which looks reasonable, but say nothing about what happens when you call it, or step through it in debugger? @mrjj linked to an example, it looks similar. You would benefit from some error checking code here.

    QProcess is to do with running an external program, or OS command. You can run a whole Python script that way, but I don't think it's what you should be looking at.

    L 1 Reply Last reply 24 Aug 2021, 17:28
    0
    • J JonB
      24 Aug 2021, 17:26

      @lfreeman6490
      I have never done any of this. You show code, which looks reasonable, but say nothing about what happens when you call it, or step through it in debugger? @mrjj linked to an example, it looks similar. You would benefit from some error checking code here.

      QProcess is to do with running an external program, or OS command. You can run a whole Python script that way, but I don't think it's what you should be looking at.

      L Offline
      L Offline
      lfreeman6490
      wrote on 24 Aug 2021, 17:28 last edited by
      #22

      @JonB Well nothing happens when I do it. It builds successfully and launches the GUI, once I click the button nothing happens. There isn't an error thrown at all either. Just wanted to ask and see

      J 1 Reply Last reply 24 Aug 2021, 17:32
      0
      • L lfreeman6490
        24 Aug 2021, 17:28

        @JonB Well nothing happens when I do it. It builds successfully and launches the GUI, once I click the button nothing happens. There isn't an error thrown at all either. Just wanted to ask and see

        J Offline
        J Offline
        JonB
        wrote on 24 Aug 2021, 17:32 last edited by
        #23

        @lfreeman6490
        So start debugging that function! I take it you actually have a test_function in the test_program, else what do you expect.

        Maybe you need the different code in https://sites.northwestern.edu/yihanzhang/2019/08/22/how-to-invoke-python-function-from-c/, I don't know?

        1 Reply Last reply
        1

        21/23

        24 Aug 2021, 17:26

        • Login

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