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

QLabel is snipping after using SetStyleSheet



  • I use QLabels in verticalLayout:
    QLabel* label;
    label->setWordWrap(true);
    label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    label->setMaximumWidth(250);
    label->setText(...);
    ...
    expected result: it's okey!
    ui->verticalLayout->addWidget(label, 0 , Qt::AlignBottom | Qt::AlignLeft)
    0_1527329215709_Without_style.PNG

    then I want to set the properties, for example:
    label->setStyleSheet("QLabel {background-color: rgb(255,255,255) }");
    result:
    0_1527329541787_snipping.PNG

    why snipping occurs and how to avoid it? Ty for help...


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    When using a style sheet, the concerned widgets are not rendered by the platform style plugin but by another one that supports style sheet, so the output might not be exactly what you are used to see from your OS.


  • Moderators

    @SHRED4RED
    is this really the only stylesheet declaration you set?!



  • @raven-worx
    Yes. only stylesheet:
    Okey, i all the same have the problem with snipping QLabel:

    code when I add QLabels:

    static int x = 1;
    
    label->setText(ui->lineEdit->text());
    ui->lineEdit->clear();
    label->setWordWrap(true);
    label->setStyleSheet("QLabel { background-color: rgb(0, 0, 0);"
                         "color: rgb(0, 200, 0);"
                         "font: 12pt \"Plantagenet Cherokee\";}"
                         );
    label->setTextFormat(Qt::RichText);
    
    QSizePolicy policy(QSizePolicy::Fixed , QSizePolicy::Fixed);
    label->setSizePolicy(policy);
    
    label->setAlignment(Qt::AlignLeft);
    label->setMaximumWidth(250);
    label->setMinimumHeight(label->sizeHint().height());
    
    if(x % 2 == 0)
    {
        ui->verticalLayout_2->addWidget(label,0, Qt::AlignLeft |  Qt::AlignBottom);
    
    }
    else
    {
        ui->verticalLayout_2->addWidget(label,0, Qt::AlignRight | Qt::AlignBottom);
    }
    
    ++x;
    

    what occurs when I resize MainWindow:
    0_1527496184897_1.PNG
    0_1527496201053_2.PNG

    scrollAreaWidgetCotents properties : SizeConstraint SetMinimumSize, sizePolicy: horizontal - Minimum, vertical - Maximum

    This problem occurs when I use Layout alignment ! If i don't use it QLabels doesn't snipped


  • Moderators

    @SHRED4RED

    QSizePolicy policy(QSizePolicy::Fixed , QSizePolicy::Fixed);
    label->setSizePolicy(policy);

    What happens when you remove this?



  • @raven-worx
    If use default QLabel sizePolicy then snipping doesn't occur. But I can't understand what happen ... :(


  • Lifetime Qt Champion

    Because the size policy you are using explicitly disallows the widget to expand or shrink as needed.



  • @SGaist
    Okey i continue testing, and next test are:

    1. just long text
      0_1527501112500_3.PNG
    2. after long text set empty string or shot string
      0_1527501169339_4.PNG

    it's occur if i use Fixed sizePolicy and if don't use it.
    Why?



  • do you see empty area between QLabels? There are the text which are snipped.
    If don't use RichText and use Fixed size policy , then all are good. But I nevertheless can't understand how are work this magic.


Log in to reply