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.
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.