Using Qt Designer
=================
Qt Designer is the Qt tool for designing and building graphical user
interfaces. It allows you to design widgets, dialogs or complete main windows
using on-screen forms and a simple drag-and-drop interface. It has the ability
to preview your designs to ensure they work as you intended, and to allow you
to prototype them with your users, before you have to write any code.
Qt Designer uses XML ``.ui`` files to store designs and does not generate any
code itself. Qt includes the ``uic`` utility that generates the C++ code that
creates the user interface. Qt also includes the ``QUiLoader`` class that
allows an application to load a ``.ui`` file and to create the corresponding
user interface dynamically.
PyQt5 does not wrap the ``QUiLoader`` class but instead includes the
:mod:`~PyQt5.uic` Python module. Like ``QUiLoader`` this module can load
``.ui`` files to create a user interface dynamically. Like the :program:`uic`
utility it can also generate the Python code that will create the user
interface. PyQt5's :program:`pyuic5` utility is a command line interface to
the :mod:`~PyQt5.uic` module. Both are described in detail in the following
sections.
Using the Generated Code
------------------------
The code that is generated has an identical structure to that generated by Qt's
``uic`` and can be used in the same way.
The code is structured as a single class that is derived from the Python
``object`` type. The name of the class is the name of the toplevel object set
in Designer with ``Ui_`` prepended. (In the C++ version the class is defined
in the ``Ui`` namespace.) We refer to this class as the *form class*.
The class contains a method called ``setupUi()``. This takes a single argument
which is the widget in which the user interface is created. The type of this
argument (typically :class:`~PyQt5.QtWidgets.QDialog`,
:class:`~PyQt5.QtWidgets.QWidget` or :class:`~PyQt5.QtWidgets.QMainWindow`) is
set in Designer. We refer to this type as the *Qt base class*.
In the following examples we assume that a ``.ui`` file has been created
containing a dialog and the name of the :class:`~PyQt5.QtWidgets.QDialog`
object is ``ImageDialog``. We also assume that the name of the file containing
the generated Python code is :file:`ui_imagedialog.py`. The generated code can
then be used in a number of ways.
The first example shows the direct approach where we simply create a simple
application to create the dialog::
import sys
from PyQt5.QtWidgets import QApplication, QDialog
from ui_imagedialog import Ui_ImageDialog
app = QApplication(sys.argv)
window = QDialog()
ui = Ui_ImageDialog()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
The second example shows the single inheritance approach where we sub-class
:class:`~PyQt5.QtWidgets.QDialog` and set up the user interface in the
``__init__()`` method::
from PyQt5.QtWidgets import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog):
def __init__(self):
super(ImageDialog, self).__init__()
# Set up the user interface from Designer.
self.ui = Ui_ImageDialog()
self.ui.setupUi(self)
# Make some local modifications.
self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.ui.okButton.clicked.connect(self.accept)
self.ui.cancelButton.clicked.connect(self.reject)
The third example shows the multiple inheritance approach::
from PyQt5.QtGui import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog, Ui_ImageDialog):
def __init__(self):
super(ImageDialog, self).__init__()
# Set up the user interface from Designer.
self.setupUi(self)
# Make some local modifications.
self.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.okButton.clicked.connect(self.accept)
self.cancelButton.clicked.connect(self.reject)
It is also possible to use the same approach used in PyQt v3. This is shown in
the final example::
from ui_imagedialog import ImageDialog
class MyImageDialog(ImageDialog):
def __init__(self):
super(MyImageDialog, self).__init__()
# Make some local modifications.
self.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.okButton.clicked.connect(self.accept)
self.cancelButton.clicked.connect(self.reject)
For a full description see the Qt Designer Manual in the Qt Documentation.
The :mod:`~PyQt5.uic` Module
----------------------------
The :mod:`~PyQt5.uic` module contains the following functions and objects.
.. currentmodule:: PyQt5.uic
.. data:: widgetPluginPath
The list of the directories that are searched for widget plugins.
Initially it contains the name of the directory that contains the widget
plugins included with PyQt5.
.. function:: compileUi(uifile, pyfile[, execute=False[, indent=4[, from_imports=False[, resource_suffix='_rc']]]])
Generate a Python module that will create a user interface from a Qt
Designer ``.ui`` file.
:param uifile:
the file name or file-like object containing the ``.ui`` file.
:param pyfile:
the file-like object to which the generated Python code will be written
to.
:param execute:
is optionally set if a small amount of additional code is to be
generated that will display the user interface if the code is run as a
standalone application.
:param indent:
the optional number of spaces used for indentation in the generated
code. If it is zero then a tab character is used instead.
:param from_imports:
is optionally set to generate import statements that are relative to
``'.'``. At the moment this only applies to the import of resource
modules.
:resource_suffix:
is the suffix appended to the basename of any resource file specified
in the ``.ui`` file to create the name of the Python module generated
from the resource file by ``pyrcc5``. The default is ``'_rc'``, i.e.
if the ``.ui`` file specified a resource file called ``foo.qrc`` then
the corresponding Python module is ``foo_rc``.
.. function:: compileUiDir(dir[, recurse=False[, map=None[, \*\*compileUi_args]]])
Create Python modules from Qt Designer ``.ui`` files in a directory or
directory tree.
:param dir:
the name of the directory to scan for files whose name ends with
``.ui``. By default the generated Python module is created in the same
directory ending with ``.py``.
:param recurse:
is optionally set if any sub-directories should be scanned.
:param map:
an optional callable that is passed the name of the directory
containing the ``.ui`` file and the name of the Python module that will
be created. The callable should return a tuple of the name of the
directory in which the Python module will be created and the (possibly
modified) name of the module.
:param compileUi_args:
are any additional keyword arguments that are passed to
:func:`~PyQt5.uic.compileUi` that is called to create each Python
module.
.. function:: loadUiType(uifile[, from_imports=False[, resource_suffix='_rc']])
Load a Qt Designer ``.ui`` file and return a tuple of the generated
*form class* and the *Qt base class*. These can then be used to
create any number of instances of the user interface without having to
parse the ``.ui`` file more than once.
:param uifile:
the file name or file-like object containing the ``.ui`` file.
:param from_imports:
is
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
PyQt-gpl-5.5.1.tar.gz (2000个子文件)
qpyopengl_add_constants.cpp 205KB
qpycore_chimera.cpp 45KB
translator.cpp 35KB
fetchtr.cpp 32KB
qpycore_pyqtboundsignal.cpp 29KB
metatranslator.cpp 27KB
qpycore_types.cpp 21KB
qpycore_pyqtsignal.cpp 21KB
rcc.cpp 18KB
qpyqmlobject.cpp 18KB
qpycore_pyqtproperty.cpp 15KB
qpyopengl_uniform_value_array.cpp 13KB
main.cpp 12KB
qpyqml_register_type.cpp 11KB
dbus.cpp 11KB
qpycore_public_api.cpp 11KB
qpyopengl_value_array.cpp 11KB
pluginloader.cpp 10KB
qpycore_pyqtslotproxy.cpp 10KB
simtexth.cpp 10KB
qpyqmllistproperty.cpp 9KB
merge.cpp 9KB
pluginloader.cpp 9KB
qpycore_qobject_helpers.cpp 9KB
numberh.cpp 9KB
qpyqmllistpropertywrapper.cpp 9KB
qpycore_pyqtmethodproxy.cpp 9KB
proparser.cpp 8KB
qpycore_pyqtslot.cpp 8KB
qpyopengl_attribute_array.cpp 7KB
main.cpp 7KB
qpycore_pyqtconfigure.cpp 7KB
qpyquickframebufferobject.cpp 6KB
qpycore_qvariant_value.cpp 6KB
qpyquickpainteditem.cpp 6KB
qpydbus_chimera_helpers.cpp 6KB
qpycore_qstring.cpp 5KB
qpyquickitem.cpp 5KB
qpycore_qmetaobject.cpp 5KB
qpyqml_register_singleton_type.cpp 5KB
qpyopengl_data_cache.cpp 5KB
qpycore_qmetaobject_helpers.cpp 5KB
qpycore_pyqtpyobject.cpp 4KB
qpycore_decorators.cpp 4KB
qpycore_qjsonvalue.cpp 4KB
qpyopengl_array_convertors.cpp 4KB
qpyquick_chimera_helpers.cpp 4KB
qpyopengl_version_functions.cpp 4KB
qpycore_qobject_getattr.cpp 4KB
sametexth.cpp 4KB
qpycore_enums_flags.cpp 4KB
qpydbusreply.cpp 3KB
qpycore_chimera_storage.cpp 3KB
qpycore_init.cpp 3KB
qpyqmlsingletonobject.cpp 3KB
qpycore_qmessagelogger.cpp 3KB
qpycore_chimera_signature.cpp 3KB
qpyqml_qjsvalue.cpp 3KB
qpyqml_post_init.cpp 3KB
qpyquick_post_init.cpp 2KB
qpyopengl_init.cpp 2KB
qpyquick_register_type.cpp 2KB
qpycore_classinfo.cpp 2KB
qpydbuspendingreply.cpp 2KB
qpyqml_listdata.cpp 2KB
qpycore_misc.cpp 2KB
qpycore_qvariant.cpp 2KB
qpydbus_post_init.cpp 1KB
basic.css 9KB
classic.css 4KB
classic.css 4KB
pygments.css 4KB
translator.h 15KB
qpycore_chimera.h 9KB
qpyqmlobject.h 7KB
metatranslator.h 7KB
rcc.h 6KB
qpycore_public_api.h 5KB
qpycore_api.h 4KB
qpycore_qmetaobjectbuilder.h 3KB
qpycore_pyqtslotproxy.h 3KB
simtexth.h 3KB
qpyquickframebufferobject.h 3KB
qpyqmlsingletonobject.h 3KB
qpyquickpainteditem.h 3KB
qpyopengl_api.h 3KB
qpyopengl_data_cache.h 3KB
qpyquickitem.h 2KB
qpycore_pyqtsignal.h 2KB
qpycore_pyqtslot.h 2KB
qpyqml_api.h 2KB
qpycore_types.h 2KB
proparser.h 2KB
qpycore_pyqtproperty.h 2KB
helper.h 2KB
qpycore_pyqtboundsignal.h 2KB
qpyqmllistpropertywrapper.h 2KB
qpydbusreply.h 2KB
qpycore_pyqtpyobject.h 2KB
qpycore_pyqtmethodproxy.h 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Jimmy-G
- 粉丝: 66
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功