10

Is it possible to pass variables through slots so I can print out certain text? Trying to pass variable 'DiffP' which is defined in another function to slot.

'DiffP' changes based on which file is selected.

def addLineEdit(self):
    try:
        self.clearLayout()
        self.FileButton ={}
        self.Input = {}
        self.TotalInput = []
        for i in range(int(self.numberLine.text())):
            self.FileButton[i] = QtWidgets.QPushButton(self.centralwidget)
            self.FileButton[i].setText('Case {}'.format(i+1))
            self.FileButton[i].setFlat(True)
            self.FileButton[i].setMaximumSize(QtCore.QSize(50, 50))
            self.hboxlayout[0].addWidget(self.FileButton[i])
            self.FileButton[i].clicked.connect(lambda i=i: self.openfile(i))
            self.buttonGroup.addButton(self.FileButton[i],i)
            self.buttonGroup.buttonClicked['int'].connect(self.input)

def searchfile(self,dir):
        with open(dir) as f:
            content = f.readlines()
            MainList = content[44].split()
            RPM = round(float(MainList[0]), 2)
            Ps = round(float(MainList[1]), 2)
            Ts = round(float(MainList[2]), 2)
            Pd = round(float(MainList[3]), 2)
            Ratio = round(Pd / Ps, 2)
            DiffP = round(Pd - Ps, 2)
@pyqtSlot(int)
def input(self,button_or_id,DiffP):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(str(DiffP))
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')

def openfile(self,i):
    filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Choose file')
    dir = filename[0]
    directory = os.path.split(dir)[0]
    return self.searchfile(dir)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Does this answer your question? [Python, how to pass an argument to a function pointer parameter?](https://stackoverflow.com/questions/13783211/python-how-to-pass-an-argument-to-a-function-pointer-parameter) – user202729 Aug 15 '21 at 06:31

1 Answers1

23

The problem can be solved in 2 ways:

Using lambda functions:

In general:

    obj.signal.connect(lambda param1, param2, ..., arg1=val1, arg2= value2, ... : fun(param1, param2,... , arg1, arg2, ....))

def fun(param1, param2,... , arg1, arg2, ....):
    [...]

where:

  • param1, param2, ... : are the parameters sent by the signal
  • arg1, arg2, ...: are the extra parameters that you want to spend

In your case:

    self.buttonGroup.buttonClicked['int'].connect(lambda i: self.input(i, "text"))

@pyqtSlot(int)
def input(self, button_or_id, DiffP):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(DiffP)
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')

Using functools.partial:

In general:

    obj.signal.connect(partial(fun, args1, arg2, ... ))

def fun(arg1, arg2, ..., param1, param2, ...):
    [...]

where:

  • param1, param2, ... : are the parameters sent by the signal
  • arg1, arg2, ...: are the extra parameters that you want to send

In your case:

from functools import partial

    [...]
    self.buttonGroup.buttonClicked['int'].connect(partial(self.input, "text"))


@pyqtSlot(int)
def input(self, DiffP, button_or_id):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(DiffP)
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I added more code to show my issue. 'DiffP' is a variable from another function. I wasn't very specific to begin with. I used slot to identify which button is pressed, but I want to get variables from the searchfile function and use them in the slot, is that possible? –  Jul 13 '17 at 21:46
  • 1
    Yes, the file is going to change, based on which the user selects. So 'DiffP ' may be different values. –  Jul 13 '17 at 21:50
  • I used QButtonGroup to keep track of the buttons and add an id to each one of them, it is all I could think of to make sure which button was clicked –  Jul 13 '17 at 21:53
  • I copy and pasted from another function, and didn't delete it, I changed the code above to reflect it. –  Jul 13 '17 at 21:58
  • It was in case I needed to access a certain button by a key –  Jul 13 '17 at 22:07
  • Your code works, but can you return variables and not the formula itself? I plan to use multiple variables from the searchfile function for each button –  Jul 13 '17 at 22:13
  • All of them (Ps,Pd,Ts,Ratio..etc), there are going to be more variables, around 50. –  Jul 13 '17 at 22:16
  • After removing the FileButton dictionary, it crashes when I click on a file button, replaced code with what you gave. –  Jul 13 '17 at 22:28
  • Directory is for a graph function, which requires just the directory where the user selected file. –  Jul 13 '17 at 22:30
  • I've rechecked my code, could the fact that the FileButtons are placed in a QHBoxLayout, which is a dictionary? –  Jul 13 '17 at 22:33
  • The code is failing for me at " t= self.openfile()', I am using the code you provided –  Jul 13 '17 at 22:42
  • It's not providing one, it just crashes. –  Jul 13 '17 at 22:44
  • Same thing, it seems to be happening when it goes to self.searchhfile(dir). When I comment it out, it opens the file dialog twice and doesn't crash –  Jul 13 '17 at 22:50
  • I'm unable to post all code, as I have only access to one part of it. I got code to work it was due to the 'str' in front of the variables in the searchfile function. However, still having an issue, with the current code you provided when I click on button '0' it opens QFileDialog twice, and doesn't get information from file. In other words its selecting the same button twice –  Jul 14 '17 at 14:11
  • Issue is that clicking 'Case [x]' button opens FileDialog twice, and doesn't paste anything from file. 'Case [x]' are flat buttons.. I believe it has something to do with the buttonGroup, its iterating through and doing it for every button –  Jul 14 '17 at 14:58
  • Run 'Untitled' and it should run the others. I fixed the double opening filedialog, I moved the 'self.buttonGroup.buttonClicked['int'].connect(self.input)' down to the end of the function, but still not getting text –  Jul 14 '17 at 15:02
  • Oh, go to 'Output' tab then go to 'Comparison tab and input number in QlineEdit and click ok –  Jul 14 '17 at 15:09
  • Cool, thanks for the tip. I used the QIntValidator, as it seems to suit it better. Still trying to solve why it isn't getting the return values from searchfile function inside the input slot –  Jul 14 '17 at 15:45
  • I was responding to the comment about 'What is the second button? You should not ask to ask". I solved the problem with second button not opening FileDialog and deleted that comment. The issue with it not printing 'DiffP' is still there. –  Jul 14 '17 at 15:50
  • Logo doesn't really matter all that much, as it can be found anywhere –  Jul 14 '17 at 16:15
  • I'll try and figure it out myself. As the issue is with the code you provided for the input slot, it doesn't return values and that is all, you don't need a file to open to check it, just add values to the variables in the searchfile function instead of it searching a file. Appreciate the help anyway. –  Jul 14 '17 at 16:23
  • Thanks. It worked for me to. I'll keep it in mind to give whole files/imports next time. I've been in a rush and trying to get it done as fast as I can. –  Jul 14 '17 at 16:28