Congratulations to our 2022 Qt Champions!

[SOLVED] Hi, again :D Problem with Layouts...

  • I got to the problem where external style sheets (set with qApp->setStyleSheet("...")) can't apply linear gradient to background image of QLabel control. I am going for workaround to my custom control (base QFrame - Grid Layout;made in designer) contains another QFrame (Grid) which now contains QLabel. Extra QFrame is used to set border radius and gradient while QLabel will have transparent background. With this solution i overcome the problem of directly setting background gradient on QLabel.

    Now the problem is (sry i am new to Qt) how to setup sizePolicy of QFrames (outer & inner) and QLabel's to allow the label to expand the layouts (or maybe some other solution). I want to make a control that will contain one line of centered text on gradient background with border and some padding.

    I could go with QPushButton but i thought that the label has smaller memory/cpu requirements and i could have a lot of these elements around ( ~ 40 ).

    Also is there a way to set custom pseudo styles on controls? I mean to have MyCustomControl style and MyCustomControl:highlighted style and to switch between them by lets say some "obj->setPseudoStyle('highlighted', true)" or alike function?

    Thanks for help!!!

    I closed window with bug explanation (my 1st paragraph) so i couldn't supply link, but it is occurring only on windows.

  • Before you start trying to optimize your code, you should make it work. So you can try to get your desired solution with the label, but if a push button is easier, by all means use the push button.

    Later you can try to find ways to improve performance if this really is required.

  • If you don't need QPushButton's functionalities, got with QLabel.

    For your layout problem:

    QFrame *frame = new QFrame();
    QVBoxLayout *layout = new QVBoxLayout(frame);
    layout->setContentsMargins(0, 0, 0, 0); // or some other value
    QLabel *label = new QLabel(frame);
    label->setText(tr("Do some fancy stuff2));

  • It's been late when i posted this so i forgot to tell that i want to layout or other container expand to accommodate label size. i.e. when label has text "a" control width should be ~ 10px & when label has text "alternate" to control resize to ~ 70px (all based on label width without cropping). Definitely i like this method for custom controls to be created via code and now I am trying to find solution for this.

  • Just to clarify some more:

    I want to create custom control that will be styled using style sheets. It will contain text and styled according to external style sheets (set on QApp). This control will contain few words at max and it needs to resize it self so the text is always seen + padding & borders set from style sheets. This is first control.

    Second control will just have HorizontalLayout and my custom control will be added to that layout so i can have some styleable status bar.

    Thank you all for help!

  • I have some updates. First i was having container issues with my test control. I added directly to QFrame and not to layout. Now when setting Fixed size policy to both QFrame (my control) and QLabel to Fixed/Fixed all looks good (label full size + qframe border). But if i set text to label to some smaller after that, label resizes but QFrame does not so i got QFrame background visible where previously was text.

    constructor (class inherits from QFrame)
    @ _innerFrame = this;
    _innerFrame->setStyleSheet("background-color:Lime; border:3px solid #AAA; border-radius: 5px;");
    QVBoxLayout *layout = new QVBoxLayout(_innerFrame);
    layout->setContentsMargins(0, 0, 0, 0); // or some other value
    _label = new QLabel(_innerFrame);
    _label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    _label->setStyleSheet("background-color: Red;");

    code for setting text in label
    this->adjustSize(); //QFrame

  • If you set the size policy of the label to fixed it does not have any chance to enlarge according to the contents...

  • I solved it finnaly. Thanks Volker i figured that in the process. Problem is that multiple layouts "create problems" (just in my head :D). The problem that i was trying to solve is that i wanted to my QFrame and QLabel occupy less space then given by outer layout. Finally i found solution by not giving my custom control additional space by putting QSpacerItem in outer layout that will force my controls to minimum (wanted) size.

    Thanks for help!

Log in to reply