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

How to change QPushButton hover area?



  • Hi,

    I have a QPushButton ( or class which inherits QPushButton ) and I set a new style for this button using setStyleSheet(). I set for example margin and QPushButton:hover section ( color etc. ).

    When my mouse cursor is on the margin area, hover is activated. I would like to decrease hover area. Hover can't be on margin section.

    I know that I can change click area using QAbstractButton::hitButton() and I would like to do the same with hover.

    I tried with eventFilter() like this:

    bool MyButton::eventFilter(QObject *obj, QEvent *event){
        auto eventType = event->type();
    
        if( eventType == QEvent::HoverEnter || eventType == QEvent::HoverMove ){
            auto hoverEv = static_cast<QHoverEvent *>(event);
            QRect newRect = QRect(0,0,50,50);
            if( !newRect.contains(hoverEv->pos()) ){
                return true;
            }
        }
    
        return QWidget::eventFilter(obj, event);
    }
    

    And the problem is:
    button.png

    I have mouse cursor in red X position. Now I move mouse cursor up. And in yellow circle point I get hover enter event. I check if my blue rectangle ( 0,0,100,100 ) contains that yellow circle point. No. So I do nothing with that event. Next I move cursor left-up and in orange circle point I don't get hover enter event. I would like to get it here.


  • Lifetime Qt Champion

    Hi,

    There's no "move the hover entry points" concept applicable. What you could do is generate a synthetic event or use an overlay widget.


Log in to reply