QGridLayout 中小部件的不同对齐方式

问题描述 投票:0回答:3

以下代码(Qt 5,与 Qt 4.8 的行为相同)使用

QGridLayout
QScrollArea
的上方和左侧添加小部件(稍后用作一种标题):

#include <QApplication>
#include <QMainWindow>
#include <QGridLayout>
#include <QScrollArea>

class ColoredWidget : public QWidget {
public:
    ColoredWidget(const  QColor& color, QWidget* parent) : QWidget(parent) {
        QPalette pal;
        QBrush brush(color);
        brush.setStyle(Qt::SolidPattern);
        pal.setBrush(QPalette::Active, QPalette::Window, brush);
        setPalette(pal);
        setAutoFillBackground(true);
    }
};

class MainWindow : public QMainWindow {
public:
    MainWindow(QWidget* parent) : QMainWindow(parent) {
        resize(300, 400);

        QWidget* centralwidget = new ColoredWidget(QColor(0xff, 0xf0, 0xb5), this);

        QGridLayout* layout = new QGridLayout();
        centralwidget->setLayout(layout);
        setCentralWidget(centralwidget);

        // create widget with fixed height of 20 px and maximum width of 200
        QWidget* topHeader = new ColoredWidget(Qt::green, centralwidget);
        topHeader->setMaximumWidth(200);
        topHeader->setFixedHeight(20);

        // create widget with fixed width of 20 px and maximum height of 200
        QWidget* leftHeader = new ColoredWidget(Qt::blue, centralwidget);
        leftHeader->setFixedWidth(20);
        leftHeader->setMaximumHeight(200);

        // create scroll area as main widget
        QWidget* view = new QScrollArea();

        layout->addWidget(topHeader,  0, 1);
        layout->addWidget(leftHeader, 1, 0);
        // layout->addWidget(leftHeader, 1, 0, Qt::AlignTop); // widget not displayed at all!
        layout->addWidget(view, 1, 1);
    }
};


int main(int argc, char ** argv) {
    QApplication app( argc, argv );
    MainWindow win(0);
    win.show();
    return app.exec();
}

QGridLayout
文档说“列和行的行为相同”,因此我希望这两个小部件的布局方式相同。

但是,左边的小部件会自动垂直居中,而顶部小部件在单元格内左对齐(并且水平居中)。我希望左侧小部件也位于固定位置,意味着与顶部对齐:

Strange Layout

  • 哪个属性导致两个小部件之间出现这种不同的行为,一个处于居中,另一个处于固定位置
  • 如何影响对齐(我可能可以添加一个灵活的垫片来修复它,但理想情况下我想避免这种情况)?我尝试使用
    Qt::AlignTop
    ,但这使得蓝色小部件消失了。
qt alignment qlayout qgridlayout
3个回答
5
投票

通过以下方式设置对齐方式:

layout->setAlignment(leftHeader, Qt::AlignHCenter | Qt::AlignTop);

或与:

layout->addWidget(leftHeader, 1, 0, Qt::AlignTop);

很直观,似乎是正确的方法。如果设置完全,它也可以按预期工作。

但是为什么Widget会消失呢?
长话短说:因为 leftHeader 的大小为 0。

在我非常相似的情况下,我已经

setfixedHeight()
并且小部件重新出现,这也应该通过为
minimalHeight()
设置一个值来工作。

长篇故事 - 详细信息
我将自己置于 gridLayout 的位置并尝试确定

leftHeader
的大小:

  1. 在布局之外
    leftHeader
    的固定宽度为 20 - 可以,但没有给定高度。
    使用 QWidget 的默认值,这会导致默认高度为 0。
  2. void QGridLayout::addWidget()
    void QGridLayout::addLayout()
    状态的文档:

...
对齐方式由alignment指定。默认对齐方式是 0,这意味着小部件填充整个单元格。
...

来源

因此,对于默认对齐 0,

leftHeader
的高度显然被设置为其行的高度。
但如果使用不同的
Qt::Alignment
,如问题 (
Qt::AlignTop
) 中所示,事情就没那么简单了。

从我的角度来看,如果没有设计师的进一步设置,布局无法确定正确的高度。

快速查看

void QGridLayout::addWidget()
的实现,我们可以检测到在
QLayoutItem
中为每个添加的小部件分别创建了
QGridBox
(作为
QGridLayout
的一部分)。布局。 所以看起来
QGridLayout
依赖于Qt的默认布局机制,基于添加的小部件/布局的设置(如
minSize
maxSize
BaseSize
Size Increment
等)。

1 结合使用。) 这意味着

leftHeader
的高度为 0,因此没有任何可绘制的内容。该小部件似乎消失了。


1
投票

虽然不是最小的解决方案,但您可能可以通过在绿色小部件的单元格中添加

QHBoxLayout
并在蓝色小部件的单元格中添加
QVBoxLayout
来修复对齐。将绿色小部件添加到
QHBoxLayout
,将蓝色小部件添加到
QVBoxLayout
。然后将
addStretch(n)
应用于
QHBoxLayout
QVBoxLayout
。也许这就是您已经提到的您可以做的事情,然后我们在这方面的知识水平相同。我相信这就是我解决此类问题的方法,并且我没有花更多时间在上面。


0
投票

试试这个:

setAlignment(Qt::AlignHCenter | Qt::AlignTop );
© www.soinside.com 2019 - 2024. All rights reserved.