Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make variable objects?
Qt 6.12 LTS is out! See what's new in the release blog

How to make variable objects?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 11.8k Views 2 Watching
  • 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.
  • Gojir4G Offline
    Gojir4G Offline
    Gojir4
    wrote on last edited by Gojir4
    #2

    Hello,

    A possible solution is to store each QLabel in a QMap.

    // .h
    QMap<int, QLabel*> m_labels;
    
    //---------------------------
    // .cpp
    //Init in constructor, after ui->setupUi(this)
    m_labels[1] = ui->hand1;
    m_labels[2] = ui->hand2;
    ...
    m_labels[10] = ui->hand10;
    
    //---------------------------
    //And then, access it
    int i = 5;
    m_labels[i]->setText("Hello");
    
    jsulmJ 1 Reply Last reply
    0
    • Gojir4G Gojir4

      Hello,

      A possible solution is to store each QLabel in a QMap.

      // .h
      QMap<int, QLabel*> m_labels;
      
      //---------------------------
      // .cpp
      //Init in constructor, after ui->setupUi(this)
      m_labels[1] = ui->hand1;
      m_labels[2] = ui->hand2;
      ...
      m_labels[10] = ui->hand10;
      
      //---------------------------
      //And then, access it
      int i = 5;
      m_labels[i]->setText("Hello");
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.

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

      M 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #4

        @jsulm said in How to make variable objects?:

        @Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.

        I have exactly 10 objects. How can I link those objects with QVector or std::array ?

        jsulmJ 1 Reply Last reply
        0
        • M MasterBlade

          @jsulm said in How to make variable objects?:

          @Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.

          I have exactly 10 objects. How can I link those objects with QVector or std::array ?

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

          @MasterBlade Like shown in documentation: http://doc.qt.io/qt-5/qvector.html

          QVector<QLabel*> labels;
          labels[0] = new QLabel();
          ...
          labels[0]->setText("...");
          

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

          M 1 Reply Last reply
          4
          • M MasterBlade

            Hello, I have 10 similar QLabels . Their object names are from hand1 to hand10.

            I want to choose an object from them using a variable, like

            int i=5;
            ui->hand[i]->settext("");
            

            How can I do this?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #6

            @MasterBlade said in How to make variable objects?:

            Their object names are from hand1 to hand10

            if by "object name" you mean QObject::objectName then you can use findChild<QLabel*>(QStringLiteral("hand%1").arg(i));

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply
            4
            • VRoninV VRonin

              @MasterBlade said in How to make variable objects?:

              Their object names are from hand1 to hand10

              if by "object name" you mean QObject::objectName then you can use findChild<QLabel*>(QStringLiteral("hand%1").arg(i));

              M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #7

              @VRonin said in How to make variable objects?:

              @MasterBlade said in How to make variable objects?:

              Their object names are from hand1 to hand10

              if by "object name" you mean QObject::objectName then you can use findChild<QLabel*>(QStringLiteral("hand%1").arg(i));

              Actually I mean like Labels or Pushbuttoms. Do they work the same way?

              J.HilkJ 1 Reply Last reply
              0
              • M MasterBlade

                @VRonin said in How to make variable objects?:

                @MasterBlade said in How to make variable objects?:

                Their object names are from hand1 to hand10

                if by "object name" you mean QObject::objectName then you can use findChild<QLabel*>(QStringLiteral("hand%1").arg(i));

                Actually I mean like Labels or Pushbuttoms. Do they work the same way?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #8

                @MasterBlade said in How to make variable objects?:

                @VRonin said in How to make variable objects?:

                @MasterBlade said in How to make variable objects?:

                Their object names are from hand1 to hand10

                if by "object name" you mean QObject::objectName then you can use findChild<QLabel*>(QStringLiteral("hand%1").arg(i));

                Actually I mean like Labels or Pushbuttoms. Do they work the same way?

                If you create them by hand, inside your cpp/h file than you have to give them one explicitly via setObjectName, if you use the designer than all objects created inside of it get an unique objectName by default.

                But keep in mind findChild can quickly become a time extensive function expecially if you call it on the main parent, as it goes through all the children!


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi
                  Just as note.
                  Yes you can find any type of widget.
                  findChild<QLabel*> <<< just change type there.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @MasterBlade Like shown in documentation: http://doc.qt.io/qt-5/qvector.html

                    QVector<QLabel*> labels;
                    labels[0] = new QLabel();
                    ...
                    labels[0]->setText("...");
                    
                    M Offline
                    M Offline
                    MasterBlade
                    wrote on last edited by
                    #10

                    @jsulm said in How to make variable objects?:

                    @MasterBlade Like shown in documentation: http://doc.qt.io/qt-5/qvector.html

                    QVector<QLabel*> labels;
                    labels[0] = new QLabel();
                    ...
                    labels[0]->setText("...");
                    

                    Hello, I tried the following code. There is no error while running. But those labels don't appear in the UI.

                    //hand is a public member of p1
                    //defined as QVector<QLabel*> hand;
                    p1.hand.resize(10);
                    for (uint8_t i = 0; i < 10; i++){
                        p1.hand[i] = new QLabel;
                        p1.hand[i]->setText("Hand" + QString::number(i + 1));
                        p1.hand[i]->setObjectName("Hand" + QString::number(i + 1));
                        p1.hand[i]->resize(120, 170);
                        p1.hand[i]->move(120 * i + 22, 522);
                    }
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi
                      But you dont give it a parent
                      or assign it to a layout.
                      so they wont show inside the existing mainwindow.

                      What do you want to insert them in ?

                      you can try with
                      p1.hand[i] = new QLabel(this);

                      if code inside the widget that should have them like mainwindow.

                      M 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        But you dont give it a parent
                        or assign it to a layout.
                        so they wont show inside the existing mainwindow.

                        What do you want to insert them in ?

                        you can try with
                        p1.hand[i] = new QLabel(this);

                        if code inside the widget that should have them like mainwindow.

                        M Offline
                        M Offline
                        MasterBlade
                        wrote on last edited by MasterBlade
                        #12

                        @mrjj said in How to make variable objects?:

                        Hi
                        But you dont give it a parent
                        or assign it to a layout.
                        so they wont show inside the existing mainwindow.

                        What do you want to insert them in ?

                        you can try with
                        p1.hand[i] = new QLabel(this);

                        if code inside the widget that should have them like mainwindow.

                        Hello, I want to insert them to the current window, gameplay.ui . These codes come from gameplay.cpp . I tried to merge those commands like this. Still those labels aren't displayed in the current window. What was wrong?

                        bool Gameplay::Main_Game()
                        {

                        MPlayer p1, p2;
                        p1.hand.resize(10);
                        for (uint8_t i = 0; i < 10; i++){
                            p1.hand[i] = new QLabel("Hand" + QString::number(i + 1), this);
                            p1.hand[i]->setObjectName("Hand" + QString::number(i + 1));
                            p1.hand[i]->resize(120, 170);
                            p1.hand[i]->move(120 * i + 22, 522);
                        }
                        

                        //...
                        }

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Hi
                          MPlayer is a local variable and will not live after Main_Game ends.
                          so you might need to make it a member of Gameplay

                          M 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            Hi
                            MPlayer is a local variable and will not live after Main_Game ends.
                            so you might need to make it a member of Gameplay

                            M Offline
                            M Offline
                            MasterBlade
                            wrote on last edited by
                            #14

                            @mrjj said in How to make variable objects?:

                            Hi
                            MPlayer is a local variable and will not live after Main_Game ends.
                            so you might need to make it a member of Gameplay

                            I tried to define it as a member of Gameplay. But got an error message. What does that mean?

                            class Gameplay : public QDialog
                            {
                            Q_OBJECT

                            public:
                            MPlayer p1, p2;
                            //...
                            }

                            C:\Users\mfdsr\Documents\MasterZMDJ\gameplay.h:36: error: field 'p1' has incomplete type 'MPlayer'
                            MPlayer p1, p2;

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              MasterBlade
                              wrote on last edited by
                              #15

                              Nevermind, I managed to make p1 and p2 global variables.

                              But still those QLabels don't appear...

                              mrjjM 1 Reply Last reply
                              0
                              • M MasterBlade

                                Nevermind, I managed to make p1 and p2 global variables.

                                But still those QLabels don't appear...

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @MasterBlade
                                Dont use global variables. its not good with Qt.

                                Anyway, the "incomplete type 'MPlayer'" means you forgot to include
                                MPlayer.h and hence it didnt know it.

                                Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
                                Or is it a window?

                                M 1 Reply Last reply
                                2
                                • mrjjM mrjj

                                  @MasterBlade
                                  Dont use global variables. its not good with Qt.

                                  Anyway, the "incomplete type 'MPlayer'" means you forgot to include
                                  MPlayer.h and hence it didnt know it.

                                  Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
                                  Or is it a window?

                                  M Offline
                                  M Offline
                                  MasterBlade
                                  wrote on last edited by
                                  #17

                                  @mrjj said in How to make variable objects?:

                                  @MasterBlade
                                  Dont use global variables. its not good with Qt.

                                  Anyway, the "incomplete type 'MPlayer'" means you forgot to include
                                  MPlayer.h and hence it didnt know it.

                                  Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
                                  Or is it a window?

                                  Actually MPlayer is just a class. I declared it in gameplay.h . It stored info for players.

                                  mrjjM jsulmJ 2 Replies Last reply
                                  0
                                  • M MasterBlade

                                    @mrjj said in How to make variable objects?:

                                    @MasterBlade
                                    Dont use global variables. its not good with Qt.

                                    Anyway, the "incomplete type 'MPlayer'" means you forgot to include
                                    MPlayer.h and hence it didnt know it.

                                    Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
                                    Or is it a window?

                                    Actually MPlayer is just a class. I declared it in gameplay.h . It stored info for players.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #18

                                    @MasterBlade
                                    So what do u see in dialog ?
                                    None of the labels are shown ?
                                    I cant guess what goes wrong.

                                    Did you try to insert just 1 label and see ?

                                    1 Reply Last reply
                                    0
                                    • M MasterBlade

                                      @mrjj said in How to make variable objects?:

                                      @MasterBlade
                                      Dont use global variables. its not good with Qt.

                                      Anyway, the "incomplete type 'MPlayer'" means you forgot to include
                                      MPlayer.h and hence it didnt know it.

                                      Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
                                      Or is it a window?

                                      Actually MPlayer is just a class. I declared it in gameplay.h . It stored info for players.

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

                                      @MasterBlade You need to call show() on your labels if you want to see them...

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

                                      M 1 Reply Last reply
                                      3
                                      • jsulmJ jsulm

                                        @MasterBlade You need to call show() on your labels if you want to see them...

                                        M Offline
                                        M Offline
                                        MasterBlade
                                        wrote on last edited by
                                        #20

                                        @jsulm said in How to make variable objects?:

                                        @MasterBlade You need to call show() on your labels if you want to see them...

                                        I have an embarrassing problem. Those Labels are shown outside of the current window. And their positions are incorrect. I moved them to the relative position of current window. But somehow they appeared outside.

                                        0_1523952747774_1.PNG

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • M MasterBlade

                                          @jsulm said in How to make variable objects?:

                                          @MasterBlade You need to call show() on your labels if you want to see them...

                                          I have an embarrassing problem. Those Labels are shown outside of the current window. And their positions are incorrect. I moved them to the relative position of current window. But somehow they appeared outside.

                                          0_1523952747774_1.PNG

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

                                          @MasterBlade Why don't you simply use layouts? And I guess you did not set parent on this labels, that's why they are outside of the mainwindow.

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

                                          M 1 Reply Last reply
                                          2

                                          • Login

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