Results 1 to 6 of 6

Thread: Trouble passing an argument

  1. #1
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default 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.

    Qt Code:
    1. self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s=[str(self.cboxa.toPlainText())] : self._edit_(s)))
    2.  
    3. def _edit_(self,inptname):
    4. print inptname
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. self.connect(self.pb1a,SIGNAL("clicked()"),self._edit_()))
    2.  
    3. def _edit_(self,inptname):
    4. print str(self.cboxa.toPlainText())
    To copy to clipboard, switch view to plain text mode 

    Just to make it even more confusing this works

    Qt Code:
    1. self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s="this is a string"] : self._edit_(s)))
    2.  
    3. def _edit_(self,inptname):
    4. print inptname
    To copy to clipboard, switch view to plain text mode 

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

    What am I missing..???
    Last edited by benlyboy; 9th May 2010 at 16:20.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  3. #3
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Trouble passing an argument

    Quote Originally Posted by benlyboy View Post
    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.
    Qt Code:
    1. class YourClass {
    2. Q_OBJECT
    3. signals:
    4. mySignal(QString);
    5. private slots:
    6. emitMySignal() {
    7. emit mySignal(pointerToTextEdit->toText());
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 
    Just translate that to python and connect the button click to the private slot.

  5. #5
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default 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?

    Qt Code:
    1. self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s=[str(self.cboxa.toPlainText())] : self._edit_(s)))
    2.  
    3. def _edit_(self,inptname):
    4. print inptname
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. self.connect(self.pb1a,SIGNAL("clicked()"),(lambda s="this is a string"] : self._edit_(s)))
    2.  
    3. def _edit_(self,inptname):
    4. print inptname
    To copy to clipboard, switch view to plain text mode 
    Last edited by benlyboy; 9th May 2010 at 21:18.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. QMap as function argument...
    By cydside in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 17:59
  2. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46
  3. Character from argument disappears
    By Pepe in forum Qt Programming
    Replies: 13
    Last Post: 19th June 2007, 23:48
  4. default argument compiler warning
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2007, 14:57
  5. Warning QString::arg() argument missing
    By quickNitin in forum Newbie
    Replies: 9
    Last Post: 16th November 2006, 19:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.