Menu

#3 Removing designMode from widget constructor

unassigned
waiting
nobody
None
bug
2015-05-14
2014-01-22
No

Using designMode as constructor parameter is not very nice.
If you create a widget by composition and use it with QtDesigner (*) , the children of the widget will not be in designerMode and it will create a mess.

(*) for example by importing a .ui file. But in pure python code, providing designerMode everywhere it is needed is also a mess.


One way to fix that, the one i use with the help of Tiago is to check the modules:

designMode = 'PyQt4.QtDesigner' in sys.modules

It does not support all the cases, but it can be improved


Another way is to inject the context to the class when we create the QtDesignerPlugin

klass.QTDESIGNER_CONTEXT = True

And using something like that on the class:

designMode = hasattr(self, "QTDESIGNER_CONTEXT")

Discussion

  • Tiago Coutinho

    Tiago Coutinho - 2014-01-27

    I like the idea! I prefer something like the first option.
    The weak point is if somebody does this kind of program:

    import PyQt4.QtDesigner
    import taurus.qt.QtGui
    
    # ...
    

    I propose here what I believe is a safer implementation:

    I think we could have in taurus.qt.QtGui an API to check/set if in QtDesigner:

    # taurus/qt/QtGui/__init__.py
    
    __designerMode = False
    
    def isDesignerMode():
        global __designerMode
        return __designerMode
    
    def setDesignerMode(yesno):
        global __designerMode
        __designerMode = yesno
    

    When QtDesigner starts, it imports taurus.qt.qtdesigner.tauruspluginplugin.
    The taurus.qt.qtdesigner.tauruspluginplugin could set the designer mode to True:

    # taurus/qt/qtdesigner/tauruspluginplugin.py
    
    from taurus.qt.QtGui import setDesignerMode
    
    def main()
        setDesignerMode(True)
        # ...
    

    The widgets could check if they are in QtDesigner if needed by doing:

    # mywidget.py
    
    from taurus.qt.QtGui import isDesignerMode
    
    class MyWidget(...):
    
        def __init__(self, parent=None):
            # ...
    
            designerMode = isDesignerMode()
            if designerMode:
                # ...
            else:
                # ...
    
            # ...
    
     
    • Carlos Pascual

      Carlos Pascual - 2014-01-28

      I like that!

       
  • Carlos Pascual

    Carlos Pascual - 2015-05-14
    • status: open --> waiting
    • Milestone: --> unassigned
    • Type: --> bug