PyQt Routine Learning-1-

Source: Internet
Author: User

Try Wxpython for a while and try to pyqt to see if the two are better. Online praise PyQt Many, should also not groundless, I saw a few pyqt demo, indeed very powerful, although wxpython is not bad. The more important reason is that wxwidgets the MFC message mechanism in Python seems a bit cumbersome, want to try Qt signal-slot way.

First, MDI-PYQT Demo

1. main window inherits from Qtgui.qmainwindow

2. Initialization process __init__ (self):

2.1 Code

        Super (MainWindow, self). __init__ ()

        Self.mdiarea = Qtgui.qmdiarea ()
        Self.mdiArea.setHorizontalScrollBarPolicy (QtCore.Qt.ScrollBarAsNeeded)
        Self.mdiArea.setVerticalScrollBarPolicy (QtCore.Qt.ScrollBarAsNeeded)
        self.setcentralwidget (Self.mdiarea)
The middle two long lines of code are clearly set when the sliding rails appear. In addition, we see the model of the QT MDI, create an MDI area first, then set the central component of the main window as it.

2.2 Code

        Self.mdiArea.subWindowActivated.connect (self.updatemenus)
        self.windowmapper = Qtcore.qsignalmapper (self)
        Self.windowmapper.mapped[qtgui.qwidget].connect (Self.setactivesubwindow)
First line: Triggers the updatemenus operation when the child window is activated.

Second line: Creating a signal Mapper

Third line: The binding signal is mapped to the Setactivesubwindow function, and the parameter passed into the function is the Qtgui.qwidget type.

Additional Instructions:

            Action.triggered.connect (Self.windowMapper.map)
            self.windowMapper.setMapping (Action, window)

Where the action is a menu item and window is a child window object. When you click on a menu item trigger action, the trigger itself is taken as a key value, and the value is searched for value in Windowmapper.map, which is the Windows Object window (SetMapping (Action, window) Bind the two to a dictionary, and then pass the window value into the Setactivesubwindow of the unified processing function that is bound by the mapper.

3. Add child windows

        Child = MDIChild ()
        Self.mdiArea.addSubWindow (child)


4. Get a list of child windows

Windows = Self.mdiArea.subWindowList ()

5. Add menu items

        Self.filemenu = Self.menubar (). AddMenu ("&file")
        self.fileMenu.addAction (self.newact)
        Self.fileMenu.addAction (self.openact)
        self.fileMenu.addAction (self.saveact)
        self.fileMenu.addAction ( Self.saveasact)
        Self.fileMenu.addSeparator ()
The Menu subkey is:

        Self.newact = Qtgui.qaction (Qtgui.qicon (':/images/new.png '), "&new",
                Self, shortcut=qtgui.qkeysequence.new ,
                statustip= "Create A new file", Triggered=self.newfile)




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.