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

When i have more than one QLabel in QWidget - only one is showing



  • Hello
    When I add to QWidget (QGraphics scene on widget) more than one QLabel:

    ...
    scene = new QGraphicsScene(this);
    
    QGraphicsView * graphicsView = new QGraphicsView(scene);
    QBoxLayout * layout = new QVBoxLayout;
    ...
    
    for(int i=0; i<=10; ++i){
            QLabel * label = new QLabel(QString::number((10-i)%10));
            label->setStyleSheet(sheetstyle);
            label->setAlignment(Qt::AlignCenter);
            label->setFont(font);
            label->setGeometry(0, (i * labelHeight) ,labelWidth,labelHeight * 0.8);
            label->show();
            scene->addWidget(label);
        }
    
    ...
    layout->addWidget(graphicsView);
    setLayout(layout);
    

    Only one is visible . When durring animation (or when i use scroll bar) visible QLabel becomes invisible, another one QLabel
    appears.

    Widget have't got any set flags or atributes. Main Windows type is popup.

    Maybe anyone know what is wrong?


  • Lifetime Qt Champion

    @moominek said:
    Hi
    Are you 100% sure they dont overlap?

    is labelHeight some ok value?
    try with move()



  • Yes i'm sure.
    I wan't to make Widget which lookslike this:

    0
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    

    And it is creating (it is embded in QWidget with lowe height. I Want to have only one digit, and when state change I want move widget up or down to show other digit). When I animate my widget next digit is showing, after previous digit goes out view. But in my project digit should change smoothly.


  • Lifetime Qt Champion

    Hi
    Your code works for a normal widget.

    I wonder if addWidget(label); does use x,y of Geometry.
    I think it stacks them all once inserted.

    have you inserted 2 labels with very different pos and seen that it does
    indeed use x,y ?



  • I add :

    qDebug() << "x:" <<label->x() << "y:" << label->y();
    

    and the results:

    x: 0 y: 0
    x: 0 y: 270
    x: 0 y: 540
    x: 0 y: 810
    x: 0 y: 1080
    x: 0 y: 1350
    x: 0 y: 1620
    x: 0 y: 1890
    x: 0 y: 2160
    x: 0 y: 2430
    x: 0 y: 2700
    

    I think is everything ok with x and y.


  • Lifetime Qt Champion

    hi
    I tested in Diagramscene sample and your code does show
    as expected
    http://postimg.org/image/ddr5urlf5/



  • Ok. I have previous version of this program. And it's works. I don't know why :).

    Is only one diffrence. In my code i don't set parent when I add Widget to another Widget.

    May this influence the problem appears?


  • Lifetime Qt Champion

    well I would assume setWidget would set the parent else
    the Labels would be free floating.

    But in this sample here, you add directly to scene so if u in the real code

    • add to another widget then yes it might be.
      try to call setParent on each label and see if it works then.

Log in to reply