PDA

View Full Version : Force dragging a QDockWidget



Kryzon
7th January 2015, 11:51
Hello.
I'm trying to force a floating dock widget to enter 'mouse drag mode,' when you drag a widget by the title bar of the window.

I've looked at the source for QDockWidget ("qdockwidget.cpp") and identified the conditions necessary for it to start the dragging mode: https://qt.gitorious.org/qt/qtbase/source/3401e91be078b54c8a385757c42da437c24bfc90:src/widgets/widgets/qdockwidget.cpp#L916
It catches a QEvent::NonClientAreaMousePressEvent event, and if it's inside the title area the floating dock widget 'dragging mode' is started.

I'm using the following to synthesize the event. "DetachableDock" inherits QDockWidget:


void DetachableDock::requestGrab()
{
// Center the dock widget window on the cursor.

int centerTitleX = frameGeometry().width() / 2;
int centerTitleY = ( frameGeometry().height() - geometry().height() ) / 2;

QPoint centerTitlePos = QCursor::pos() - QPoint( centerTitleX, centerTitleY );
move( centerTitlePos );

// Synthesize the mouse press event.

QMouseEvent grabEvent( QEvent::NonClientAreaMouseButtonPress, QPoint( centerTitleX, -centerTitleY ), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
QApplication::sendEvent( this, &grabEvent );

qDebug() << "EVENT SENT";
}

This is called when I click on a button. Before calling this I synthesize a "mouse release" event for the button. The window remains unselected and undragabble. If I move the cursor it doesn't drag it.
I've subclassed QDockWidget with this "DetachableDock" class. I reimplemented the QWidget::event( QEvent* event ) function and I can certify that this NonClientAreaMousePressEvent is being received by the widget.

Added after 54 minutes:

Here's the project folder.
I appreciate if someone can tell me what it's missing to be able to fully enter that dock widget dragging mode with only synthesized events.