How to detect whether mouseEvents take place over this or not?
-
When I press the mouse over this (ActionButton), it turns red and as I move mouse (while it's pressed) away from this, the
leaveEvent
doesn't fire. That's ok, I want to suppress theemit triggered()
in themouseReleaseEvent
if it's released elsewhere (not over this). Here's what happened:First, the
Print
button is disabled so it shouldn't change color, I wanted to make it light gray when it's disabled and reset the color onisEnabledChanged
, looks like there's no such event to handle the effect ofsetEnabled
, is there any?Second, when I moved mouse away, while it was pressed, from the plus icon on
Objects
section it was red and as soon as I released mouseemit triggered
was called so the file dialog opened. I want it to open the file dialog if the mouse is released over that icon otherwise ignore. Here's what I've for now:ActionButton::ActionButton(const QString &fileName, const QString toolTip, int size, QWidget *parent) : QSvgWidget(parent) { QFile f(fileName); f.open(QIODevice::ReadOnly | QIODevice::Text); m_document.setContent(f.readAll()); setToolTip(toolTip); m_isChecked = false; if(size == 0) setMaximumSize(QSize(16,16)); else setMaximumSize(QSize(size, size)); changeColor(QColor(Qt::black)); } bool ActionButton::isChecked(){ return m_isChecked; } void ActionButton::setIsChecked(bool value){ if(m_isChecked == value) return; m_isChecked = value; if(value) changeColor(Qt::blue); else changeColor(Qt::black);; } void ActionButton::enterEvent(QEnterEvent*){ if(!m_isChecked) changeColor(Qt::blue); } void ActionButton::leaveEvent(QEvent*){ if(!m_isChecked) changeColor(Qt::black); } void ActionButton::mousePressEvent(QMouseEvent*){ if(!m_isChecked) changeColor(Qt::red); } void ActionButton::mouseReleaseEvent(QMouseEvent*){ if(!m_isChecked) emit triggered(); } void ActionButton::changeColor(const QColor &color){ m_document.elementsByTagName("path").at(0).toElement().setAttribute("fill", color.name()); renderer()->load(m_document.toByteArray()); }
-
Then I would recommend taking a look at the various button class implementation directly and start with QAbstractButton.
-
Hi,
Beside your SVG color customization, doesn't QPushButton provide the behaviour you want ?
-
As @SGaist said, QPushButton or QAbstractButton already implements that behaviour.
I want it to open the file dialog if the mouse is released over that icon otherwise ignore
if(! hitButton(event->pos())) return;
I wanted to make it light gray when it's disabled
isEnabled() exists in QAbstractButton
and isDown() for highlight the button when clicked inside. -
Then I would recommend taking a look at the various button class implementation directly and start with QAbstractButton.