• PySide 模块
  • PySide.QtUiTools
  • 内容表表

    上一话题

    PySide.QtUiTools

    下一话题

    PySide.QtXml

    QUiLoader

    概要

    函数

    虚函数

    详细描述

    PySide.QtUiTools.QUiLoader class enables standalone applications to dynamically create user interfaces at run-time using the information stored in UI files or specified in plugin paths.

    In addition, you can customize or create your own user interface by deriving your own loader class.

    If you have a custom component or an application that embeds Qt Designer , you can also use the QFormBuilder class provided by the QtDesigner module to create user interfaces from UI files.

    PySide.QtUiTools.QUiLoader class provides a collection of functions allowing you to create widgets based on the information stored in UI files (created with Qt Designer ) or available in the specified plugin paths. The specified plugin paths can be retrieved using the PySide.QtUiTools.QUiLoader.pluginPaths() function. Similarly, the contents of a UI file can be retrieved using the PySide.QtUiTools.QUiLoader.load() function. For example:

    class MyWidget(QWidget):
        def __init__(self, parent):
            super(QWidget, self).__init__(parent)
            loader = QUiLoader()
            file = QFile(":/forms/myform.ui")
            file.open(QFile.ReadOnly)
            myWidget = loader.load(file, self)
            file.close()
            layout = QVBoxLayout()
            layout.addWidget(myWidget)
            self.setLayout(layout)
    										

    By including the user interface in the form's resources ( myform.qrc ), we ensure that it will be present at run-time:

    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/forms">
    <file>myform.ui</file>
    </qresource>
    </RCC>
    									

    PySide.QtUiTools.QUiLoader.availableWidgets() function returns a PySide.QtCore.QStringList with the class names of the widgets available in the specified plugin paths. To create these widgets, simply use the PySide.QtUiTools.QUiLoader.createWidget() function. For example:

    def loadCustomWidget(parent):
        loader = QUiLoader()
        availableWidgets = loader.availableWidgets()
        if availableWidgets.contains("AnalogClock"):
            myWidget = loader.createWidget("AnalogClock", parent)
        return myWidget
    										

    To make a custom widget available to the loader, you can use the PySide.QtUiTools.QUiLoader.addPluginPath() function; to remove all available widgets, you can call the PySide.QtUiTools.QUiLoader.clearPluginPaths() 函数。

    PySide.QtUiTools.QUiLoader.createAction() , PySide.QtUiTools.QUiLoader.createActionGroup() , PySide.QtUiTools.QUiLoader.createLayout() ,和 PySide.QtUiTools.QUiLoader.createWidget() functions are used internally by the PySide.QtUiTools.QUiLoader class whenever it has to create an action, action group, layout, or widget respectively. For that reason, you can subclass the PySide.QtUiTools.QUiLoader class and reimplement these functions to intervene the process of constructing a user interface. For example, you might want to have a list of the actions created when loading a form or creating a custom widget.

    For a complete example using the PySide.QtUiTools.QUiLoader class, see the Calculator Builder Example .

    另请参阅

    QtUiTools QFormBuilder

    class PySide.QtUiTools. QUiLoader ( [ parent=None ] )
    参数: parent PySide.QtCore.QObject

    Creates a form loader with the given parent .

    PySide.QtUiTools.QUiLoader. addPluginPath ( path )
    参数: path – unicode

    添加给定 path to the list of paths in which the loader will search when locating plugins.

    PySide.QtUiTools.QUiLoader. availableLayouts ( )
    返回类型: list of strings

    Returns a list naming all available layouts that can be built using the PySide.QtUiTools.QUiLoader.createLayout() function

    PySide.QtUiTools.QUiLoader. availableWidgets ( )
    返回类型: list of strings

    Returns a list naming all available widgets that can be built using the PySide.QtUiTools.QUiLoader.createWidget() function, i.e all the widgets specified within the given plugin paths.

    PySide.QtUiTools.QUiLoader. clearPluginPaths ( )

    Clears the list of paths in which the loader will search when locating plugins.

    PySide.QtUiTools.QUiLoader. createAction ( [ parent=None [ , name="" ] ] )
    参数:
    返回类型:

    PySide.QtGui.QAction

    Creates a new action with the given parent and name .

    The function is also used internally by the PySide.QtUiTools.QUiLoader class whenever it creates a widget. Hence, you can subclass PySide.QtUiTools.QUiLoader and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call PySide.QtUiTools.QUiLoader ‘s version first.

    PySide.QtUiTools.QUiLoader. createActionGroup ( [ parent=None [ , name="" ] ] )
    参数:
    返回类型:

    PySide.QtGui.QActionGroup

    Creates a new action group with the given parent and name .

    The function is also used internally by the PySide.QtUiTools.QUiLoader class whenever it creates a widget. Hence, you can subclass PySide.QtUiTools.QUiLoader and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call PySide.QtUiTools.QUiLoader ‘s version first.

    PySide.QtUiTools.QUiLoader. createLayout ( className [ , parent=None [ , name="" ] ] )
    参数:
    返回类型:

    PySide.QtGui.QLayout

    Creates a new layout with the given parent and name using the class specified by className .

    The function is also used internally by the PySide.QtUiTools.QUiLoader class whenever it creates a widget. Hence, you can subclass PySide.QtUiTools.QUiLoader and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call PySide.QtUiTools.QUiLoader ‘s version first.

    PySide.QtUiTools.QUiLoader. createWidget ( className [ , parent=None [ , name="" ] ] )
    参数:
    返回类型:

    PySide.QtGui.QWidget

    Creates a new widget with the given parent and name using the class specified by className . You can use this function to create any of the widgets returned by the PySide.QtUiTools.QUiLoader.availableWidgets() 函数。

    The function is also used internally by the PySide.QtUiTools.QUiLoader class whenever it creates a widget. Hence, you can subclass PySide.QtUiTools.QUiLoader and reimplement this function to intervene process of constructing a user interface or widget. However, in your implementation, ensure that you call PySide.QtUiTools.QUiLoader ‘s version first.

    PySide.QtUiTools.QUiLoader. isLanguageChangeEnabled ( )
    返回类型: PySide.QtCore.bool

    Returns true if dynamic retranslation on language change is enabled; returns false otherwise.

    PySide.QtUiTools.QUiLoader. isScriptingEnabled ( )
    返回类型: PySide.QtCore.bool

    Returns true if execution of scripts is enabled; returns false otherwise.

    PySide.QtUiTools.QUiLoader. isTranslationEnabled ( )
    返回类型: PySide.QtCore.bool

    Returns true if translation is enabled; returns false otherwise.

    PySide.QtUiTools.QUiLoader. load ( device [ , parentWidget=None ] )
    参数:
    返回类型:

    PySide.QtGui.QWidget

    Loads a form from the given device and creates a new widget with the given parentWidget to hold its contents.

    PySide.QtUiTools.QUiLoader. load ( arg__1 [ , parentWidget=None ] )
    参数:
    返回类型:

    PySide.QtGui.QWidget

    PySide.QtUiTools.QUiLoader. pluginPaths ( )
    返回类型: list of strings

    Returns a list naming the paths in which the loader will search when locating custom widget plugins.

    PySide.QtUiTools.QUiLoader. registerCustomWidget ( customWidgetType )
    参数: customWidgetType PyObject

    Registers a Python created custom widget to QUiLoader, so it can be recognized when loading a .ui file. The custom widget type is passed via the customWidgetType argument. This is needed when you want to override a virtual method of some widget in the interface, since duck punching will not work with widgets created by QUiLoader based on the contents of the .ui 文件。

    (Remember that duck punching virtual methods is an invitation for your own demise! )

    Let's see an obvious example. If you want to create a new widget it's probable you'll end up overriding QWidget ‘s paintEvent() 方法。

    class Circle(QWidget):
        def paintEvent(self, event):
            painter = QPainter(self)
            painter.setPen(self.pen)
            painter.setBrush(QBrush(self.color))
            painter.drawEllipse(event.rect().center(), 20, 20)
    # ...
    loader = QUiLoader()
    loader.registerCustomWidget(Circle)
    circle = loader.load('circle.ui')
    circle.show()
    # ...
    											
    PySide.QtUiTools.QUiLoader. setLanguageChangeEnabled ( enabled )
    参数: enabled PySide.QtCore.bool

    enabled is true, user interfaces loaded by this loader will automatically retranslate themselves upon receiving a language change event. Otherwise, the user interfaces will not be retranslated.

    PySide.QtUiTools.QUiLoader. setScriptingEnabled ( enabled )
    参数: enabled PySide.QtCore.bool

    enabled is true, the loader will be able to execute scripts. Otherwise, execution of scripts will be disabled.

    PySide.QtUiTools.QUiLoader. setTranslationEnabled ( enabled )
    参数: enabled PySide.QtCore.bool

    enabled is true, user interfaces loaded by this loader will be translated. Otherwise, the user interfaces will not be translated.

    注意

    This is orthogonal to languageChangeEnabled.

    PySide.QtUiTools.QUiLoader. setWorkingDirectory ( dir )
    参数: dir PySide.QtCore.QDir

    Sets the working directory of the loader to dir . The loader will look for other resources, such as icons and resource files, in paths relative to this directory.

    PySide.QtUiTools.QUiLoader. workingDirectory ( )
    返回类型: PySide.QtCore.QDir

    Returns the working directory of the loader.