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.5k 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.
  • 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
                          • jsulmJ jsulm

                            @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.

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

                            @jsulm said in How to make variable objects?:

                            @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.

                            Sorry I didn't go to this forum recently. I set parent label and everything works just fine.

                            I am using a customized label to store more information. That's why I created them in functions.

                            Now I am creating context menus for these labels. It looks like this. hands_s[i] are pointers for those 10 labels.

                                connect(hands_s[i], &Gameplay::customContextMenuRequested, this, &Gameplay::HandContextMenu);
                            

                            My problem is, how do I know which label I am right-clicking? Do I have to connect them with 10 different functions?

                            mrjjM 1 Reply Last reply
                            0
                            • M MasterBlade

                              @jsulm said in How to make variable objects?:

                              @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.

                              Sorry I didn't go to this forum recently. I set parent label and everything works just fine.

                              I am using a customized label to store more information. That's why I created them in functions.

                              Now I am creating context menus for these labels. It looks like this. hands_s[i] are pointers for those 10 labels.

                                  connect(hands_s[i], &Gameplay::customContextMenuRequested, this, &Gameplay::HandContextMenu);
                              

                              My problem is, how do I know which label I am right-clicking? Do I have to connect them with 10 different functions?

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

                              @MasterBlade
                              You can use sender() to get the QLabel;

                               QLabel* lab= qobject_cast<QLabel*>(sender());
                                 if( lab ) 
                                 { 
                                    ...
                                 }
                              
                              M 1 Reply Last reply
                              3
                              • mrjjM mrjj

                                @MasterBlade
                                You can use sender() to get the QLabel;

                                 QLabel* lab= qobject_cast<QLabel*>(sender());
                                   if( lab ) 
                                   { 
                                      ...
                                   }
                                
                                M Offline
                                M Offline
                                MasterBlade
                                wrote on last edited by
                                #24

                                @mrjj said in How to make variable objects?:

                                @MasterBlade
                                You can use sender() to get the QLabel;

                                 QLabel* lab= qobject_cast<QLabel*>(sender());
                                   if( lab ) 
                                   { 
                                      ...
                                   }
                                

                                That does work! Many thanks!!

                                1 Reply Last reply
                                0

                                • Login

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