PDA

View Full Version : Trouble passing an argument



benlyboy
9th May 2010, 16:07
Hi I have been working on this all day and it has me beat.
I want to pass the contents of a Qtextedit as a argument in a connect statement.

I tryed this.


self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s=[str(self.cboxa.toPlainText())] : self._edit_(s)))

def _edit_(self,inptname):
print inptname


But all I seem to get with this is a empty string, but if i do this it works.


self.connect(self.pb1a,SIGNAL("clicked()"),self._edit_()))

def _edit_(self,inptname):
print str(self.cboxa.toPlainText())


Just to make it even more confusing this works


self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s="this is a string"] : self._edit_(s)))

def _edit_(self,inptname):
print inptname


So way can't i pass the text from the Qtextedit.

What am I missing..???

Lykurg
9th May 2010, 16:28
you can't pass values inside a connect statement. The way to go is: connect the clicked() signal with a custom slot. In there emit a costumiced signal (which has QString as an argument), this you can connect to any function you want.

For a better answer, you might want to describe a litte more how your application looks like (where is your text edit and where is the function you want call...). It seems you can use a simple pointer to the text edit in your edit slot. So you don't have to pass the text at all. just use self.cboxa.toPlainText() inside your slot.

benlyboy
9th May 2010, 16:41
Thanks for the reply

The function the textedit are part of the same class, at this point at least. But I wanted to write the program in such a way that later I could move it out side the class as this function will be used by other classes

I should point out that i am writing in pyqt

Lykurg
9th May 2010, 18:21
But I wanted to write the program in such a way that later I could move it out side the class as this function will be used by other classesThen create and emit your own signal.
class YourClass {
Q_OBJECT
signals:
mySignal(QString);
private slots:
emitMySignal() {
emit mySignal(pointerToTextEdit->toText());
}
}; Just translate that to python and connect the button click to the private slot.

benlyboy
9th May 2010, 19:30
Thanks I will try that.

I am still interested in why this wouldn't work...after all shouldn't "str(self.cboxa.toPlainText())" just return a string?



self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s=[str(self.cboxa.toPlainText())] : self._edit_(s)))

def _edit_(self,inptname):
print inptname


and thats all this is a string isn't it? and yet this works



self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s="this is a string"] : self._edit_(s)))

def _edit_(self,inptname):
print inptname

squidge
9th May 2010, 22:49
The signal parameters must match the slot being called upon reception of that signal. So if you use the signal "clicked()" then your slot can not have a parameter, as there will be none passed to it. You can't pass a parameter in the connect statement. Therefore the best solution would be to call a method that doesn't expect a parameters, and then emit another signal that does emit a signal with appropriate parameter.

That said, I'm no PyQt expert, so I'm not sure about the qwerks and workings of that language, but what I say above is normal for Qt.