QTableView - focusOut event



  • Hi guys,

    I'm having a hard time fixing a minor inconvenience in a custom QTableView.
    Here is the behavior:

    #1-User select a row in the QTableView:
    "first":https://www.dropbox.com/s/5syaq2nsnloasnz/first.png

    #2-User change widget, QTableView loose it's focus and the selection color change color:
    "after lost focus":https://www.dropbox.com/s/mlnmxc5j226wp1i/second.png

    What I would like :
    Remove #2

    I have tried overwriting the function "void focusOutEvent(QFocusEvent *event);" In QTableView, it works fine for the focusOut, the selection color stays the same, but as soon as the user mouse back his mouse on the QTableView, we see the gray selection again just like #2
    Also, if I use
    @void MyTableView::focusOutEvent(QFocusEvent *event) {
    qDebug () << "FOCUS OUT EVENT!";
    this->clearSelection();
    }@
    it works fine but I don't want to loose the selection every time the widget loose his focus.

    If you know which event is causing the problem, feel free!
    Thank you


  • Moderators

    how to you draw the background? via stylesheet or by your own in the delegate?



  • I use a custom delegate, one that put a different background for user mouse hover, one for when the mouse is not hovering the row.

    It's pretty complex for such simple thing, too bad stylesheet doesn't support it... here is the code :

    MyTableView.cpp
    @MyTableView::MyTableView(QWidget *parent) : QTableView(parent) {

    this->setMouseTracking(true);
    
    m_background_delegate_hover = new delegateRowHover(this, true);
    m_background_delegate_notHover = new delegateRowHover(this, false);
    
    curr_row_hovered = -1;
    

    }

    void MyTableView::mouseMoveEvent(QMouseEvent* event) {

    QPoint pos = event->pos();
    QModelIndex index = indexAt(pos);
    int row = index.row();
    
    if (index.isValid() && row != curr_row_hovered) {
    
        SortFilterProxyModel *_model = static_cast<SortFilterProxyModel*>(model());
    
        for (int i=0; i<_model->rowCount(); i++) {
            for (int j=0; j<_model->columnCount(); j++) {
    
                if (i == row) {
                    QModelIndex inn = _model->index(i, j);
                    setItemDelegateForRow(inn.row(), m_background_delegate_hover);
                }
                else {
                    QModelIndex inn = _model->index(i, j);
                    setItemDelegateForRow(inn.row(), m_background_delegate_notHover);
                }
            }
        }
        curr_row_hovered = row;
    }
    
    
    else if (!index.isValid() && curr_row_hovered != -1 ){
    
        SortFilterProxyModel *_model = static_cast<SortFilterProxyModel*>(model());
    
        for (int i=0; i<_model->rowCount(); i++) {
            for (int j=0; j<_model->columnCount(); j++) {
    
                QModelIndex inn = _model->index(i, j);
                setItemDelegateForRow(inn.row(), m_background_delegate_notHover);
            }
        }
        curr_row_hovered = -1;
    }
    

    }

    void MyTableView::leaveEvent(QEvent *event) {

    if (curr_row_hovered != -1 ) {
    
        SortFilterProxyModel *_model = static_cast<SortFilterProxyModel*>(model());
    
        for (int i=0; i<_model->rowCount(); i++) {
            for (int j=0; j<_model->columnCount(); j++) {
    
                QModelIndex inn = _model->index(i, j);
                setItemDelegateForRow(inn.row(), m_background_delegate_notHover);
            }
        }
        curr_row_hovered = -1;
    }
    

    }

    void MyTableView::focusOutEvent(QFocusEvent *event) {
    this->clearSelection(); //TODO: Fix correctly..
    }@

    delegateRowHover.cpp
    @delegateRowHover::delegateRowHover(QObject *parent, bool hovered) :QStyledItemDelegate(parent) {
    this->hovered = hovered;
    }

    void delegateRowHover::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {

    if(index.data().isValid() && index.column() != 5) {
        QStyleOptionViewItem  viewOption(option);
        painter->save();
    
        //Background color
        if (hovered)
            painter->fillRect(option.rect, QColor(30,85,110));
        else
            painter->fillRect(option.rect, QColor(35,35,35));
    

    ...
    }

    //Graph
    else if(index.column() == 5) {
    

    ...

        //Background color
        if (hovered)
            painter->fillRect(option.rect, QColor(30,85,110));
        else
            painter->fillRect(option.rect, QColor(35,35,35));
    
        if (option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());
    

    ...
    }
    }@


Log in to reply
 

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