欢迎访问我的个人网站,网站正在创建期间,主要供自己学习交流使用!

PyQt5窗口间的数据传递



  1. 在发送端母窗口中定义pyqtSignal实例,并赋值,并调用pyqtSignal.emit函数发送参数。信号可以带任何形式的参数。

 

   signal1 = pyqtSignal()  # 无参数

    signal2 = pyqtSignal(int)  # 1个字符串参数

    signal3 = pyqtSignal(int, str) # 2个不同类型参数,数字和字符串

   signal4 = pyqtSignal(list) # 列表

    signal5 = pyqtSignal(dict) # 字典

# 可以重载的不同类型及不同个数的参数

    signal6 = pyqtSignal([int, str], [str])

示例如下:

class Product_Read_Window(QMainWindow, Raws_database):

    _send_list = pyqtSignal(list)

    def __init__(self, parent=None):

        super().__init__(parent)

        self.setupUi(self)

        self.row_num = 0

        self._table_settings()

 

    def on_ChooseButton_clicked(self):

        choose_num = 0

        choose_list = []

        self.send_list = []

        for i in range(self.row_num):

            if self.Raws_chem_table.item(i, 0).checkState():

                choose_num += 1

                choose_list.append(i)

        if (choose_num > 4)or(choose_num == 0):

            dia_err_choose = Raw_content_err_Dialog()

            dia_err_choose.err_msg.setText("选择的岩棉产品数量错误!")

            dia_err_choose.exec_()

            return 1

        else:

            for i in choose_list:

                self.send_list.append(self.product_chem_list[i])

        self._send_list.emit(self.send_list)

        self.close()

  1. 在接收端窗口中,调用发送端窗口子类,并使用pyqtSignal.connect函数触发相应函数,并将参数传递给函数。发送子窗口一旦触发emit发送参数,将会触发接收端触self.product_input函数:

def on_Button_Pdt_Read_clicked(self):       

self.dia_pdt_data = Product_Read_Window()

self.dia_pdt_data.show()

    self.dia_pdt_data._send_list.connect(self.product_input)

def product_input(self, input_list):

chem_list = ['Name','Si','Al','Ca','Mg','Fe','Ti','Na','K','Mn']

        for j in range(1, 4):

            for i in chem_list:

                eval('self.label_%s_%s.setText("")'%(i, j))

        self.Pdt1_Emp = 1

        self.Pdt2_Emp = 1

        self.Pdt3_Emp = 1

        self.Pdt4_Emp = 1

        product_num = len(input_list)

        for j in range(1, product_num+1):

            for i, k in enumerate(chem_list, 1):

                eval('self.label_%s_%s.setText("%s")'%(k, j,input_list[j-1][i]))

            exec("self.Pdt%s_Emp=0"%j)


湘公网安备43012102001039    湘ICP备14007447号-1    邮箱:35744243#qq.com