简体   繁体   中英

Connec pyqt signal with another class

Is it possible for me to get pyqt signals to trigger a method from another class ? I've tried all sorts but with no luck. My aim is to trigger the pickFile() method in the get_rooms when the room_file_button is clicked (marked).

import sys
from PyQt4 import QtCore, QtGui, uic
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Protection
import xlrd
import csv
import os
import re 

class MyApp(QtGui.QMainWindow, Ui_MainWindow):    

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)


        self.room_file_button.clicked.connect(get_rooms.pickFile)   # this one
        self.radioButton_1.clicked.connect(self.onRadioButton1)       
        self.radioButton_2.clicked.connect(self.onRadioButton2)       
        self.radioButton_3.clicked.connect(self.onRadioButton3) 
        self.spinBox.valueChanged.connect(self.valuechange)


class first_file(MyApp):            
    def __init__(self):
        MyApp.__init__(self)

        some methods .... 


class get_rooms(MyApp):

    def __init__(self):
        MyApp.__init__(self)

    def pickFile(self, value, group_1):
        print 'yipeee !'
        xy = 0
        while True:
            filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
            if filename == '' and xy < 2:
                print(" ")
                xy = xy + 1
                continue
            elif filename != '':
                break
            else:
                sys.exit()

At first, you can set pickFile function as static function (without self):

class get_rooms(MyApp):

    def __init__(self):
        MyApp.__init__(self)

    @staticmethod
    def pickFile(value, group_1):

and then you can use room_file_button.clicked signal; if you want to send parameters to that function, you can use lambda :

self.room_file_button.clicked.connect(lambda: get_rooms.pickFile(myValue,myGrupe))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM