3 ways to use QT Designer files

Source: Internet
Author: User
Tags qt designer

The. UI interface files that are designed using QT designers are XML files and are used in 3 ways in PYQT, which is explained in the example.

Examples of how to use PYQT in conjunction with QT designers in development. Click the button to modify the contents of the label.

1. Working directly with UI files

The PyQt4 UI module provides the ability to load the. ui file, and the Ui.uic.loadUi (uifile[, Baseinstance=none]) method returns an instance of the Qwdiget subclass.

Pros: Do not manually convert. ui files, use directly.
Disadvantage: Low efficiency, one more in the process of conversion; UI files are exposed and are not conducive to publication.
The code is as follows (widget1.py):

#-*-Coding:utf-8-*-
From PYQT4 import Qtgui, UIC

Class Widget (Qtgui.qwidget):
def __init__ (self, Parent=none):
Qtgui.qwidget.__init__ (self, parent)
Self.ui = Uic.loadui (' Widget.ui ')
Self.ui.show ()

Self.ui.pbHello.clicked.connect (Self.sayhello)

def SayHello (self):
Self.ui.lHello.setText ("Hello PyQt4")

if __name__ = = ' __main__ ':
Import Sys
App = Qtgui.qapplication (SYS.ARGV)
Widget = Widget ()
Sys.exit (App.exec_ ())
2. Post-conversion loading using

The PYUIC4 command is first transferred to a. py file and then called by Setupui (). Similar to Method 1, there is a more conversion process.

Advantages: The interface loading speed is quick and convenient to pack and publish.
Disadvantage: The object in the call interface needs to pass Self.ui, the code is cumbersome to write, each time you modify the. ui file, you need to convert.
Convert first:

$ pyuic4-o ui_widget.py Widget.ui
The code is as follows (widget2.py):

#-*-Coding:utf-8-*-
From PYQT4 import Qtgui
From Ui_widget import Ui_form

Class Widget (Qtgui.qwidget):
def __init__ (self, Parent=none):
Qtgui.qwidget.__init__ (self, parent)
Self.ui = Ui_form ()
Self.ui.setupUi (self)

Self.ui.pbHello.clicked.connect (Self.sayhello)

def SayHello (self):
Self.ui.lHello.setText ("Hello PyQt4")

if __name__ = = ' __main__ ':
Import Sys
App = Qtgui.qapplication (SYS.ARGV)
Widget = Widget ()
Widget.show ()
Sys.exit (App.exec_ ())
3. Using multiple inheritance after conversion

Similar to Method 2, but it is used in multiple inheritance when writing classes.

Advantages: The code is simple to write, the interface object can be referenced by Self.objectname method, and the signal and slot can be bound using decorator mode.
Disadvantage: conversion is required after each modification of the. ui file.
The code is as follows (widget3.py):

#-*-Coding:utf-8-*-
From PYQT4 import Qtgui, Qtcore
From Ui_widget import Ui_form

Class Widget (Qtgui.qwidget, Ui_form):
def __init__ (self, Parent=none):
Qtgui.qwidget.__init__ (self, parent)
Self.setupui (self)

@QtCore. Pyqtsignature ("")
def on_pbhello_clicked (self):
Self.lHello.setText (' Hello PyQt4 ')

if __name__ = = ' __main__ ':
Import Sys
App = Qtgui.qapplication (SYS.ARGV)
Widget = Widget ()
Widget.show ()
Sys.exit (App.exec_ ())

3 ways to use QT Designer files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.