Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. variable as object

variable as object

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 382 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rktech
    wrote on last edited by
    #1
      for (int i =1 ;i<5;i++) {
                 QString s = QString::number(i);
                 ui->("modra"+s)->setAutoDefault(false);
                 ui->("modra"+s)->setIcon(QPixmap::fromImage(image));
                 ui->("modra"+s)->setIconSize(image.rect().size());
                 ui->("modra"+s)->resize(image.size());
           }
    

    I have buttons modra1-modra4. Image image is defined. Is that any way to use i from for cycle to change corresponding button properities?
    Thanks
    rktech

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can search for objects by the object name: QObject::findChild()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • R Offline
        R Offline
        rktech
        wrote on last edited by
        #3

        I think, that this is not what I asked. I need a way to create ui->modra1->... if i == 1 and ui->modra2->... if i == 2 ...

        jsulmJ 1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What @Christian-Ehrlicher said is right. Your example code using it would look like this:

          for (int i =1 ;i<5;i++) {
                       QString name = "modra" + QString::number(i);
                       QPushButton* button = findChild<QPushButton*>(name);
                       button->setAutoDefault(false);
                       button->setIcon(QPixmap::fromImage(image));
                       button->setIconSize(image.rect().size());
                       button->resize(image.size());
                 }
          
          1 Reply Last reply
          4
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by Kent-Dorfman
            #5

            huh!? what's wrong with arrays of objects instead of discrete names for each indexed button?

            Vector<QPushbutton*> buttonVector(5, nullptr);  // 5 element container
            for (auto&  i: buttonVector) { // c++17; access to each element
                i = new QPushButton;
            }
            // at this point you have a vector of QPushbutton* that are 
            // each initialized with a QPushbutton
            
            // direct access by index
            // arguably more efficient than searching by char-array name
            buttonVector[3]->setIcon(image.rect().size());
            buttonVector[1]->resize(image.size);
            
            1 Reply Last reply
            3
            • R rktech

              I think, that this is not what I asked. I need a way to create ui->modra1->... if i == 1 and ui->modra2->... if i == 2 ...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @rktech said in variable as object:

              I need a way to create ui->modra1->... if i == 1 and ui->modra2->... if i == 2

              This is not possible in C/C++ - variable names are compile time.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved