Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Refer to an interface object name with a variable (string)

  1. #1
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Refer to an interface object name with a variable (string)

    Hello,

    I'm developing an app that has 100 line edits.
    I named them t001, t002, t003, ..., t100
    I want to convert the value of each text in the line edits to double (no problem there) and save it in a vector, called t[].
    I don't want to repeat the following code a hundred times over:

    Qt Code:
    1. double t[100];
    2. int count1 = 0;
    3. if(ui->t001->text() != "")
    4. {
    5. t[count1] = ui->t001->text().toDouble();
    6. count1++;
    7. }
    To copy to clipboard, switch view to plain text mode 

    So, is there anyway I can use the name of the object as a string variable, so I can use a loop? Like:

    Qt Code:
    1. double t[100];
    2. int count1 = 0;
    3. QString current_element;
    4.  
    5.  
    6. for(count1 = 0; count1 < 100; count1++)
    7. {
    8. current_element = "t" + count1+1;
    9.  
    10. if(ui->current_element->text() != "")
    11. t[count1] = ui->current_element->text().toDouble();
    12. }
    To copy to clipboard, switch view to plain text mode 

    edited: I know this code wouldn't work and needs adapting in at least 2 parts, but still, what I need answered is the main question in the thread.


    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Why not simply put the line edits in a list as well?
    Or even create them programatically in the first place into a list.

    Cheers,
    _

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

    guiismiti (28th January 2015)

  4. #3
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    I don't understand how putting the objects in a list would help me, I'm a newbie, that's why I posted it in this board.
    I tried googling it, the key words are too common.

    Anyway, this should make my problem very clear for those who did not understand:

    Qt Code:
    1. double t[100];
    2. int count1 = 0;
    3. QString objectname;
    4.  
    5. for(count1 = 0; count1 < 100; count1++)
    6. {
    7. if(count1 < 9)
    8. objectname = "t0";
    9. else
    10. objectname = "t";
    11.  
    12. objectname += QString::number(count1+1);
    13. t[count1] = ui->objectname->text().toDouble();
    14. }
    To copy to clipboard, switch view to plain text mode 

    And, when I try to build this, I get:
    'objectname': is not a member of 'Ui::MainWindow'

    since I'm trying to use ui->objectname instead of ui->t01 or ui->t02 or ui->t03, and all the way to t100, one by one.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by guiismiti View Post
    I don't understand how putting the objects in a list would help me,
    You would iterate over the list the same way you are iterating over 't' in your code snippet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    guiismiti (28th January 2015)

  7. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Read about QObject::findChild

  8. The following user says thank you to Lesiok for this useful post:

    guiismiti (28th January 2015)

  9. #6
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    thank you all, i'll look it up and test.

  10. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Refer to an interface object name with a variable (string)

    I don't understand how putting the objects in a list would help me
    Instead of creating your QLineEdit objects in Qt Designer, create them in code and insert them into a vector:

    Qt Code:
    1. // in the constructor for your widget
    2. // assumption is that there is a layout to handle the 100 line edits
    3. // also assume there is a QVector< QLineEdit * > * pEdits member variable
    4.  
    5. pEdits = new QVector< QLineEdit * >( 100, 0 );
    6. for ( int i = 0; i < 100; i++ )
    7. {
    8. QLineEdit * pEdit = new QLineEdit( this );
    9. pLayout->addWidget( pEdit );
    10. pEdits[ i ] = pEdit;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now if you want to retrieve the text from the "nth" line edit, you simply do this:

    Qt Code:
    1. t[ n ] = pEdits[n]->text().toDouble();
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to d_stranz for this useful post:

    guiismiti (28th January 2015)

  12. #8
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Creating the line edits by code would get too complicated for me, I'm using a few stacked widgets.

  13. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by guiismiti View Post
    Creating the line edits by code would get too complicated for me, I'm using a few stacked widgets.
    That's really not a problem at all. Even if you were to copy and paste the same code 100 times, it would still be beneficial. And you don't have to do that, you can create your widgets based on some array or whatever can hold the ui definition and is iterable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    guiismiti (28th January 2015)

  15. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by guiismiti View Post
    I don't understand how putting the objects in a list would help me
    A list is a sequential container, its element can be accessed by numerical index, like the array.
    If you have the elements in a list, you can access the result value array index and the source element with the same integer value.

    You can even use an array of pointers instead of the list, or a vector.

    If you can't create the elements in code, you still need to initialize this container once, while every other access can then be based on numerical index.

    Cheers,
    _

  16. The following user says thank you to anda_skoa for this useful post:

    guiismiti (28th January 2015)

  17. #11
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by anda_skoa View Post
    A list is a sequential container
    Thank you once again.

    Is there anything about lists in the Qt docs? I would like to explore syntax and read about it.

  18. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    See documentation for QList and QVector.

    Cheers,
    _

  19. The following user says thank you to anda_skoa for this useful post:

    guiismiti (28th January 2015)

  20. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Refer to an interface object name with a variable (string)

    Creating the line edits by code would get too complicated for me, I'm using a few stacked widgets.
    As wysota and others have said, if you don't want to create the widgets in code, you can still retrieve their pointers and store them in a list or vector:

    Qt Code:
    1. // In the constructor for each widget in the stack:
    2.  
    3. ui->setupUi( this );
    4.  
    5. pVector = new QVector< QLineEdit * >( 100 );
    6.  
    7. pVector[ 0 ] = ui->t000;
    8. pVector[ 1 ] = ui->t001;
    9. // etc. for the next 98 line edits
    10.  
    11. // Then, you still retrieve the text for each widget using the vector
    12.  
    13. t[n] = pVector[n]->text().toDouble();
    To copy to clipboard, switch view to plain text mode 

    Edit: You could avoid the cut-and-paste if you are consistent in your names for the line edits. You can then use findChild, as Lesiok suggests:

    Qt Code:
    1. pVector = new QVector< QLineEdit * >( 100 );
    2.  
    3. for ( int i = 0; i < 100; i++ )
    4. {
    5. QString name = QString( "t%1" ).arg( i, 3, '0' );
    6. pVector[ i ] = findChild< QLineEdit * >( name );
    7. }
    To copy to clipboard, switch view to plain text mode 

    This assumes the line edits are named "t000", "t001", ... "t099".
    Last edited by d_stranz; 28th January 2015 at 18:47.

  21. The following user says thank you to d_stranz for this useful post:

    guiismiti (28th January 2015)

  22. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refer to an interface object name with a variable (string)

    Just remember, despite having to write less lines of code to use findChild this is actually slower to execute than 100 copy&paste of the manual code that retrieves a single pointer. So all in all it is a matter of balance between the number of lines of code you have to write (once) and the amount of code that needs to be executed (every time you run your app).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  23. #15
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by wysota View Post
    Just remember, despite having to write less lines of code to use findChild this is actually slower to execute than 100 copy&paste of the manual code that retrieves a single pointer. So all in all it is a matter of balance between the number of lines of code you have to write (once) and the amount of code that needs to be executed (every time you run your app).
    Yea, I thought about that.

    I had the code written and ready just before I started the thread, but in my honest opinion I don't really like the idea of doing this kind of manual job when it can be avoided.

    I said I have 100 elements but I actually have 3 stacked widgets with 100 each - but that doesn't affect anything, so I decided to make it simple to present the problem. All elements are essential to the app - they are results of a laboratory experiment that must be informed as float (or double, I don't really know the difference).

    I haven't got much time right now (couldn't even open Qt today ), but I think that the list is the best option for me.
    But still, does anybody know what's wrong here? I'm trying to use findChild. I have tried to use parentWidget instead of centralWidget, but no difference.

    Qt Code:
    1. QLineEdit *lineedit = centralWidget->findChild<QLineEdit *>("t01");
    To copy to clipboard, switch view to plain text mode 

    The error message I get is this

    '->QObject::findChild' : left operand has " type, use '.'


    Edited: This may be helpful

    structure.png

  24. #16
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Refer to an interface object name with a variable (string)

    Perhaps you need ui->centralWidget->findChild< QLineEdit * >( "t01" ) ? The compiler is probably complaining about the dereferencing because it can't find a variable named "centralWidget" in the current scope.

    And you probably don't want to use centralWidget in any case - findChild will get only the first of the QLineEdit widgets it finds with a matching name in one of the stacked widgets, probably the first one in the stack. (Assuming that the line edits on each page have the same names - eg. "t01" is the first line edit on each page). You will want to retrieve each set of 100 from each stacked widget separately and store them in three separate vectors / lists.

  25. The following user says thank you to d_stranz for this useful post:

    guiismiti (29th January 2015)

  26. #17
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by d_stranz View Post
    Assuming that the line edits on each page have the same names - eg. "t01" is the first line edit on each page
    Not the case - it's tn for time, vn for volume and cn for concentration.

  27. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Refer to an interface object name with a variable (string)

    Not the case - it's tn for time, vn for volume and cn for concentration.
    Then you're lucky Even so, I would still retrieve the line edit pointers from their most immediate parent (eg. the individual page widgets inside the stack widget).

    It's an encapsulation thing. The main window (and indeed, the rest of your app) doesn't need to know that there are 100 line edits buried down on a page somewhere in a stack widget. What it needs to know is when the array of time values has been updated. What I would do in that case is to keep all the code that retrieves the line edit pointers and keeps track of when the user changes things encapsulated within the parent widget that contains the line edits. When the user changes a value, you update it in the vector or list (which is held by the parent widget containing the line edits), and then emit a custom signal ("timeValuesChanged( const QVector< double > & )" ) with the new array of time values. Likewise volume and concentration.

    This way, you can do whatever you want to change the widgets inside the stack, the only thing the main window will know is that it should expect a signal like that when the values change. You could even substitute an object that reads time, volume, and concentration values from a file (or sensor array), and as long as it emits the same signal, the app won't know the difference.

  28. The following user says thank you to d_stranz for this useful post:

    guiismiti (29th January 2015)

  29. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refer to an interface object name with a variable (string)

    Quote Originally Posted by guiismiti View Post
    I said I have 100 elements but I actually have 3 stacked widgets with 100 each - but that doesn't affect anything, so I decided to make it simple to present the problem. All elements are essential to the app - they are results of a laboratory experiment that must be informed as float (or double, I don't really know the difference).
    I would have a configuration file (e.g. xml) that would list all the parameters and at runtime I would read the file, create the widgets based on it and so on. Should be really easy. If at any time you decide to modify the widgets, it is enough to modify the configuration file and the program will follow.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  30. The following user says thank you to wysota for this useful post:

    guiismiti (31st January 2015)

  31. #20
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refer to an interface object name with a variable (string)

    Things seem to be working fine right now.

    Although, I've got this little problem going on with the interface - the first page of the stacked widget is always fully displayed, but all the others are not (like in the images). It doesn't matter which is the first page, all the other ones will be like in the second image, with a huge gray square on the right side.

    Here is the first page:
    01.png

    Here is the second page:
    02.png

    Please let me know if you need any more info.


    Again, thank you very much, I really appreciate it.

Similar Threads

  1. How define an interface for a Qt object
    By hubbobubbo in forum Qt Programming
    Replies: 7
    Last Post: 24th August 2013, 13:37
  2. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 21:09
  3. how to concatenate a variable with a string
    By doforumda in forum Newbie
    Replies: 1
    Last Post: 25th August 2010, 09:34
  4. Get value for variable name defined as string
    By webquinty in forum General Programming
    Replies: 2
    Last Post: 18th November 2009, 11:30
  5. (ActiveQt) Invoking interface from COM object
    By mridulgandhi in forum Qt Programming
    Replies: 4
    Last Post: 11th October 2009, 21:29

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.