View Full Version : QPushButton on Shift+Click?
vishal.chauhan
9th September 2008, 10:37
Hi All,
I want to implement a QPushButton in such a way that it do something when user click it
for which I can use clicked() signal but I also want that when user click while shift button is pressed then it will do something else.
How can I do it?
Thanks
Vishal Chauhan
spirit
9th September 2008, 10:45
you can subclass QPushButton and reimplement keyPressEvent or you can install event fillter for your button and process QKeyEvent.
vishal.chauhan
9th September 2008, 10:51
Thanks for reply.
Can you give some sample code for doing it.
spirit
9th September 2008, 11:00
sorry, not QKeyEvent, but QMouseEvent :)
1. subclassing
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
if (e->modifiers() == Qt::ShiftModifier) {
//do what you need
return;
}
QPushButton::mousePressEvent(e);
}
2. installFilter
...
pushButton->installEventFilter(this);
...
bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == pushButton && event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (event->modifiers() == Qt::ShiftModifier) {
//do what you need
return true;
}
}
return QWidget::eventFilter(obj, event);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.