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. Widgets don't receive hover event in windows style
Forum Updated to NodeBB v4.3 + New Features

Widgets don't receive hover event in windows style

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 195 Views
  • 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.
  • S Offline
    S Offline
    schrute
    wrote on last edited by
    #1

    I am trying to change the appearance of QMenuBar item on hover, however, it seems the widget doesn't receive the hover event. Only the sunken state is getting honoured. Following is my style code

    class MenuBarStyle : public QProxyStyle
    {
    public:
    	PtMenuBarStyle()
    		:QProxyStyle("windows") 
    	{}
    
    	void drawControl(ControlElement element, const QStyleOption* pOption,
    		QPainter* pPainter, const QWidget* pWidget = nullptr) const override
    	{
    		if (element == CE_MenuBarItem)
    		{
    			if (pOption->state & QStyle::State_MouseOver
    				|| pOption->state & QStyle::State_Sunken)
    			{
    				pPainter->fillRect(pOption->rect, QColor("#FF0000"));
    			}
    			QCommonStyle::drawControl(element, pOption, pPainter, pWidget);
    		}
    		else if (element == CE_MenuBarEmptyArea)
    		{
    
    		}
    		else {
    			QProxyStyle::drawControl(element, pOption, pPainter, pWidget);
    		}
    	}
    
    	int pixelMetric(PixelMetric metric, const QStyleOption* pOption = nullptr,
    		const QWidget* pWidget = nullptr) const override
    	{
    		if (metric == PM_MenuBarItemSpacing) {
    			return PtUtil::getDimensionBasedOnDPI(16);
    		}
    		else {
    			return QProxyStyle::pixelMetric(metric, pOption, pWidget);
    		}
    	}
    
    
    };
    

    I have also called setAttribute(Qt::WA_Hover) and setMouseTracking(true) on the QMenuBar object but it still doesn't work.

    Christian EhrlicherC 1 Reply Last reply
    0
    • S schrute

      I am trying to change the appearance of QMenuBar item on hover, however, it seems the widget doesn't receive the hover event. Only the sunken state is getting honoured. Following is my style code

      class MenuBarStyle : public QProxyStyle
      {
      public:
      	PtMenuBarStyle()
      		:QProxyStyle("windows") 
      	{}
      
      	void drawControl(ControlElement element, const QStyleOption* pOption,
      		QPainter* pPainter, const QWidget* pWidget = nullptr) const override
      	{
      		if (element == CE_MenuBarItem)
      		{
      			if (pOption->state & QStyle::State_MouseOver
      				|| pOption->state & QStyle::State_Sunken)
      			{
      				pPainter->fillRect(pOption->rect, QColor("#FF0000"));
      			}
      			QCommonStyle::drawControl(element, pOption, pPainter, pWidget);
      		}
      		else if (element == CE_MenuBarEmptyArea)
      		{
      
      		}
      		else {
      			QProxyStyle::drawControl(element, pOption, pPainter, pWidget);
      		}
      	}
      
      	int pixelMetric(PixelMetric metric, const QStyleOption* pOption = nullptr,
      		const QWidget* pWidget = nullptr) const override
      	{
      		if (metric == PM_MenuBarItemSpacing) {
      			return PtUtil::getDimensionBasedOnDPI(16);
      		}
      		else {
      			return QProxyStyle::pixelMetric(metric, pOption, pWidget);
      		}
      	}
      
      
      };
      

      I have also called setAttribute(Qt::WA_Hover) and setMouseTracking(true) on the QMenuBar object but it still doesn't work.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QMenuBar::paintEvent() does not set the MouseOver state: https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qmenubar.cpp#n882 and https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qmenubar.cpp#n882

      Looks like you have to derive from QMenuBar, override initStyleOption() and set it by your yourself.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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