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

What 's the most pro way to hide focus rect for QTableView?



  • setStyleSheet is simple, but it seems less efficient, because qss need to be interprected, when finding a more efficient way, I got question

    code below works, but why the second parameter is made to be const?

    void MyStyle::drawControl(ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const
    {
        if (element == CE_ItemViewItem)
        {
            if (option->state & State_HasFocus)
            {
                QStyleOption * option2 = (QStyleOption *)option;
                option2->state ^= State_HasFocus;
                QProxyStyle::drawControl(element, option2, painter, widget);
            }
            else
                QProxyStyle::drawControl(element, option, painter, widget);
        }
        else
        {
            QProxyStyle::drawControl(element, option, painter, widget);
        }    
    }
    

    code below doesn't work, why?

    void MyStyle::drawControl(ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const
    {
        if (element == CE_ItemViewItem)
        {
            if (option->state & State_HasFocus)
            {
                QStyleOption option2(option);
                option2->state ^= State_HasFocus;
                QProxyStyle::drawControl(element, &option2, painter, widget);
            }
            else
                QProxyStyle::drawControl(element, option, painter, widget);
        }
        else
        {
            QProxyStyle::drawControl(element, option, painter, widget);
        }    
    }
    

    And what is the most efficient way to hide the focus rect for QTableView?


  • Moderators

    @jronald said in What 's the most pro way to hide focus rect for QTableView?:

    code below works, but why the second parameter is made be const?

    because the purpose of the method is solely drawing, and thus the method doesn't need to change the parameters basically.

    XOR'ing doesn't make any sense: option->state ^= State_HasFocus;
    You are rather looking for something like: option->state &= ~QStyle::State_HasFocus

    Also you can subclass QStyledItemDelegate and do it in there.



  • @raven-worx said in What 's the most pro way to hide focus rect for QTableView?:

    because the purpose of the method is solely drawing, and thus the method doesn't need to change the parameters basically.

    To draw according the fixed parameters, ok.

    XOR'ing doesn't make any sense: option->state ^= State_HasFocus;
    You are rather looking for something like: option->state &= ~QStyle::State_HasFocus

    Yes, the latter is simpler. The former also works, because if ensures that State_HasFocus is set, so xor make it zero, more fast, but not human friendly. I prefer the simpler one :)

    Also you can subclass QStyledItemDelegate and do it in there.

    Fine


Log in to reply