Trouble passing an argument
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.
Code:
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.
Code:
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
Code:
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..???
Re: Trouble passing an argument
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.
Re: Trouble passing an argument
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
Re: Trouble passing an argument
Quote:
Originally Posted by
benlyboy
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
Then create and emit your own signal.
Code:
class YourClass {
Q_OBJECT
signals:
private slots:
emitMySignal() {
emit mySignal(pointerToTextEdit->toText());
}
};
Just translate that to python and connect the button click to the private slot.
Re: Trouble passing an argument
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?
Code:
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
Code:
self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s="this is a string"] : self._edit_(s)))
def _edit_(self,inptname):
print inptname
Re: Trouble passing an argument
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.