Handle Left-Right Double Click on QListWidget
-
Hello,
Qt doesn't see a difference between a Double Left Click and a Double Right Click, and I would like to do different things depending on the Double Click. So I had to code that on my own but that didn't work.
My problem is that I have a QListWidget A, a QListWidget B and a QListWidget C. Whenever I Double Left Click an item in QListWidget A, I would like it to be added to QListWidget B. But if I Double Right Click it, I would like it to be added to QListWidget C instead.
So this is what I did:
@QListWidget searchList;
searchList.setParent(&mainWidget);
QObject::connect(&searchList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(MoveCard(QListWidgetItem*)));ClickEater eater;
searchList.installEventFilter(&eater);@(moveItem is the slot to be used that I implemented myself for double clicks)
ClickEater has the following definition:
@#include <QMouseEvent>
class ClickEater : public QObject
{
Q_OBJECTpublic: bool eventFilter(QObject*, QEvent*); bool Goto;
};@
and eventFilter has the following definition:
@ bool ClickEater::eventFilter(QObject obj, QEvent event)
{
QMouseEvent truc = (QMouseEvent) event;if (truc->type() == QEvent::MouseButtonPress) { if (truc->button() == Qt::LeftButton) Goto = false; else if (truc->button() == Qt::RightButton) Goto = true; } else return QObject::eventFilter(obj, event); return true;
}@
That way, whenever I (simple) click an item of QListWidget A, Goto is set to true or false. If I double click it, the last value of Goto will tell me what kind of double click it was. But that doesn't work. Even if I cast event into a QMouseEvent, I still never enter "if" nor "else". Help please!
-
Hi, welcome to devnet.
First of all this code is not ok:
@
QMouseEvent* truc = (QMouseEvent*) event;
if (truc->type() == QEvent::MouseButtonPress)
@
You shouldn't cast first and then check for event type. The C type cast will always succeed, even if it's not a correct type (i.e. reinterpret_cast). From there it's undefined behavior. This should be
@
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* truc = static_cast<QMouseEvent*>(event);
...
@But anyway there's no need for an event filter here. You can check which button was pressed a lot easier:
@
//assuming this is your slot
void Whatever::moveCard(QListWidgetItem* item)
{
auto buttons = qApp->mouseButtons();
if(buttons & Qt::LeftButton)
//do something
else if(buttons & Qt::RightButton)
//do something else
}
@ -
Hello,
thank you for your fast reply. This was the code I tested:
@void Interface::moveCard(QListWidgetItem* Item)
{
auto buttons = qApp->mouseButtons();
std::cout << "??" << std::endl;if(buttons & Qt::LeftButton)
std::cout << "ha" << std::endl;
else if(buttons & Qt::RightButton)
std::cout << "ho" << std::endl;
}@But unfortunately, I never see "ha" nor "ho" :( Just:
@??
??
??
??
??@ -
I still need help I'm afraid :(
-
This method works and I checked it before posting so there must be something else in your app interfering with it. Can you post somewhere the complete project or, better yet, a small reproducible code that doesn't work with this method?
Oh, I forgot to ask which OS are you on. On Windows the double-click signal is emitted before the final release so mouseButtons() has meaningful info. This might not be the case on other platforms.
-
I am using X-ubuntu (latest LTS).
Well what is qApp exactly? I do declare in main
@QApplication app(argc, argv);@
but I can't rename app into qApp for some reason.
-
Hi,
qApp is a macro used to get the current instance of your QApplication. "Here":http://qt-project.org/doc/qt-5/qapplication.html#qApp for more informations
-
I see thanks.
So does this mean my problem has no solutions on Linux?
-
Maybe you can subclass the list widget, reimplement the mouse press/release event and emit a custom signal that would indicate which button was pressed.
-
Oh okay. I'm a newbie regarding Qt unfortunately, I would really need to see very similar examples so that I could get this done.
This is surprising that so simple a problem needs such complicated solutions, though.
-
Well it might be a simple case for you but right button double clicks are not something you see every day. I would even dare to say that's unintuitive.
For some info and examples on overloading event handlers you can go "here":http://qt-project.org/doc/qt-5/eventsandfilters.html#event-handlers
There's also a lot of that stuff on the net. Just look for "qt overriding mousepressevent" eg. "here":http://stackoverflow.com/questions/2733668/qwidget-keypressevent-override -
Okay before trying the long way to do it, I replaced the itemDoubleClicked signal by simply itemClicked. I still print neither "haha" nor "hoho" after the second click (when I double click). Are you sure Ubuntu is the problem here?
buttons is always equal to 0 (when I print it)
-
If you really want to implement the non-standard double right click (you might even be going against the guidelines from Apple/Microsft/etc.), you should rather look at the mouse events functions.