#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0

    PyQt: setText of all LineEdit's on form with one button click


    Here are my system specifications (for my machine @ home):
    Suse 9.1
    kernel-default-2.6.5-7.108
    python-2.3.3-85
    qt3-3.3.1-36.16
    PyQt-3.11-17
    sip-3.10.1-16

    Using PyQt, I'm trying to figure out how to do the following: With one button click, set the text of all LineEdits on a form. I can get the example code below to pass the string "hi" to the function set() in my class MyLineEdit, but it will not set the text to "hi" for all of the LineEdits on the form. What I'm I doing wrong? I'm not a programer (as you can tell), I just like learning.....

    Thanks

    Bill

    Here's the code:

    Code:
    #!/usr/bin/env python
    
    import os
    import sys
    
    from qt import *
    
    class MyLineEdit(QLineEdit): 
        
        def __init__(self, parent):
            QLineEdit.__init__(self, parent)
                
        def set(self, txt):
            print txt
            MyLineEdit(self).setText(txt)
            
    class MainWindow(QMainWindow):
    
        def __init__(self):
            QMainWindow.__init__(self)
                    
            self.grid=QGrid(2, self)
            self.grid.setFrameShape(QFrame.StyledPanel)
            self.setCentralWidget(self.grid)
    
            self.le1=MyLineEdit(self.grid)
            self.le2=MyLineEdit(self.grid)
            self.le3=MyLineEdit(self.grid)
            self.le4=MyLineEdit(self.grid)      
         
            self.bn1=QPushButton("Clear", self.grid)
            self.connect(self.bn1, SIGNAL("clicked()")
                         , self.setLineEditText)   
            
        def setLineEditText(self):
            MyLineEdit(self).set("hi")      
        
    def main(args):
        app=QApplication(args)
        win=MainWindow()
        win.show()
        app.connect(app, SIGNAL("lastWindowClosed()")
                    , app, SLOT("quit()"))
        app.exec_loop()
      
    if __name__=="__main__":
        main(sys.argv)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0
    I received an answer on the PyKDE mailing list. I posted virtually the same question. Maybe this will help someone else. Both solutions list below worked for me. -- Bill

    Here's the reply I received from Torsten:

    -------------------------------------------------------------

    Try something like this (solution 1):

    class MyLineEdit(QLineEdit):
    # keeps track of all instances of MyLineEdit,
    # but is buggy if instances are removed
    allinstances = []
    def __init__(self, parent):
    self.allinstances.append(self)
    QLineEdit.__init__(self, parent)
    self.setText("hello")

    def set(self, txt):
    self.setText(txt)

    def setAll(cls, x):
    for le in cls.allinstances:
    le.set(x)

    setAll=classmethod(setAll)

    and then

    def setLineEdit(self):
    MyLineEdit.setAll("hi")


    solution 2:

    def setLineEdit(self):
    for name in self.__dict__:
    w = getattr(self, name)
    if isinstance(w, MyLineEdit):
    w.set("hi")

    There are certainly some more solutions, maybe this helps you finding the best one.

    greetings

    Torsten

    -------------------------------------------------------------

IMN logo majestic logo threadwatch logo seochat tools logo