Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom QHeaderView doesn't adjust size

Custom QHeaderView doesn't adjust size

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 245 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • rudagR Offline
    rudagR Offline
    rudag
    wrote on last edited by
    #1

    Hello

    I'm subclassing QHeaderView based on this example and on @Oak77 's post here. The filters are working fine but I can't resize the headers or even click them for sorting. Method resizeColumnsToContents() also doesn't work. What might be wrong?

    #include "filterheader.h"
    #include "copytable.h"
    #include <QHeaderView>
    #include <QTableView>
    #include <QLineEdit>
    #include <QScrollBar>
    
    FilterHeader::FilterHeader(QTableView *parent) :
        QHeaderView(Qt::Horizontal, parent)
    {
        _padding = 4;
        setStretchLastSection(true);
        setSectionResizeMode(QHeaderView::Stretch);
        setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        setSortIndicatorShown(false);
        connect(this, &QHeaderView::sectionResized, this, &FilterHeader::adjustPositions);
        connect(parent->horizontalScrollBar(), &QScrollBar::valueChanged, this, &FilterHeader::adjustPositions);
    }
    
    FilterHeader::FilterHeader(CopyTable *parent) :
        QHeaderView(Qt::Horizontal, parent)
    {
        _padding = 4;
        setStretchLastSection(true);
        setSectionResizeMode(QHeaderView::Stretch);
        setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        setSortIndicatorShown(false);
        connect(this, &QHeaderView::sectionResized, this, &FilterHeader::adjustPositions);
        connect(parent->horizontalScrollBar(), &QScrollBar::valueChanged, this, &FilterHeader::adjustPositions);
        show();
    }
    
    void FilterHeader::setFilterBoxes(int count)
    {
        while (!_editors.empty())
        {
            QLineEdit* editor = _editors.back();
            _editors.pop_back();
            editor->deleteLater();
        }
        for (int i = 0; i < count; i++)
        {
            QLineEdit* editor = new QLineEdit(parentWidget());
            editor->setPlaceholderText("Filter");
            connect(editor, &QLineEdit::returnPressed, this, &FilterHeader::emitFilterActivated);
            _editors.push_back(editor);
            editor->show();
        }
        adjustPositions();
    }
    
    void FilterHeader::emitFilterActivated()
    {
        emit filterActivated(_editors.indexOf(sender()));
    }
    
    QSize FilterHeader::sizeHint() const
    {
        QSize size = QHeaderView::sizeHint();
        if (!_editors.empty())
        {
            int height = _editors[0]->sizeHint().height();
            size.setHeight(size.height() + height + _padding);
        }
        return size;
    }
    
    void FilterHeader::updateGeometries()
    {
        if (!_editors.empty())
        {
            int height = _editors[0]->sizeHint().height();
            setViewportMargins(0, 0, 0, height + _padding);
        }
        else
            setViewportMargins(0, 0, 0, 0);
    
        QHeaderView::updateGeometries();
        adjustPositions();
    }
    
    void FilterHeader::adjustPositions()
    {
        for (int i = 0; i < _editors.size(); i++)
        {
            QLineEdit* editor = _editors[i];
            int height = editor->sizeHint().height();
            editor->move(sectionPosition(i) - offset() + qobject_cast<QTableView*>(parent())->verticalHeader()->width(), height + (_padding / 2));
            editor->resize(sectionSize(i), height);
        }
    }
    
    QString FilterHeader::filterText(int index) const
    {
        if (index >= 0 && index < _editors.size())
            return _editors[index]->text();
    
        return QString();
    }
    
    void FilterHeader::setFilterText(int index, const QString& text)
    {
        if (index >= 0 && index < _editors.size())
            _editors[index]->setText(text);
    }
    
    void FilterHeader::clearFilters()
    {
        for (auto editor : _editors)
            editor->clear();
    }
    
    

    Code below at the end of the function that loads the tableView

    FilterHeader* header = new FilterHeader(ui->tableView);
    ui->tableView->setHorizontalHeader(header);
    header->setFilterBoxes(model->columnCount());
    connect(header, &FilterHeader::filterActivated, this, &Extratos::handleFilterActivated);
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      setSectionResizeMode(QHeaderView::Stretch); <- this is why you cannot manually resize.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      rudagR 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        setSectionResizeMode(QHeaderView::Stretch); <- this is why you cannot manually resize.

        rudagR Offline
        rudagR Offline
        rudag
        wrote on last edited by
        #3

        @SGaist resizing is working now! But still the headers are "unclickable", they dont even highlight if I move the cursor over them.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If you want to sort, why are you setSortIndicatorShown(false); ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved