zoukankan      html  css  js  c++  java
  • pyqt的信号槽机制(转)

    This article is part 5 of 8 in the series Python PySide/PyQt Tutorial
    Published: Wednesday 6th February 2013 
    Last Updated: Tuesday 9th December 2014
     
     

    You don't have to rely solely on the signals that are provided by Qt widgets, however; you can create your own. Signals are created using the Signal class. A simple signal definition would be:

    Then, when the conditions for the object being tapped are satisfied, you call the signal's emit method, and the signal is emitted, calling any slots to which it is connected:

    This is good for two reasons; first, it allows users of your objects to interact with them in familiar ways; and second, it allows your objects to be used more flexibly, leaving the definition effects of actions on your object to the code that uses them.

    A Simple PySide/PyQt Signal Emitting Example

    Let's define a simple PunchingBag class that does only one thing: when its punch is called, it emits a punchedsignal:

    You can easily see what we've done. The PunchingBag inherits from QObject so it can emit signals; it has a signal called punched, which carries no data; and it has a punch method which does nothing but emit the punched signal.

    To make our PunchingBag useful, we need to connect its punched signal to a slot that does something. We'll define a simple one that prints, "Bag was punched" to the console, instantiate our PunchingBag, and connect its punched signal to the slot:

    Then, we'll punch the bag and see what happens:

    When you put it all in a script and run it, it will print:

    Effective, but not particularly impressive. However, you can see the usefulness of it: our punching bag would be a good fit anywhere you need a bag that reacts to punching, because the PunchingBag leaves implementation of a reaction to punching to the code that uses it.

    Data-Carrying PySide/PyQt Signals

    One of the most interesting things you can do when creating signals is to make them carry data. For example, you could make a signal carry an integer, thus:

    or a string:

    The datatype may be any Python type name or a string identifying a C++ datatype. Since this tutorial presupposes no C++ knowledge, we'll stick to Python types.

    A PySide/PyQt Signal-Sending Circle

    Let's define a Circle with properties xy, and r, denoting the x and y position of the center of the circle, and its radius, respectively. You might want to have one signal that is emitted when the circle is resized, and another that is emitted when it is moved; we'll call them resized and moved, respectively.

    It would be possible to have the slots to which the resized and moved signals are connected check the new position or size of the circle and respond accordingly, but it's more convenient and requires less knowledge of circles by the slot functions if the signal that is sent can include that information.

    Note these salient points:

    • The Circle inherits from QObject so it can emit signals.
    • The signals are created with the signature of the slot to which they will be connected.
    • The same signal can be emitted in multiple places.

    Now, let's define some slots that can be connected to the Circle's signals. Remember last time, when we said we'd see more about the @Slot decorator? We now have signals that carry data, so we'll see how to make slots that can receive it. To make a slot accept data from a signal, we simply define it with the same signature as its signal:

    Very simple and intuitive. For more information on Python decorators, you might want to checkout the article - Python Decorators Overview to familiarise yourself.

    Finally, let's instantiate a Circle, hook up the signals to the slots, and move and resize it:

    When you run the resulting script, your output should be:

    Now that we've developed a better understanding of signals and slots, we are ready to use some more advanced widgets. In our next instalment, we will begin to discuss the QListWidget and QListView, two ways of creating list box controls.

  • 相关阅读:
    7.29 H5学习笔记
    8.1H5学习笔记
    8.4 H5知识点总结
    8.15 CSS知识点6
    8.12 CSS知识点5
    HTTP协议简析(二)
    php实现二分查找法
    http协议简析(一)
    telnet客户端模拟浏览器发送请求
    导入txt文件到SQL SERVER 2008
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/6242089.html
Copyright © 2011-2022 走看看