如何限制 QHeaderView 的大小(调整部分大小时)?

3

QHeaderView的各个部分在调整大小时可能会超出视图范围(向右)。我可以将一个部分调整得太大,其他部分就会消失到右边。

是否有可能限制这一点?例如设置标题的最大宽度。

我尝试在表格和标题上使用 setMaximumWidth(实际应用中只有标题而没有标准表格),但并没有帮助。

enter image description here

更新:

实际上,我希望其他列会缩小而不是超出可见区域,类似于 MS Word 中的情况:

enter image description here

因此,如果没有内置选项来实现此类行为,那么我认为需要找到一个适当的信号来在调整大小后调整大小,例如减小/增加下一列或上一列的大小...


嗯,你尝试设置 minimumSectionSize: int 了吗? - Surt
是的,它有什么帮助吗?它们不会缩小,只是超出可见区域(请参见滚动条)。即使限制了最大的小部件/标题大小。 - Alex P.
有一些信号,例如QHeaderView::geometriesChanged()。你需要一个告诉你表头(列)正在被调整大小的信号。然后,只要所有列都在table->viewport()->width()内,就允许移动该列。这只是一个想法,没有经过测试。 - user3606329
如何使用header()->setStretchLastSection(true)?或者一般地,对于某些列使用QHeaderView::Stretch作为ResizeMode。 - SteakOverflow
我有一个可伸缩列,但它会带来什么变化呢?我只是想在表格中拥有一些“固定”的区域,当用户调整一个列的大小时,其他列应该收缩而不是超出可见区域并添加滚动条。我已经更新了我的问题。 - Alex P.
我找不到你想要的任何内置功能。我猜你得自己管理,当用户调整列大小时,通过调整下一列的大小来实现,尽管我没有看到任何直接访问该事件column-resize的方法。 - ymoreau
2个回答

2
在Qt 5.12中设置"最初的回答"。
QHeaderView()->setCascadingSectionResizes(true);

最初的回答
并且
QHeaderView()->setSectionResizeMode(c, QHeaderView::Stretch);
c = lastColumn;

1
5.12?根据文档,这些函数从4.2-5.0版本就已经存在了。 - Alex P.

1

好的,正如一些评论所说,您可以使用来自 QHeaderView 的信号来监听更改,并随后使用 resizeSection() 进行更改。下面我编写了一个 AutoResizer,它可以执行您想要的调整大小。

AutoResizer.hpp

#ifndef AUTORESIZER_HPP
#define AUTORESIZER_HPP

#include <QObject>

class QHeaderView;

class AutoResizer : public QObject
{
    Q_OBJECT

    QHeaderView *m_header;
public:
    AutoResizer(QHeaderView *parent = 0);

private slots:
    // Listen to resized sections
    void sectionResized(int logicalIndex, int oldSize, int newSize);
};

#endif // AUTORESIZER_HPP

AutoResizer.cpp

#include "AutoResizer.hpp"

#include <QHeaderView>

AutoResizer::AutoResizer(QHeaderView *parent) : QObject(parent), m_header(parent)
{
    // Connect ourselves to the section resize signal
    connect(m_header,SIGNAL(sectionResized(int,int,int)),SLOT(sectionResized(int,int,int)));

    // Enable last column to stretch as we don't do that ourselves
    m_header->setStretchLastSection(true);
}

void AutoResizer::sectionResized(int logicalIndex, int oldSize, int newSize)
{
    // Do some bookkeeping
    int count = m_header->model()->columnCount(QModelIndex());
    int length = m_header->width(); // The width of the headerview widget
    int sum = m_header->length(); // The total length of all sections combined

    // Check whether there is a discrepancy
    if(sum != length) {
        // Check if it's not the last header
        if(logicalIndex < count) {
            // Take the next header size (the one right to the resize handle)
            int next_header_size = m_header->sectionSize(logicalIndex+1);
            // If it can shrink
            if(next_header_size > (sum - length)) {
                // shrink it
                m_header->resizeSection(logicalIndex+1, next_header_size - (sum - length));
            } else {
                // The next header can't shrink, block the resize by setting the old size again
                m_header->resizeSection(logicalIndex, oldSize);
            }
        }
    }
}

有一些注意事项。我没有在可重新排序的标题上进行测试,它提到了“logicalIndices”,这可能意味着您需要使用 sectionPosition 进行一些翻译。当视图变小时,它也不会做太多事情,但一旦列被调整大小,它就会自动修复。而且您应该查看是否将 ResizeMode 设置为 Stretch 不已经足以解决您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接