Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLabel size, packing algorithm



  • Hi,

    I am developing a Text Mining application. Part of the functionality is to create visualization. I am developing a packing algorithm in separate class, this algorithm generates a QList of QLabls, and a QList of QPoints.
    How it works:
    I have x and y values initialized to 5 (in this case),
    I have string values stored in the model,
    I am taking strings one by one,
    Taken string is used to create a label, label is added to the list QLabels
    Values x and y are used to construct the QPoint, and next added to the list of QPoints

    New values x and y are calculated for the next label
    calculate by formula :

    @x= x + lbl->width()+2;
    if (x >= 1200) {
    x = 5;
    y += lbl->height() +2;
    }
    @

    Process continues ...

    Then pointers to the both lists are passed to the Form class and displayed on the form.

    Here is the whole code:
    @
    void packer::createColoredLabels(){

    model->sort(0,Qt::AscendingOrder);

    int x = 5;
    int y = 5;

    lablesList = new QList<QLabel*>;
    coords = new QList<QPoint>;
    for(int j=0; j< wCount ; j++){

      QString lblName = model->item(j,0)->text();
      QLabel *lbl = new QLabel(lblName);
      lbl->setAutoFillBackground(true);
      lbl->setFrameShape(QFrame::Panel);
      lbl->setFrameShadow(QFrame::Raised);
    
      lablesList->append(lbl);
      QPoint p = QPoint(x,y);
      coords->append(p);
      //lbl->show();
     
      x= x + lbl->width()+2;
      if (x >= 1200) {
          x = 5;
          y += lbl->height() +2;
      }
    

    }

    }
    @

    The problem I have is that when I execute the application I see that the coordinates are not calculated correctly, each label inherits properties from QWidget and assign its size. So each label size is fixed to 640x480.

    But if i use

    @lbl->show();@

    commented out in the code I am getting the desired results, that means each label size is actually the size it takes(not 640x480 as above), and everything works as intended. The only thing is I am seeing the small window popping up during the process of generating coordinates by the packing algorithm.

    So finally the question is:

    Is there any other way than

    @lbl->show();@

    To force the label to get the its actual size instead of inherited 640x480?

    Rgds
    Bart


Log in to reply