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

QTableView QHeaderView section moved - styling



  • Hello,

    I would like to change style "moving section" in QTableView when columns order is changed? Please exist some easy way?

    QHeaderView::section:?????? { color: red; }

    Thanks. David.


  • Moderators

    @David.Reznicek
    this is not possible.
    There may be only ways to change all tabs during the drag, but not the dragged one.



  • @raven-worx Thank you for response.



  • @raven-worx said:

    this is not possible.

    Everything is possible!

    you just need to create your own headerview reimplementing the paint event


  • Moderators

    @VRonin said:

    Everything is possible!

    you just need to create your own headerview reimplementing the paint event

    Of course everybody can download the Qt source code and modify it to his needs...

    I am very interested in your suggestion how to implement your paint() method implementation.
    I am pretty sure that the paint() method is simply not enough. And that most of the stuff happens in the private part of the class.

    Edit:

    checked the Qt sources. The dragged section is painted to a pixmap, which is then set to a label and this label, which is moved during the drag.
    The section is painted using paintSection(). So you can just change what the style option offers. And even this is not guaranteed and may be overwritten/ignored internally by the QStyle during drawing the control.
    So you you can copy as much of the code from paintSection() implementation and add a member to the paintEvent handler.
    The paintSection() should be called (may be more often than i found) only twice. Once in the paintEvent() handler and once for the dragging.
    So you you can copy as much of the code from paintSection() implementation as possible and add a member to the paintEvent handler to be able to distinguish from where you get called.

    void MyHeaderView::paintEvent(QPaintEvent* event)
    {
         callFromPaintEvent = true;
         QHeaderView::paintEvent(event);
         callFromPaintEvent = false;
    }
    
    void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    {
          if( callFromPaintEvent  )
               return QHeaderView::paintSection( painter, rect, logicalIndex );
    
          // do the painting for the dragged section
    }
    

    This solution is far beyond being ideal and also it's not very reliable (due to QStyle drawing).


Log in to reply