How to call specific Python functions from QT GUI
-
@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.
wrote on 24 Aug 2021, 17:26 last edited by@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. -
@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.wrote on 24 Aug 2021, 17:28 last edited by@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
-
@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
wrote on 24 Aug 2021, 17:32 last edited by@lfreeman6490
So start debugging that function! I take it you actually have atest_function
in thetest_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?
21/23