Results 1 to 4 of 4

Thread: Can you use a pointer to take the place of an object name?

  1. #1
    Join Date
    Jun 2016
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanks
    2

    Default Can you use a pointer to take the place of an object name?

    I need to change properties of a couple of hundred objects (mostly pushButtons) according to integer data. The code would look like this:

    ui->pbN_20->setText(display_value);
    ui->pbN_20->setStyleSheet("background-color: white");
    ui->pbN_21->setStyleSheet("background-color: palegreen");

    Having the integers 20 and 21, it takes an awful lot of switches and if else code to hook up with the code above.

    It would be great if you could do something like:

    int cellNumber = 20;
    OName = "pbN_" + QString::number(cellNumber);
    ui->OName->setText(display_value);

    but a string is not a valid object name. So, is there some way of constructing a pointer to an object name that would accomplish this?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: Can you use a pointer to take the place of an object name?

    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Can you use a pointer to take the place of an object name?

    Instead of using tons of variables (pbN_*) to store the pointers to the pushbuttons, you should put them all in a container such as a QVector.

  4. The following user says thank you to yeye_olive for this useful post:

    JustBobC (12th June 2016)

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

    Default Re: Can you use a pointer to take the place of an object name?

    Quote Originally Posted by yeye_olive View Post
    Instead of using tons of variables (pbN_*) to store the pointers to the pushbuttons, you should put them all in a container such as a QVector.
    Indeed, or a hash/map if not all index values are used.

    While jefftee's suggestion would also work, a suitable data structure is much cleaner than traversing the object tree every time such a change is required.

    Cheers,
    _

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

    JustBobC (12th June 2016)

Similar Threads

  1. Replies: 0
    Last Post: 1st March 2012, 22:35
  2. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  3. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41
  4. Saving object pointer
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 11th January 2008, 11:53
  5. Pointer to ActiveX object in QT
    By json84 in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2007, 12:42

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
  •  
Qt is a trademark of The Qt Company.