View Single Post
Old 01-07-2021, 09:43 PM   #54
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 8,810
Karma: 6000000
Join Date: Nov 2009
Device: many
Quote:
Originally Posted by wrCisco View Post
Since my program crashed while calling PyObject_CallObject (inside runInPython), it was probably a problem with the conversion the other way around, from QList<int> to QVariant.
Okay I ran the following testcase and got this to work. I think a QVariant of a QList should work directly but I could not get it to work.

In PythonRoutines.cpp and .h
-------------------------------------
Code:
int PythonRoutines::SumIntList(const QList<int>& ilist);

int PythonRoutines::SumIntList(const QList<int>& ilist)
{
    int results;
    int rv = -1;
    QString error_traceback;
    QList<QVariant> args;
    QVariantList qvl;
    foreach(int v, ilist) {
       qvl.append(QVariant(v));
    }
    args.append(QVariant(qvl));

    EmbeddedPython * epython  = EmbeddedPython::instance();

    QVariant res = epython->runInPython( QString("testcase"),
                                         QString("sum_int_list"),
                                         args,
                                         &rv,
                                         error_traceback);
    if (rv == 0) {
        results = res.toInt();
    }
    return results;
}
And in the python3lib I used the following testcase.py

Code:
import sys
import os

def sum_int_list(alist):
    sum = 0
    for v in alist:
        sum = sum + v
    return sum
And here is how I invoked it:

Code:
   // test pass ing list of ints to python routine                                                                             
    QList<int> alist;
    alist << 1 << 3 << 6 << 9;
    PythonRoutines pr;
    int sum = pr.SumIntList(alist);
    qDebug() << "Sum from SumIntList is: " << sum;
I will work on lists.

But this should get you going.
KevinH is offline   Reply With Quote