Results 1 to 3 of 3

Thread: How to programatically form QLineEdit() object names?

  1. #1
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default How to programatically form QLineEdit() object names?

    I am writting a GUI program using Python 2.7 and PyQt on Windows. My program reads parameter values from xml files and displays them allowing the user to change them on a GUI screen. The parameter names and the file where they are stored at are in a configuration xml file that I created by hand.

    The program displays roughly 50 values on the screen. To display the initial values of the parameters once they are read in from the data files, I need 50 lines of code to display the values:

    ui.txtBox1.setText(value1)
    ui.txtBox2.setText(value2)
    ..........
    ui.txtBox50.setText(value50)

    I have a class for the form, winForm generated by QT designer. In the main Python code file, ui is the object created from the winForm class (ui = winForm()).

    Is there a way to programatically form these text box commands to set the text so I don't need 50 lines of code?

    In the configuration xml file that I created that is used by the program, I store the text box name for the variable. For example:

    <boxName>ui.txtBox1</boxName>

    I created an object, VarObject from a custom class that stores the boxName and the initial value of the parameter.

    I then use the box name like this to set the text box value:
    boxName.setText(value)

    and get the error: boxName doesn't have a setText() attribute or member

    I have also tried to store the text box names in a list and dictionary:

    list = [ui.txtBox1, ui.txtBox2 ........]
    dict = { "key1":uitxtBox1, "key2":uitxtBox2, ......}

    Then I use list[0].setText(value)

    That doesn't work either. I prefer to store the text box names in my configuration xml file so I don't have to modify the Python code.

    Thank you for your help.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to programatically form QLineEdit() object names?

    The reason that your code is not working is that you need the pointers to the objects in the form. Try something like this (in C++ -don't know Python):
    Qt Code:
    1. QHash<QString, QLineEdit *> hash;
    2. QList<QLineEdit *>lineEditNames = findChildren<QLineEdit *>();
    3. foreach(QLineEdit *lineEditPtr, lineEditNames)
    4. hash[lineEditPtr->objectName()]=lineEditPtr;
    5.  
    6. hash.value(txtBox1)->setText(value); // put this in the loop where you read from your xml file
    To copy to clipboard, switch view to plain text mode 
    QMap can also be used, but the docs say that QHash performs faster lookups.

    Note: you will need to remove the "ui." from your object names.
    HTH

  3. #3
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: How to programatically form QLineEdit() object names?

    Thank you for the suggestion. I have several years of C++ experience and should be able to modify your code to work in Python. I feel the Python code will be a little shorter.

Similar Threads

  1. Replies: 1
    Last Post: 30th July 2010, 23:05
  2. Object Names
    By slava in forum Qt Programming
    Replies: 4
    Last Post: 17th July 2010, 11:24
  3. Form QLineEdit
    By waynew in forum Newbie
    Replies: 4
    Last Post: 5th November 2009, 21:16
  4. How to know the object names of any generic application ?
    By soumyadeep_pan in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2009, 07:11
  5. object files' names collision
    By PowerKiKi in forum Qt Programming
    Replies: 7
    Last Post: 30th October 2007, 21:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.