QHeaderView change mouse click behavior



  • Hi everyone

    I have a table view with a QSortFilterProxyModel. The user can bind a column to QSortFilterProxyModel by Ctrl+Click the header. The column bound to the proxy model gets marked with an asterisks, but also the the sort behavior gets changed. How can I prevent the latter if the user "Ctrl+Clicks". The sort behavior should only be changed while no Ctrl-key is pressed.

    Do I have to subclass QHeaderview and reimplement mouse events?


  • Moderators

    @Qutie said in QHeaderView change mouse click behavior:

    Do I have to subclass QHeaderview and reimplement mouse events?

    yes, exactly.
    Reimplement mousepress/mouserelease event handlers (alternatively install an event-filter) and simply don't call the base-class implementation in case CTRL is contained in the event.



  • Thank's. After set my subclassed header view to TableView, the signal SectionPressed() was not fired. QTableView sets standard HeaderView SectionClickable to true. So I had to set it as well to true.

    MarkHeaderView::MarkHeaderView(Qt::Orientation orientation, QWidget *parent)
       :QHeaderView(orientation,parent)
    {
       setSectionsClickable(true);
       setHighlightSections(true);
    }
    
    void MarkHeaderView::mousePressEvent(QMouseEvent *e){
      if(QGuiApplication::queryKeyboardModifiers() != Qt::ControlModifier){
          QHeaderView::mousePressEvent(e);
       }
      else{
          emit SectionCtrlPressed(logicalIndexAt(e->pos()));
      }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.