Vojislav Stojkovic
Vojislav Stojkovic

Reputation: 8153

Change mouse cursor when dragging a floating QDockWidget

Is it possible to use a different mouse cursor when dragging a floating QDockWidget? Neither QWidget::setCursor nor QApplication::setOverrideCursor have any effect.

Upvotes: 0

Views: 892

Answers (1)

minirop
minirop

Reputation: 326

A floating QDockWidget is a window, so you need to ask the OS to change the cursor when it's on the non-client area.

A little buggy example for windows:

#define WINVER 0x0500
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
bool DockWidget::winEvent(MSG * message, long * result)
{
    switch(message->message)
    {
        case WM_NCMOUSEMOVE:
            if(message->wParam == HTCAPTION)
            {
                qDebug() << "WM_NCMOUSEMOVE";
                if(!cursorHasBeenChanged && !cursorHasBeenClosed)
                {
                    cursorHasBeenChanged = true;
                    QApplication::setOverrideCursor(Qt::OpenHandCursor);
                }
            }
            else
                if(cursorHasBeenChanged)
                {
                    cursorHasBeenChanged = false;
                    QApplication::restoreOverrideCursor();
                }
            break;
        case WM_NCMOUSELEAVE:
            qDebug() << "WM_NCMOUSELEAVE";
            if(cursorHasBeenChanged && !cursorHasBeenClosed)
            {
                cursorHasBeenChanged = false;
                QApplication::restoreOverrideCursor();
            }
            break;
        case WM_NCLBUTTONDOWN:
            if(message->wParam == HTCAPTION)
            {
                qDebug() << "WM_NCLBUTTONDOWN";
                cursorHasBeenClosed = true;
                QApplication::setOverrideCursor(Qt::ClosedHandCursor);
            }
            break;
        case WM_NCLBUTTONUP:
            qDebug() << "WM_NCLBUTTONUP";
            if(cursorHasBeenClosed)
            {
                cursorHasBeenClosed = false;
                QApplication::restoreOverrideCursor();
            }
            break;
        default:
            ;
    }

    return QDockWidget::winEvent(message, result);
}

I think the code is self-explanatory, byut don't hesitate to ask if there something you don't understand.

The buggy part, is that I never receive WM_NCLBUTTONUP messages and I don't know why (I get WM_NCMOUSEMOVE instead) neither WM_NCMOUSEHOVER (which is the "enter event" for non-client area).

Upvotes: 1

Related Questions