0

In a QTableView I need the last (rightmost) column to be empty, expandable but not movable. The goal is that the table not to suddenly end (for I use alternate color for rows) or to ugly expand to the right. In QHeaderView there is a setFirstSectionMovable(bool); I need something similar for the last section, letting the rest of them movable. (in other words: Fill the rest of the table with an empty not movable column). Any clue how to acheive this?

I did override mousePressEvent() in a subclass of QHeaderView to skip the last section but it still can be moved by moving other column in its place and I don't know how to prevent this.

  • Did you try reimplementing `moveSection()`? https://doc.qt.io/qt-5/qheaderview.html#moveSection – Minh Nov 08 '20 at 20:39

1 Answers1

0

AMOQ:

Be HeaderView a subclass of QHeaderView and have

setSectionsMovable(true);
setStretchLastSection(true);

Treat the sectionMoved signal:

connect(this, &QHeaderView::sectionMoved, [this](int, int, int newVisual) {                
  if(newVisual == count()-1) { moveSection(newVisual, newVisual-1); }
});

Override mousePressEvent:

void HeaderView::mousePressEvent(QMouseEvent* event) {    
int logicalIdx = logicalIndexAt(event->pos());       
if(logicalIdx != count()-1) { QHeaderView::mousePressEvent(event); }
}