pyqt父子窗口传值-转自论坛

import sys
from PyQt4 import QtCore, QtGui

style = “”"
.QPushButton{
border-style:none;
border:1px solid #C2CCD8;
color:#F0F0F0;
padding:5px;
min-height:20px;
border-radius:5px;
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #4D4D4D,stop:1 #292929);
}
“”"

class W2(QtGui.QDialog):
def init(self, parent=None):
QtGui.QWidget.init(self, parent)
self.lineEdit = QtGui.QLineEdit()

    self.label1 = QtGui.QLabel(u"从父窗口接收")
    self.label2 = QtGui.QLabel(u"发送给父窗口")
    self.lineEdit1 = QtGui.QLineEdit()
    self.lineEdit2 = QtGui.QLineEdit() 
    
    self.button2 = QtGui.QPushButton(u'发送',self)
    
    layout = QtGui.QGridLayout()
    layout.addWidget(self.label1,0,0)
    layout.addWidget(self.lineEdit1,0,1)
    layout.addWidget(self.label2,1,0)
    layout.addWidget(self.lineEdit2,1,1)
    layout.addWidget(self.button2,2,1)
    
    self.setLayout(layout)
    self.setStyleSheet(style)
    self.button2.clicked.connect(self.transfer)
    
    
def receive(self,s):
    print (u'接受到父窗口值')
    self.lineEdit1.setText(str(s))        
    
def transfer(self):
    str = self.lineEdit2.text()
    self.emit(QtCore.SIGNAL("transfer_father"),str)

class MyForm(QtGui.QDialog):
def init(self, parent=None):
QtGui.QWidget.init(self, parent)
self.setWindowTitle(u’父窗口’)

    self.label1 = QtGui.QLabel(u"从子窗口接收")
    self.label2 = QtGui.QLabel(u"发送给子窗口")
    self.lineEdit1 = QtGui.QLineEdit()
    self.lineEdit2 = QtGui.QLineEdit() 
    
    
    self.button1 = QtGui.QPushButton(u'子窗口',self)
    self.button2 = QtGui.QPushButton(u'发送',self)
    
    layout = QtGui.QGridLayout()
    layout.addWidget(self.label1,0,0)
    layout.addWidget(self.lineEdit1,0,1)
    layout.addWidget(self.label2,1,0)
    layout.addWidget(self.lineEdit2,1,1)
    layout.addWidget(self.button1,2,0)
    layout.addWidget(self.button2,2,1)
    self.setLayout(layout)

    self.button1.clicked.connect(self.child)
    self.button2.clicked.connect(self.transfer)
    self.setStyleSheet(style)


def child(self):
    print (u'弹出子窗口')
    self.w2 = W2()
    self.connect(self.w2, QtCore.SIGNAL("transfer_father"), self.receive)
    self.w2.show()
    self.w2.connect(self, QtCore.SIGNAL("transfer_child"), self.w2.receive)

     
@QtCore.pyqtSlot(str) 
def receive(self,s):
    print (u'接受到子窗口值')
    self.lineEdit1.setText(str(s))

def transfer(self):
    str = self.lineEdit2.text() 
    self.emit(QtCore.SIGNAL("transfer_child"),str)

if name == “main”:
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值