Reputation: 90776
I'm trying to find out whether a button is being pressed or not from within the paintEvent(), so that I can draw the "down" state. However, I don't know where to find this information. I tried QStyleOptionButton::state but it doesn't tell whether the button is being clicked or not. The output of the debug statement is always something like "QStyle::State( "Active | Enabled | HasFocus | MouseOver" )" so nothing about a MouseDown state.
void XQPushButton::mousePressEvent(QMouseEvent* event) {
QPushButton::mousePressEvent(event);
QStyleOptionButton options;
options.initFrom(this);
qDebug() << (options.state);
}
void XQPushButton::paintEvent(QPaintEvent* event) {
QPushButton::paintEvent(event);
QStyleOptionButton options;
options.initFrom(this);
qDebug() << (options.state);
}
So any idea how I can detect if the button is being clicked?
Upvotes: 2
Views: 2696
Reputation: 50908
QPushButton
inherits QAbstractButton
, which provides the down
property:
This property holds whether the button is pressed down.
The documentation of the QStyleOption
parent class contains an example that uses this property:
void MyPushButton::paintEvent(QPaintEvent *)
{
QStyleOptionButton option;
option.initFrom(this);
option.state = isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
//...
}
In other words, the sunken/raised state is not initialized by initFrom()
. This makes some sense, since initFrom
is inherited from QStyleOption
and takes a QWidget
:
void initFrom ( const QWidget * widget )
– and a generic QWidget
has no notion of "raised" or "sunken".
At least this is how I read the docs.
Upvotes: 3