Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Last empty column



  • 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 ?



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

    and override mousePressEvent:

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


  • @QWorkingClassHero

    Subclass QTableView and do whatever you want to do with your last column.



  • I did override mousePressEvent() in a subclass of QHeaderView to skip (inactivate) 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.



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

    and override mousePressEvent:

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

Log in to reply