Results 1 to 4 of 4

Thread: Access variable using its name as QString

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Question Access variable using its name as QString

    Hello!

    As I wrote yesterday in another post, I have to read QStrings from many QLineEdits.
    The answer was that this is best accomplished through a class using a QList of QLineEdits. But I have to set the names of all lineedits somehow.

    Is it possible to access the variables through their QString'ed name?

    Is something like that possible:

    Qt Code:
    1. QLineEdit lineedit_1
    2. QLineEdit lineedit_2
    3. QLineEdit lineedit_3
    4. ...
    5.  
    6. QString lineedit1, lineedit2, lineedit3
    7.  
    8. QStringList lineedits
    9. lineedits << "lineedit1" << "lineedit2" << "lineedit3".....
    10.  
    11. while (lineedits.hasNext)
    12. {
    13. PseudoCode: lineedits[INDEX] = SameNamedQLineEdit_[INDEX].text();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Well somehow my feeling points to "not possible, find another solution"
    Kind regards,
    HomeR

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

    Default Re: Access variable using its name as QString

    I would instead have an array of QLineEdit's and reference them through that array. If you use QMap to store them, you can use the key as the name and the value as the pointer to the QLineEdit, so then you can reference them via a string rather than a numerical index (as QMap overrides operator [])

  3. The following user says thank you to squidge for this useful post:

    homerun4711 (22nd December 2010)

  4. #3
    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: Access variable using its name as QString

    It's a dangerous reply, so first:

    Don't use my suggestion! Try what squidge said.

    But theoretical you could use QObject::findChild().

  5. The following user says thank you to Lykurg for this useful post:

    homerun4711 (22nd December 2010)

  6. #4
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Access variable using its name as QString

    Thank you for your suggestions. They sound very good!
    Kind regards,
    HomeR

Similar Threads

  1. Problem with a dynamicCall and a Qstring variable
    By jokinb in forum Qt Programming
    Replies: 5
    Last Post: 25th October 2010, 09:11
  2. Replies: 0
    Last Post: 20th July 2010, 19:31
  3. QtScript access variable from C++
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2009, 00:51
  4. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 22:12
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 21:48

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.