Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent
-
I have a widget where I draw this button:
void QWidget::paintEvent(QPaintEvent *event) { QIcon icon = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical); QStyleOptionButton opt; opt.state = QStyle::State_Active | QStyle::State_Enabled; opt.rect = QRect(50, 25, 100, 50); QPainter painter(this); painter.drawText(MAIN.height() / 2, 30, 80, 30, Qt::AlignCenter, ""); painter.beginNativePainting(); qApp->style()->drawControl(QStyle::CE_PushButton,&opt,&painter); painter.endNativePainting(); QWidget::paintEvent(event); }
I want to know how would I connect it to a slot when it's clicked, if that's possible.
-
If you want a QPushButton why don't you simply use one instead trying to paint it by yourself?
If you really want to handle a click on a widget by yourself, you have to overwrite QWidget::mousePressEvent()
-
@Christian-Ehrlicher Because the button has to drawn inside paintEvent, like everything else in the widget
-
@hbatalha
If you use QAbstractButton as the base class, the following signals are available:void clicked(bool checked = false)
void pressed()
void released()
void toggled(bool checked)as well as standard button states: checkable, checked, pressed (isDown) etc.
-
@hbatalha said in Clicked signal and slot for a QStyleOptionButton drawn inside paintEvent:
Because the button has to drawn inside paintEvent, like everything else in the widget
This does not make any sense to me. Why needs a widget paint a QPushButton? Why can't you just simply use a QPushButton widget?
-
@Christian-Ehrlicher sorry. that's true, that doesn't make sense. I was thinking about it in a different way ( a wrong way btw).
Using the regular qpushbutton does the job.