PDA

View Full Version : mousePressEvent Only Called on Rightclick



TEAmerc
19th October 2015, 20:07
I've reimplemented mousePressEvent from QMainWindow so I can start a drag and drop of a QDockWidget that is a child of said QMainWindow since I wanted to be able to drag and drop QDockWidgets between QMainWindows. Everything about the reparenting process works, except my problem is my mousePressEvent only gets called when I'm holding both the right (RMB from here) and left mouse buttons (LMB from here) down. My implementation of mousePressEvent is below:



void TableViewWindow::mousePressEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;

QDockWidget *draggedChild = static_cast<QDockWidget*>(childAt(event->pos()));
if (!draggedChild)
return;

QPoint hotSpot = event->pos() - draggedChild->pos();
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData();
mimeData->setText((this->objectName()) + "." + (draggedChild->objectName()));

QPixmap pixmap(draggedChild->size());
draggedChild->render(&pixmap);

drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);

Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
(void)dropAction; // Remove unused variable compiler warning
}


If I remove the lines



if (!(event->buttons() & Qt::LeftButton))
return;


Then my mousePressEvent will get called when I'm dragging using RMB and won't work with LMB but I have no mention of RMB anywhere in my code and I'm not sure why the mouse press is limited to RMB. If the issue is caused because of a conflict between the contained QDockWidget's mousePressEvent that I'm dragging and this one is there a way I could make something like CTRL + LMB bypasses the QDockWidget's mousePressEvent and then have this QMainWindow's mousePressEvent called from CTRL + LMB because the current RMB usage is pretty unintuitive to users but I don't know how to change it since I never specified it anywhere.

Thanks.

TEAmerc
20th October 2015, 16:39
So I've made some progress in finding out it's not actually the QDockWidget within the QMainWindow that's "consuming" the mousePressEvent, but it's specifically the titlebar of the QDockWidget that's "consuming" the LMB click and not propagating it up to the QMainWindow while it does propagate up the RMB click and that's why my QMainWindow's mousePressEvent is only getting called via RMB.

In order to fix this can someone tell me what kind of widget a QDockWidget's titlebar is? I just need to find what it is, subclass it, and change the MousePressEvent to ignore Mouse Events if CTRL is held down and otherwise I want it to be exactly the same as the default titlebar.

Thanks

anda_skoa
24th October 2015, 09:24
In order to fix this can someone tell me what kind of widget a QDockWidget's titlebar is?

You could check the code, e.g.: http://code.woboq.org/qt5/qtbase/src/widgets/widgets/qdockwidget.cpp.html

There is also a QDockWidget::setTitleWidget() method.

Just to be sure: you want to trigger the drag when a mouse button is pressed while the LMB is already down, right?

Cheers,
_