简体   繁体   中英

How to limit QHeaderView size (when resizing sections)?

QHeaderView sections can go out of view (to the right) when resizing. That is I can resize a section to be too big and other sections will disappear to the right.

Is it possible to limit this? Such as set max width of header.

I tried setMaximumWidth on table and header (actually in the real app I have just header without standard table) but it didn't help.

在此输入图像描述

UPD:

Actually I want other columns to shrink instead of going out of visible area, something like in MS Word:

在此输入图像描述

So if there is no built-in option for behavior like this, then I guess I need to find a suitable signal to adjust sizes after resizing, such as decreasing/increasing size of the next or previous column...

In Qt 5.12 set

QHeaderView()->setCascadingSectionResizes(true);

and

QHeaderView()->setSectionResizeMode(c, QHeaderView::Stretch);
c = lastColumn;

Well, as some comments have said, you can use signals from the QHeaderView to listen for changes and subsequently change them using resizeSection() . Below I have written a AutoResizer which performs your wanted resizing.

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);
            }
        }
    }
}

There are some caveats. I haven't tested this on reorder-able headers, it talks about "logicalIndices" which might mean you need some translation using sectionPosition . It also doesn't do much when the view is made smaller though it will fix itself once a column is being resized. And you should look whether setting the ResizeMode to Stretch doesn't already sufficiently solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM