qt


Right mouse functionality in QPushButton


I want to achieve all the functionality of left button of mouse ( such as clicked(), pressed() , released() , setAutoRepeat() ) on QPushButton from the right button of mouse keeping all the left button functionality intact. Here is the code for re implementation of the eventFilter , which is not giving desirable result.
bool PreciseControl::eventFilter(QObject *, QEvent *event)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(event->type() == QEvent::MouseButtonPress &&
mouseEvent->button()==Qt::RightButton )
{
emit rightMousePressed();
return true;
}
else if(event->type() == QEvent::MouseButtonRelease &&
mouseEvent->button()==Qt::RightButton )
{
emit rightMouseReleased();
return true;
}
if(event->type() == QEvent::MouseButtonPress &&
mouseEvent->button()==Qt::LeftButton )
{
emit leftMousePressed();
return true;
}
else if(event->type() == QEvent::MouseButtonRelease &&
mouseEvent->button()==Qt::LeftButton )
{
emit leftMouseReleased();
return true;
}
return false;}
Can any one help? Thanks in advance.
You shouldn't use event filters for this. Subclass QPushButton and implement your own right click handling. Here is a working example (I didn't test it thoroughly, so there might be some problems):
#include <QApplication>
#include <QPushButton>
#include <QMouseEvent>
#include <QLayout>
#include <QDebug>
class CustomButton : public QPushButton
{
Q_OBJECT
public:
explicit CustomButton(QWidget *parent = 0) : QPushButton(parent){}
protected:
void mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton && hitButton(e->pos()))
{
setDown(true);
emit pressed();
}
QPushButton::mousePressEvent(e);
}
void mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() & Qt::RightButton)
{
if(hitButton(e->pos()) != isDown())
setDown(!isDown());
}
QPushButton::mouseMoveEvent(e);
}
void mouseReleaseEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton)
{
setDown(false);
if(hitButton(e->pos()))
click();
}
QPushButton::mouseReleaseEvent(e);
}
};
void buttonClicked()
{
qDebug() << "CLICKED";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.resize(500,500);
w.setLayout(new QVBoxLayout);
CustomButton button;
button.setAutoRepeat(true);
QObject::connect(&button, &CustomButton::clicked, &buttonClicked);
w.layout()->addWidget(&button);
w.show();
return a.exec();
}
#include "main.moc"
If you want to differentiate between the right click and the left click, then see the source code for QAbstractButton::click, and implement your own for the right click.
You can implement the functionality in an event filter, and install it on the button(s) you want to be right-clickable.
// https://github.com/KubaO/stackoverflown/tree/master/questions/right-to-left-event-39220180
#include <QtWidgets>
class RightToLeftClick : public QObject {
bool eventFilter(QObject *watched, QEvent *event) {
if (event->type() == QEvent::MouseButtonDblClick ||
event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease) {
auto ev = static_cast<QMouseEvent*>(event);
if (ev->button() == Qt::RightButton) {
auto buttons = ev->buttons();
if (buttons & Qt::RightButton) {
buttons ^= Qt::RightButton;
buttons |= Qt::LeftButton;
}
QMouseEvent lev{ev->type(),
ev->localPos(),
ev->windowPos(),
ev->screenPos(),
Qt::LeftButton,
buttons,
ev->modifiers(),
ev->source()};
Q_ASSERT(! (lev.buttons() & Qt::RightButton));
QCoreApplication::sendEvent(watched, &lev);
return true;
}
}
return QObject::eventFilter(watched, event);
}
public:
explicit RightToLeftClick(QObject * parent = nullptr) : QObject{parent} {
addTo(parent);
}
void addTo(QObject * obj) {
if (obj) obj->installEventFilter(this);
}
};
Here's how to use it:
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget ui;
QVBoxLayout layout{&ui};
QPushButton button1{"Left Click Me"};
QPushButton button2{"Right Click Me"};
layout.addWidget(&button1);
layout.addWidget(&button2);
ui.show();
RightToLeftClick rtl{&button2};
return app.exec();
}

Related Links

qmake eval function “always true” for string literals
Implementing a GUI Shell in qt
Get rid of Qt's dotted outline on QListView items
How to stop the transition animation in QML?
How does Qt on Windows draw exactly? Does it call GDI internal to draw?
QML: aliases to children properties
QTreeView remove decoration/expand button for all items
Share window created in Qt C++ with PyQt
Qt: QLineEdit cursor moves to end after textChanged() or commitData()
How can I extract plain text (no markup) from QWebElement?
How can I paint on a specific pixel in BlackBerry 10, Qt QML Cpp
Size of Qt containers: is QMap much larger than Qlist?
Qt QProcess Complains about QThread::Start , Thread Creation Error
QML Repeater itemAt not working
How to reach a Qt widget from another class
Qt Popup as a completer window

Categories

HOME
apple-push-notifications
yaml
focus
swi-prolog
loopbackjs
whmcs
google-play-services
mirc
linker
clone
ag-grid
firebase-dynamic-links
angular2-template
specifications
alljoyn
watch-os-3
chromecast
cuba-platform
sonicwall
varnish-vcl
activeadmin
quantitative-finance
ava
chronicle-queue
restier
opentext
tracing
running-object-table
jackrabbit-oak
tortoisehg
docx4j
commonsware-cwac
markov-chains
assertions
cargo
djcelery
eclipse-gmf
vb.net-to-c#
pyttsx
reverse-dns
webalizer
roracle
sharpdx
node-apn
exc-bad-access
algebra
deedle
emacs25
alertify
.when
mpmovieplayercontroller
mifos
java-2d
django-testing
ftp-client
dpkg
cdo.message
android-syncadapter
fdt
django-redis
wepay
servicestack-bsd
cryptojs
http.client
cfwheels
blank-line
wso2ml
ngcordova
darwin
actionpack
subfolder
encryption-symmetric
symja
dc
utf8-decode
device-admin
iplimage
berkeley-db-xml
squeezebox
csslint
sqlbindparameter
map
lsa
visual-c++-2005
notifyjs
contextswitchdeadlock
towers-of-hanoi
voice-recording
autosize
memory-pool
uipasteboard
page-curl
jquery-1.4
simile
grails-validation
anonymous-types
solandra
powergui
comment-conventions
gui-designer
word-automation

Resources

Encrypt Message