Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView - focusOut event
Forum Updated to NodeBB v4.3 + New Features

QTableView - focusOut event

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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());
        

        ...
        }
        }@


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved