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.

  • 相关阅读:
    4星|《门口的野蛮人2》:美国宝万之争专业户KKR公司的疯狂借债收购史
    3星|《多样性红利》:10年旧书,《群体智慧》的注解版,习题集性质
    4星|《财经》2018年第26期:全国流感疫苗供应不足
    互联网行业的左其盛好评书21本
    华为相关6本书,都不值得读
    3.5星|《产品觉醒》:针对互联网商业和产品的评论与思考,有实践有洞察
    3星|埃森哲《机器与人》:基于人工智能新闻甚至宣传稿的畅想与分析
    3星|《华为终端战略》:华为手机大事记,企业宣传稿风格
    2.5星|《极简企业史》:资料汇编,一大半内容是相关图书简介
    AspNet Core 发布到Linux系统和发布IIS 注意项
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/6242089.html
Copyright © 2011-2022 走看看