0

Is it possible to run a Qt gui application as a boost module through python? It was working as a standard C++ executable, but now I'm compiling it down to a shared library and trying to launch it from python. Right now it just goes into the python interpreter every time I run simpleMain() from the interpreter. As in, I get a new "Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)" greetings every time and my program segfaults when I close the interpreter. Also, I can't call the main function directly because I'm not sure how to convert a python list to a char*. A string to char seems to wrok naturally.

This is my python code to launch it:

import libsunshine

libsunshine.simpleMain()

and here's my C++ code:

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(libsunshine)
{
    def("say_hello", say_hello);
    def("simpleMain", simpleMain);
    def("main", main);
}

int simpleMain()
{
   char* args[] = {};
   main(0,args);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Sunshine w;
    w.show();
    return a.exec();
}
voodoogiant
  • 2,118
  • 6
  • 29
  • 49

2 Answers2

0

You can write your application setup in PyQt, which is as simple as

import sys
from PyQt4 import QtGui    
QtGui.QApplication(sys.argv)

at the beginning of your script. Then can call c++ code in your modules which can open/close/... windows. (I have code which works this way)

I think it is illegal to call main in c++, which might be perhaps the reason for segfault.

eudoxos
  • 18,545
  • 10
  • 61
  • 110
  • Is it illegal to call main? It's just another function, named main by convention. But I don't know the C++ standard by heart, so i may be wrong here. – Torp Aug 12 '11 at 14:04
  • @Torp: there is a [question at SO](http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c) about that (and there are several others as well) – eudoxos Aug 12 '11 at 17:21
  • Ah well. Linkage is implementation defined. I don't think compiler writers bother to treat it in a special way though. In this case he can move main() into something called init(), call that directly from boost and call it from main otherwise. Also he could #ifdef out the main() when compiling as a module. – Torp Aug 13 '11 at 07:52
0

Hmm usually main is called with

argc == 1

even when there are no params, argc[0] being the executable name.

Also argv is expected to be a list of pointers to strings terminated with a null pointer, while you're passing nothing. Depending on how QApplication parses the argument list (it may loop depending on argc, or it may just look for a null pointer), it can crash if passed even an argc of zero.
Try

char *args[1] = { NULL }; main(0, args);

or

char *args[2] = { "Dummy name", NULL }; main(1, args);
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
Torp
  • 7,924
  • 1
  • 20
  • 18