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

Implementing hover



  • i need to activate hover or implement it whitin PaintEvent() .
    i can not use StyleSheet.

    how can i do it?


  • Lifetime Qt Champion

    Hi
    You need to subclass and override enterEvent/leaveEvent
    Then you can use a variable to know if you been hovered and paint differently in paintEvent.

    
    class Label : public QLabel {
      Q_OBJECT
    public:
      Label(QWidget* parent = Q_NULLPTR) :QLabel("test", parent) {}  
    protected:  
      virtual void enterEvent(QEvent* event) override {
        event->accept();
        qDebug() << "enterEvent" << objectName();
      }
      virtual void leaveEvent(QEvent* event) override {
        event->accept();
      }
    };
    
    

    Notice, i used Label here. you might want plain QWidget instead.



  • Answer above is more flexible, but may be overkill if your need to hover is more trivial (like displaying tool tip text), then you can simply set the setToolTip(Qstring) for each widget where you want a tip to pop up on hover. See

    QWidget::setToolTip(QString& str)
    QWidget::setToolTipDuration(int msec)



  • i used in "underMouse()" in paintEvent(). with WA_Hover attribute set.
    it works as i need.


Log in to reply