Python Script with C++Qt

Hi
i work under CentOS 5.5, Qt 4.7, Python 2.4.
i need to send data from C++ to Python.
i write a code but it's not work, i have an Inferior System error on it.

PyObject* objArg = Py_BuildValue("(z)", cKeyNum);
PyObject* objFunc = (PyObject*)Init;
pValue = PyEval_CallObject(objFunc, objArg);

if there any know how to solve this problem please answer me .
Thanx

Please post the error.

The inferior stopped because it received a signal from the Operating System.

Signal name :
SIGSEGV
Signal meaning :
Segmentation fault

What is 'Init'? Where do you get it? Post all your code, if you can.

 #ifndef QPYTHON_H
#define QPYTHON_H
#endif // QPYTHON_H
#include "python2.4/Python.h"
#include "QString"
#include "QFile"
#include "QTextStream"
#include "QSound"
#include "QMessageBox"
#include "phonon/AbstractAudioOutput"
#include "phonon/AbstractMediaStream"
#include "phonon/phonon"
class QPython
{
private:
    QString strScript;
    static QString strANI;
    static QString strDNIS;
public:
    QPython(){}
    QPython(QString _strScript)
    {strScript  = _strScript;}
    ~QPython(){}
    int PythonInitialize()
    {
        Py_Initialize();
        return Py_IsInitialized();
    }
    int process_expression(char* cScript, char* cKeyNum)
    {
        PyObject *pValue;
        static PyMethodDef dvoice[] = {
            // "Python name", C Ffunction Code, Argument Flags, __doc__ description
            {"playwave", playwave, METH_VARARGS, ""},
            {"ClearDigitBuffer", ClearDigitBuffer, METH_NOARGS, ""},
            {NULL, NULL, 0, NULL}
        };
        cKeyNum = "1";
        try
        {
        PyObject *objModule = PyImport_ImportModule("main");
        //PyObject *objArg = Py_BuildValue("(s)", cKeyNum);
        //PyObject *objFunc = PyObject_GetAttrString(objModule, "main");
        pValue = PyEval_CallObject(PyObject_GetAttrString(objModule, "main")
                                   , Py_BuildValue("(s)", cKeyNum));
        Py_DECREF(pValue);}
        catch(QString strError)
        {QMessageBox msg;
        msg.setText(strError);
        msg.exec();}
        Py_InitModule("dvoice", dvoice);
        return PyRun_SimpleString(cScript);
    }
    int process_expression_FILE(char *cFile, char* cKeyNum)
    {
        static PyMethodDef dvoice[] = {
            // "Python name", C Ffunction Code, Argument Flags, __doc__ description
            {"Init", Init, METH_VARARGS, ""},
            {"playwave", playwave, METH_VARARGS, ""},
            {"ClearDigitBuffer", ClearDigitBuffer, METH_NOARGS, ""},
            {NULL, NULL, 0, NULL}
        };
        PyObject* objArg = Py_BuildValue("(0)", cKeyNum);
        PyObject* objFunc = (PyObject*)Init;
        PyEval_CallObject(objFunc, objArg);
        Py_InitModule("dvoice", dvoice);
        FILE* file_1 = fopen(cFile, "r");
        return PyRun_SimpleFile(file_1, cFile);
    }
    void PythonFinalize()
    {Py_Finalize();}
    //, QString strWaveFile, QBool bSkip, int iFuture
    static PyObject* playwave(PyObject* pSelf, PyObject* pArgs)
    {
        char* strWaveFile;
        int *iSkip;
        int *iFuture;
        PyArg_ParseTuple(pArgs, "sii", &strWaveFile, &iSkip, &iFuture);
        QString ster(strWaveFile);
        Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory, Phonon::MediaSource(ster));
        music->play();
    }
    static PyObject* ClearDigitBuffer(PyObject* pSelf, PyObject* pArgs)
    {
    }
};

the error causes in PyEval_CallObject() function

And what about the code that calls this object?

The PyEval_CallObject is in Python Classes i just call it.

I still don't see "Init" declared anywhere. It must be a global, if it's not declared inside the class. What is it? Have you checked the value to see if it's sane? Are you sure it's really a valid PyObject *?

first forget about Init.
Second it's a really a valid PyObject*.

If you insist. You should probably make sure both those pointers aren't NULL before you evalobject anything though, if something's gone wrong an error message instead of a crash would be good.

Thanks Corona688 but it's still got the same error.

Sorry, I can't help you without you telling me more about where these external objects come from. A segmentation fault usually means a bugged pointer, going past the bounds of an array, a bad typecast, etc, etc.

hi Corona688
the error is come from

        pValue = PyEval_CallObject(PyObject_GetAttrString(objModule, "main")
                                   , Py_BuildValue("(s)", cKeyNum));

---------- Post updated 10-04-10 at 04:58 AM ---------- Previous update was 10-03-10 at 07:11 AM ----------

Hi Corona688
i solve the problem and the code is work good

            PyObject *objValue = NULL;
            PyObject *objModule = NULL;
            PyObject *objDict = NULL;
            PyObject *objExpression = NULL;
            PyObject *objArgs = NULL;
            objDict = PyDict_New();
            objModule = PyImport_AddModule("__main__");
            //PyObject *objStrtoLong = ;
            objArgs = Py_BuildValue("(s)", iKeyNum);
            Py_IncRef(objModule);

            objDict = PyModule_GetDict(objModule);
            objExpression = PyDict_GetItemString(objDict, "main");
            if(PyCallable_Check(objExpression))
            {
                objValue = PyObject_CallObject(objExpression, objArgs);
                PyObject_CallMethod(objValue, "Sub", "(s)", iKeyNum);
            }
            else
            {
//                msg.setText("Not Callable");
//                msg.exec();
            }