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. Dynamically Adding QLabels At Runtime

Dynamically Adding QLabels At Runtime

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.9k Views 1 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on last edited by
    #1

    I have an application that I wont know in advance the number of labels needed and they need to be created and added to the form at runtime.

    I have this code but nothing happens. The lables are never added to the form

    @
    dataGroupBox = new QGroupBox;
    //gridGroupBox->setTitle(tr("Buttons"));
    datalayout = new QGridLayout;

    QLabel* field[5];
    
    for(int j = 0; j < 4; j++)
    {
        field[j] = new QLabel("test " + j);
        datalayout->addWidget(field[j],j,1);
    }
    
    dataGroupBox->setLayout(datalayout);@
    

    header looks like this
    @ QGridLayout *datalayout;
    QGroupBox *dataGroupBox;@

    Can somwone show me how to dynamically create labels?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      EverydayDiesel
      wrote on last edited by
      #2

      Your a genius. Thank you for your help!

      Do you know how I can move each label with my mouse? I have been trying to get this to work but so far have been unsuccessful.

      I searched and found this code but it is more related to moving the form itself and not a dynamic label

      Add to window .h file:
      @
      private:
      void mousePressEvent(QMouseEvent *event);
      void mouseMoveEvent(QMouseEvent *event);
      int m_nMouseClick_X_Coordinate;
      int m_nMouseClick_Y_Coordinate;
      @

      And this to .cpp file:

      @
      void MainWindow::mousePressEvent(QMouseEvent *event) {
      m_nMouseClick_X_Coordinate = event->x();
      m_nMouseClick_Y_Coordinate = event->y();
      }

      void MainWindow::mouseMoveEvent(QMouseEvent *event) {
          move(event->globalX()-m_nMouseClick_X_Coordinate,event->globalY()-m_nMouseClick_Y_Coordinate);
      }
      

      @

      I also found this code but I am not sure how to attach it to the dynamic labels mouse down/move event

      @
      void DropLabel::mousePressEvent(QMouseEvent *event)
      {
      offset = event->pos();
      }

      void DropLabel::mouseMoveEvent(QMouseEvent *event)
      {
      if(event->buttons() & Qt::LeftButton)
      {
      this->move(mapToParent(event->pos() - offset));
      }
      }
      @

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

        This code uses a subclass of QLabel. You can't just "paste" it anywhere in your code. It's more complicated than that.

        This is entirely different topic. Please start a new thread with this question.

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