Results 1 to 4 of 4

Thread: Manipulating member name to be controlled by string

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Manipulating member name to be controlled by string

    I have a 10 by 10 grid of frames, i can change their backgrounds with
    Qt Code:
    1. ui->box_9_9->setStyleSheet("background-color: rgb(255,0,0)");
    To copy to clipboard, switch view to plain text mode 
    but that's a lot of code, id like to have a For Loop
    Qt Code:
    1. int count;
    2. QString boxnum;
    3. for (count = 0; count <= 10; count++)
    4. {
    5. boxnum += "box_";
    6. boxnum += QString::number(count);
    7. boxnum += "_";
    8. boxnum += QString::number(count);
    9. ui->boxnum->setStyleSheet("background-color: rgb(255,0,0)");
    10. }
    To copy to clipboard, switch view to plain text mode 
    but i get an error. What am i doing wrong?

  2. #2
    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: Manipulating member name to be controlled by string

    This is C++, a static-typed compilable language. An identifier has to be an identifier and not a string containing textual representation of an identifier. You can probably do this instead:
    Qt Code:
    1. QFrame *frm = findChild<QFrame*>(QString("box_%1_%1").arg(count));
    2. if(frm)
    3. frm->setStyleSheet(...);
    To copy to clipboard, switch view to plain text mode 
    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.


  3. #3
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Manipulating member name to be controlled by string

    that worked for a diagonal line, how do i get it to go row by row or column by column?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Manipulating member name to be controlled by string

    Wysota's solution does what your pseudo-code does. Think about what Wysota gave you and adapt.

    Two hints: more than one loop, and find a good introductory programming text.

Similar Threads

  1. Manipulating excel files in Qt?
    By icttrack in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2010, 12:44
  2. Exception not controlled
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2009, 17:43
  3. Button-Controlled Loop
    By freekill in forum Qt Programming
    Replies: 5
    Last Post: 4th June 2009, 10:09
  4. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 21:11
  5. Replies: 1
    Last Post: 7th July 2007, 09:41

Tags for this Thread

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.