Menu

PyGuiExample - imports fail

Help
Jeff S.
2013-01-04
2013-04-06
  • Jeff S.

    Jeff S. - 2013-01-04

    I have built Python2.6.8 in Visual Studio 2008, and also built PythonQt in same (64 bit, release).
    Python works fine, and no build problems.
    I also built the PyGuiExample to try things out.

    When I run the PyGuiExample, it runs fine, but the python script example.py does not run correctly.
    The python console window appears, but give and error :

     NameError: name 'QGroupBox' is not defined.
    

    The top line of the _example.py _script is

        from PythonQt.QtGui import *
    

    It seems that this import is failing. The python console window that appears works - I can type in basic commands and get the correct result back.

    Any ideas or suggestions as to what might be wrong are appreciated.

    Thanks.

     
  • Chris Bevan

    Chris Bevan - 2013-01-07

    Hi Jeff,

    Just guessing here - but which version of Qt did you build it with?

    PythonQt's Qt bindings (PythonQt.*) are built using a generator script (see the "generator" folder) against a particular version of Qt.  If they were built with a different version of Qt to what you're building with, that might cause some errors like the ones you're seeing.  I can't remember off-hand what version the bindings that come with the download were built with, though - probably 4.6 or 4.7…?

    Cheers,
    - Chris

     
  • Jeff S.

    Jeff S. - 2013-01-07

    Chris,

    Thanks for your reply.

    I built using Qt 4.8 with VisualStudio 2008 and the Qt plugin.

    I see the generator folder (under PythonQt2.1_Qt4.8) so I'm assuming that the bindings were already generated for 4.8, but the README says that the bindings are for 4.6.2.

    Is the idea that I create a VisualStudio solution from the generator.pro file and then run that as some console app? What does this do and what are the next steps?

    Thanks for your help.

    Jeff.

    I did build PythonQt using this build as well.

     
  • Chris Bevan

    Chris Bevan - 2013-01-08

    Hi Jeff,

    I haven't built it myself for about a year, but here's the relevant part of my build script:

    # -- Generate bindings --
    config = 'release'
    qt_path, qmake = setQtPath(config)
    openssl_path = setOpenSSLPath(config)
    os.environ['LD_LIBRARY_PATH'] = openssl_path + ':' + qt_path + '/lib'
    # Clean any previous build
    generated_cpp_path = pq_src_path + '/generated_cpp'
    if os.path.exists(generated_cpp_path):
        shutil.rmtree(generated_cpp_path)
    os.chdir(pq_src_path + '/generator')
    makeClean()
    # Build the generator
    callSubprocess([qmake])
    callSubprocess(['make'])
    # Generate the bindings
    callSubprocess(['./pythonqt_generator'])
    

    It's for Linux, but hopefully should be clear enough.  LD_LIBRARY_PATH is just a DLL load path, and 'make' is the build step.

    In essence, though, yes: generator.pro will give you the generator executable, and running that will give you a set of CPP files for bindings that should be automatically built with the PythonQt_QtAll project to give you access to the Qt libraries in your Python app.  I think you can also tweak the XML files in the generator directory to alter what code gets generated, but I haven't messed around with it much myself.

    Hope that helps.

    - Chris

     
  • Jeff S.

    Jeff S. - 2013-01-08

    Chris,

    I don't think that the generator is the problem. The DLLs load just fine, and  I read in another thread that the existing bindings (4.6.1) will work with Qt 4.8, you just don't get any new bindings. So things like QGroupBox and QString should work.

    I built the example PyGuiEample app  cutting out  PythongQt_QtAll since that wants to bring in everything (WebKit, SQL, XML, etc.) and I only need QtGui and QtCore - which I think should be accessible after calling PythonQt::init().

    Do you know if PythonQt can be used without including all the bindings (i.e. leaving out PythonQt_QtAll)?  I'm at a loss as to why the bindings are not working for this simple example app.

     
  • Jeff S.

    Jeff S. - 2013-01-08

    I've answered part of my question:  PythonQt only has a very rudimentary set of Qt classes inclued from QtGui and QtCore. This is what I show when doing a dir(QtCore) and dir(QtGui) in Python:

    QtCore:
    ['QBitArray', 'QByteArray', 'QDate', 'QDateTime', 'QLine', 'QLineF', 'QLocale', 'QMetaObject', 'QPoint', 'QPointF', 'QRect', 'QRectF', 'QRegExp', 'QSize', 'QSizeF', 'QTime', 'QUrl', 'Qt', 'SIGNAL', 'SLOT', '__doc__', '__name__', '__package__', 'qAbs', 'qBound', 'qCritical', 'qDebug', 'qFatal', 'qFuzzyCompare', 'qMax', 'qMin', 'qRound', 'qRound64', 'qVersion', 'qWarning', 'qrand', 'qsrand']
    QtGui:
    ['QBitmap', 'QBrush', 'QColor', 'QCursor', 'QFont', 'QIcon', 'QImage', 'QKeySequence', 'QMatrix', 'QPalette', 'QPen', 'QPixmap', 'QPolygon', 'QRegion', 'QSizePolicy', 'QTextFormat', 'QTextLength', '__doc__', '__name__', '__package__']
    

    Looking into PythonQt_QtAll, I see that the classes I'm looking for (QString, QGroupBox) are there, but I don't want to have to include all the Qt dlls for things not used in my app (like Webkit, QtXML, QtSql, etc.)

    So, is there a nice, simple way to cut down what's in QtAll? I could make another project and just exclude things from the project, but I thought there might be a "built-in" way to customize what you wanted bound.

    Any help is appreciated.  And thanks to Chris for following me through this so far.

    Jeff.

     
  • Florian Link

    Florian Link - 2013-01-09

    Currently there is not "built-in" way to do this with PythonQt/qmake, but as you said, you can quite easily build an extra DLL which only contains the classes that you need. I am not sure, maybe the cmake build files (courtesy of the Slicer community) allow that kind of configuration.

    In MeVisLab we actually go a bit further and created loadable C-Python modules for each of the Qt DLLs, so that e.g. import PythonQt.QtGui will load the QtGui binding. Unfortunately I did not have time to contribute this to the PythonQt project itself.

     
  • Chris Bevan

    Chris Bevan - 2013-01-09

    I made a few changes to the bindings in PythonQt_QtAll some time back when building for Qt 4.5, but have to admit I mostly went brute force and hacked out a bunch of includes and things from the generated CPP files and such.  I found that reasonably easy to do, so I'm sure you (Jeff) will be able to do the same if it's the best solution.  Sorry I'm not much more help - but as I say, I haven't built it in ages now; I was just trying to think of something that might be useful :)

    - Chris

     
  • Jeff S.

    Jeff S. - 2013-01-09

    This is now resolved.  I modified the PythonQt_QtAll project (in VisualStudio 2008) to remove references to unwanted libraries. My version with PyGuiExample now works, without the needs to have dlls for QtWebkit, QtXML, QtSql present.
    Thanks to Florian and Chris for help.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.