[PySide] Detecting which framework (PyQt or PySide) is active
David García Garzón
david.garcia at upf.edu
Mon Sep 3 19:28:12 CEST 2012
That wont work in my case. To clarify the situation with some simplified code:
#################################
# This is user code.
# User could either choose PyQt4 or PySide
#from PyQt4 import QtGui
from PySide import QtGui
import mylib # that's the lib i am coding
# my guess: this will trigger framework dependant initializations
app = QApplication(sys.argv)
w = mylib.createInterface()
w.show()
app.exec_()
##############################
# This is the python layer of mylib
# imports from previous file are not available
# Indeed we could need to import both
import PyQt4.QtGui
import PySide.QtGui
def createInterface() :
if isPySideInitialized() : # Required functionality!!
return createInterfacePySide()
else :
return createInterfacePyQt4()
///////////////////////////////////////////////
// This is C++ layer of mylib
QWidget * createInterface(...)
{
return new QPushButton();
}
PyObject * createInterfacePyQt4(...)
{
return SipWrap(createInterface());
}
PyObject * createInterfacePySide(..)
{
return ShibokenWrap(createInterface());
}
The decision could be made delayed to the C++ code if there is any mean from
C++, but it should be done at library layer, to avoid tainting user code with
such nasty ehem... details.
Maybe on the line of your dirty hacks, instead of doing something that do not
work for either framework, we should try something that it won't work (but
fails safely with an exception) if the framework is not initialized.
David.
On Monday 03 September 2012 18:16:09 Aaron Richiger wrote:
> Hello David!
>
> As far as I know, there is no direct function in the PySide (Qt) API to
> get the used technology. But others may know more... I could imagine two
> solutions:
> - Dirty hack: Some things work in PySide, that do not work with PyQt4.
> You could try one of them and if it fails, then know that you are on PyQt4
> - Get the info from the built-ins of the QApplication.instance() object,
> e.g. __repr__ . I wrote the following little script that checks what you
> asked for, but don't know whether it's usable for your purpose:
>
>
> ############ Code ####################
> import sys
>
> from PySide.QtCore import *
> from PySide.QtGui import *
>
> # from PyQt4.QtCore import *
> # from PyQt4.QtGui import *
>
> def is_PySide():
> return 'PySide' in repr(QApplication.instance())
>
> def is_PyQt4():
> return 'PyQt4' in repr(QApplication.instance())
>
>
> if __name__ == '__main__':
> app = QApplication(sys.argv)
> w = QWidget()
> w.show()
> print 'Is PySide:', is_PySide()
> print 'Is PyQt4: ', is_PyQt4()
> sys.exit(app.exec_())
>
> ###################################
>
> Am 03.09.2012 17:47, schrieb David García Garzón:
> > Hi, i am working on a CPython function i want to transparently run with
> > either PyQt or PySide. It is a functions that builds an interface using
> > C++ and then wraps it using either Sip (PyQt) or shiboken (PySide) in
> > order to return it to python. The wrapping part was solved thanks to your
> > help in a different thread but i still have the problem of knowing which
> > wrapper i should use in a given situation.
> >
> > Is there some state (function, singletons, globals...) i could check? I am
> > thinking on the pyside created QApp or any PySide state it requires to be
> > initializated.
> >
> > Additional info: The library is split in two layers, a python and a C one,
> > so it could be either api, but not in user code, so solution such as
> > checking the imported modules are not convinient. If there is no api for
> > that i am thinking in hacks like using PySide initializations required by
> > QApp if any.
> >
> > Any ideas?
> >
> > David.
> >
> > _______________________________________________
> > PySide mailing list
> > PySide at qt-project.org
> > http://lists.qt-project.org/mailman/listinfo/pyside
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
More information about the PySide
mailing list