设为首页 加入收藏

TOP

PyQt5 signal and slot(一)
2017-12-07 14:22:55 】 浏览:63
Tags:PyQt5 signal and slot

PyQt5 的 signal 与 slot 有所改变,例如,先定义一个 ZeroSignal 类:

class ZeroSignal(QObject):
    atzero = pyqtSignal(int)

使用时,一是绑定 slot 如下:

    self.zerosig = ZeroSignal()
       self.zerosig.atzero[int].connect(self.countZero)

然后是找个机会发动之:

def checkZero(self):
        if self.value() == 0:
            self.zerosig.atzero.emit(self.value())

大约如此,完整代码如下:

import os
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class ZeroSignal(QObject):
    atzero = pyqtSignal(int)

class ZeroSpinBox(QSpinBox):
    def __init__(self, parent=None):
        super(ZeroSpinBox, self).__init__(parent)
        self.zeros = 0
        self.valueChanged[int].connect(self.checkZero)
        self.zerosig = ZeroSignal()
        self.zerosig.atzero[int].connect(self.countZero)

    def countZero(self, v):
        if v == 0:
            self.zeros += 1
            print(self.zeros)

    def checkZero(self):
        if self.value() == 0:
            self.zerosig.atzero.emit(self.value())
            



class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        
        dial = QDial()
        dial.setNotchesVisible(True)
        spin = ZeroSpinBox()
        layout = QHBoxLayout()
        layout.addWidget(dial)
        layout.addWidget(spin)
        self.setLayout(layout)
        dial.valueChanged.connect(spin.setValue)
        spin.valueChanged.connect(dial.setValue)
        spin.valueChanged.connect(self.emitZero)
        self.setWindowTitle("Sinal and Solt")
        
        self.zerobox = spin 
        self.zerobox.zerosig.atzero[int].connect(self.annouce)

    def emitZero(self, v):
        if v == 0:
            self.zerobox.zerosig.atzero.emit(self.zerobox.zeros)

    def annouce(self, v):
        print("zero count: %d" % v)  # print two times because add ZeroSpinBox emit once

def run():
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

if __name__ == '__main__':
    run()

关于对话框的代码如下,尤其 NumberLiveDialog 值得推荐:

import sys,math,random,string
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class NumberDialog(QDialog):
    def __init__(self, format, parent=None):
        super(NumberDialog, self).__init__(parent)

        thousandsLabel = QLabel("&Thousands separator:")
        self.thousandsEdit = QLineEdit(format["thousandsseparator"])
        thousandsLabel.setBuddy(self.thousandsEdit)
        decimalMarkerLabel = QLabel("Decimal $marker:")
        self.decimalMarkerEdit = QLineEdit(format["decimalmarker"])
        decimalMarkerLabel.setBuddy(self.decimalMarkerEdit)
        decimalPlacesLabel = QLabel("&Decimal places:")
        self.decimalPlacesSpinBox = QSpinBox()
        decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox)
        self.decimalPlacesSpinBox.setRange(0, 6)
        self.decimalPlacesSpinBox.setValue(format["decimalplaces"])
        self.redNegativesCheckBox = QCheckBox("&Red negative numbers")
        self.redNegativesCheckBox.setChecked(format["rednegatives"])

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.format = format.copy()

        grid = QGridLayout()
        grid.addWidget(thousandsLabel, 0, 0)
        grid.addWidget(self.thousandsEdit, 0, 1)
        grid.addWidget(decimalMarkerLabel, 1, 0)
        grid.addWidget(self.decimalMarkerEdit, 1, 1)
        grid.addWidget(decimalPlacesLabel, 2
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇从CentOS安装完成到生成词云pytho.. 下一篇python装饰器探究与参数的领取

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目