Python code generation with pyside-uic

前端 未结 8 1117
野的像风
野的像风 2020-12-01 12:18

How can I generate python code from a QtDesigner file ? I found pyside-uic but I can\'t find an example for the syntax. I run win7 and pythonxy with spyder.

相关标签:
8条回答
  • 2020-12-01 12:59

    pyside-uic is more or less identical to pyuic4, as such the man page specifies:

    Usage:
            pyside-uic [options] <ui-file>
    
    Options:
        --version
            show program's version number and exit
    
        -h,--help
            show this help message and exit
    
        -oFILE,--output=FILE
            write generated code to FILE instead of stdout
    
        -x,--execute
            generate extra code to test and display the class
    
        -d,--debug
            show debug output
    
        -iN,--ident=N
            set indent width to N spaces, tab if N is 0 (default: 4)
    

    I usually use it like this:

    pyside-uic -o output.py input.ui
    
    0 讨论(0)
  • 2020-12-01 13:03

    Using QtUiTools (as suggested in another answer) is currently discouraged by the PySide team.

    Read the full story here: https://groups.google.com/forum/?fromgroups=#!topic/pyside/_s1HPe6XTZs

    0 讨论(0)
  • 2020-12-01 13:05
    import pysideuic
    import xml.etree.ElementTree as xml
    from cStringIO import StringIO
    
    def loadUiType(uiFile):
        """
        Pyside "loadUiType" command like PyQt4 has one, so we have to convert the 
        ui file to py code in-memory first and then execute it in a special frame
        to retrieve the form_class.
        """
        parsed = xml.parse(uiFile)
        widget_class = parsed.find('widget').get('class')
        form_class = parsed.find('class').text
    
        with open(uiFile, 'r') as f:
            o = StringIO()
            frame = {}
    
            pysideuic.compileUi(f, o, indent=0)
            pyc = compile(o.getvalue(), '<string>', 'exec')
            exec pyc in frame
    
            # Fetch the base_class and form class based on their type
            # in the xml from designer
            form_class = frame['Ui_%s'%form_class]
            base_class = eval('QtGui.%s'%widget_class)
    
        return form_class, base_class
    

    You can use this way to load the UI and can also get form_class as well as the base class as return type... but if you do not want to convert, otherwise Yes the following is the correct way.

    pyside-uic.exe MyWindow.ui -o MyWindow.py
    
    0 讨论(0)
  • 2020-12-01 13:17
    pyside-uic.exe MyWindow.ui -o MyWindow.py 
    

    is what I've been doing and it's working fine (as far as I know)

    0 讨论(0)
  • 2020-12-01 13:19

    QUiLoader class will do the job without making an intermediate file.

    http://www.pyside.org/docs/pyside/PySide/QtUiTools/QUiLoader.html

    0 讨论(0)
  • 2020-12-01 13:20

    Just tried Pyside's QUILoader, works fine:

    from PySide import QtGui  
    from PySide import QtCore
    from PySide import QtUiTools
    
    class MyWidget(QtGui.QMainWindow):
        def __init__(self, *args):  
           apply(QtGui.QMainWindow.__init__, (self,) + args)
    
           loader = QtUiTools.QUiLoader()
           file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
           file.open(QtCore.QFile.ReadOnly)
           self.myWidget = loader.load(file, self)
           file.close()
    
           self.setCentralWidget(self.myWidget)
    
    if __name__ == '__main__':  
       import sys  
       import os
       print("Running in " + os.getcwd() + " .\n")
    
       app = QtGui.QApplication(sys.argv)  
    
       win  = MyWidget()  
       win.show()
    
       app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
                   app, QtCore.SLOT("quit()"))
       app.exec_()
    

    I used Eclipse and QTDesigner to create the .ui file (right-click on module, "New -> Other..", choose "Qt Designer -> Qt Designer Form"). No explicit uic call is needed.

    0 讨论(0)
提交回复
热议问题