Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

qt - How to have a fixed-size layout that also keeps the window from resizing?

I have a container widget.

It's size policies are "Fixed" (both vertical and horizontal). it has setMaxmimumWidth and setMaximumHeight defined.

I put an QHBoxLayout container there. adding widgets to it (with fixed width/height too). But when i call "Show", none of this gets respected. The height is random almost, doesn't match any of the internal widgets in the layout, twice the size of the tallest item, and the width of my container is stretched far beyond the fixed width I set it.

What am I missing? Why can't the container stay with fixed width/height as I defined it? why is the layout interfering with it and not respecting its constraints?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The confusion comes from the fact that widgets that have fixed sizes do indeed have fixed sizes. The following holds: if a layout has only fixed-size widgets, then the layout's overall size is nominally fixed, and the widgets inside of it can't grow. If you set it as a layout on a window, it can constrain the window from growing if you set the SetFixedSize constraint on it. It is useless to set any sort of size policies on a window with a layout that has only fixed size items - unless you want to grow the spacing between the widgets.

If you want your widgets to stay fixed size, but the spacing between them to grow, the only change necessary to the code below is to set all widgets to fixed size policy. Since they can't grow, the spacing is the only thing that can. Literally set all policies in the example to QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) and you'll get this behavior.

If you want widgets of a certain minimum size and grow-able, and the overall container widget of a certain minimum size, then just set it so. This means:

  1. Set the minimum, maximum or fixed size or width/height on the container widget.

  2. Set the minimum, maximum or fixed sizes or widths/heights on the widgets.

  3. Set the size policies on the widgets to reflect the behavior you want. You may want, for example, some of the widgets to grow in certain directions only, or not at all.

At least one widget will need to grow in a given direction if you set the minimum size on the container. Thus, if you set the width on the container, at least one laid out widget must be able to expand horizontally to fill it. If you set the height on the container, at least one laid out widget must be able to expand vertically to fill it.

The below works fine under Qt 4.8.5 and 5.1.1. The window can be expanded but not contracted - it has a minimum size. You can change the setMinimumSize to setFixedSize to obtain a window with fixed size, or you can set both minimum and maximum sizes.

screenshot

#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QFontDatabase>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget window;
    QHBoxLayout layout(&window);

    QLabel lbl1("one");
    lbl1.setStyleSheet("QLabel { background-color: #FF8080 }");
    lbl1.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));

    QLabel lbl2("two");
    lbl2.setStyleSheet("QLabel { background-color: #80FF80 }");
    lbl2.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    lbl2.setMinimumHeight(300);

    QLabel lbl3("three");
    lbl3.setStyleSheet("QLabel { background-color: #8080FF }");
    lbl3.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    lbl3.setMinimumWidth(300);

    layout.addWidget(&lbl1);
    layout.addWidget(&lbl2);
    layout.addWidget(&lbl3);
    window.setMinimumSize(800, 800);
    // Any combination of setMinimumSize and setMaximumSize is OK.
    // If the minimum and maximum are the same, just do setFixedSize

    window.show();
    return a.exec();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...