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. Vector of QLabels

Vector of QLabels

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #1

    Hello everybody,

    I am trying to create a QVector of QLabels to add dinamically labels such as:

    QVector <QLabel*> labels;

    but it does not run because theoretically it says use of undeclared identifier QLabel.

    so, if I try to append a QLabel such as labels.append(new QLabel);

    It does not work, why?

    thanks!

    1 Reply Last reply
    0
    • GerhardG Offline
      GerhardG Offline
      Gerhard
      wrote on last edited by
      #2

      #include <QLabel>

      Gerhard

      1 Reply Last reply
      3
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        I did it, but It does not work...

        mrjjM JonBJ 2 Replies Last reply
        0
        • J jss193

          I did it, but It does not work...

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

          @jss193
          Hi
          it must work or the #include is not in the same file where you have
          QVector <QLabel*> labels;
          or where you have
          labels.append(new QLabel);
          Also,
          make sure you have
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          or
          QT += widgets
          in your .pro file.

          1 Reply Last reply
          3
          • J jss193

            I did it, but It does not work...

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @jss193
            What "does not work"?

            "use of undeclared identifier QLabel" sounds like a compile-time (or just possibly link-time) error, so I don't know what you have in mind by "it does not run"....

            1 Reply Last reply
            3
            • J Offline
              J Offline
              jss193
              wrote on last edited by jss193
              #6

              @mrjj Now it works, and it is creating QLabels dynamically, in my h file I have:

              
              private:
                  Ui::MainWindow *ui;
                  QAbstractItemModel *model;
                  int columns = 7;
                  int rows = 0;
                  QVector<QLabel*> labels;
                  int countLabels = 0;
              private slots:
                  void changeText();
              };
              

              And in .cpp:

              void MainWindow::on_pushButton_clicked()
              {
                  model->insertRows(0,1);
              
                  QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed);
                  ui->horizontalLayout_2->addSpacerItem(item);
              
                  for(int i =0;i<1;i++){
                      labels.append(new QLabel);
              
                      ui->scrollArea->setWidgetResizable(true);
                    
                      ui->horizontalLayout_2->insertWidget(i,labels[countLabels]);
                      countLabels =countLabels +1;
                  }
              
              }
              

              But it does not add labels to my layout, before I had the following code and it added Qlabels when pushing the button:

              void MainWindow::on_pushButton_clicked()
              {
                  model->insertRows(0,1);
              
                  QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed);
                  ui->horizontalLayout_2->addSpacerItem(item);
              
                  for(int i =0;i<1;i++){
              
                      QLabel *label1 = new QLabel("aaaa");
              
                      ui->scrollArea->setWidgetResizable(true);
                      ui->horizontalLayout_2->insertWidget(i,label1);
                      
                  }
              
              }
              

              Why now it is not creating the QLabels??

              JonBJ 1 Reply Last reply
              0
              • J Offline
                J Offline
                jss193
                wrote on last edited by
                #7

                Fixed, it was creating empty QLabels I just added the text:

                labels.append(new QLabel("TEST"));

                1 Reply Last reply
                0
                • J jss193

                  @mrjj Now it works, and it is creating QLabels dynamically, in my h file I have:

                  
                  private:
                      Ui::MainWindow *ui;
                      QAbstractItemModel *model;
                      int columns = 7;
                      int rows = 0;
                      QVector<QLabel*> labels;
                      int countLabels = 0;
                  private slots:
                      void changeText();
                  };
                  

                  And in .cpp:

                  void MainWindow::on_pushButton_clicked()
                  {
                      model->insertRows(0,1);
                  
                      QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed);
                      ui->horizontalLayout_2->addSpacerItem(item);
                  
                      for(int i =0;i<1;i++){
                          labels.append(new QLabel);
                  
                          ui->scrollArea->setWidgetResizable(true);
                        
                          ui->horizontalLayout_2->insertWidget(i,labels[countLabels]);
                          countLabels =countLabels +1;
                      }
                  
                  }
                  

                  But it does not add labels to my layout, before I had the following code and it added Qlabels when pushing the button:

                  void MainWindow::on_pushButton_clicked()
                  {
                      model->insertRows(0,1);
                  
                      QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed);
                      ui->horizontalLayout_2->addSpacerItem(item);
                  
                      for(int i =0;i<1;i++){
                  
                          QLabel *label1 = new QLabel("aaaa");
                  
                          ui->scrollArea->setWidgetResizable(true);
                          ui->horizontalLayout_2->insertWidget(i,label1);
                          
                      }
                  
                  }
                  

                  Why now it is not creating the QLabels??

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @jss193

                  I did it, but It does not work...

                  Now it works,

                  Amazing how these things just happen!

                  Pl45m4P 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @jss193

                    I did it, but It does not work...

                    Now it works,

                    Amazing how these things just happen!

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @JonB

                    @JonB said in Vector of QLabels:

                    Amazing how these things just happen!

                    Reminds me of:

                    • "My code doesnt work, and I dont know why" -> "My code works, and I dont know why"

                    :)


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    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 Extensions
                    • Unsolved