Qt无边框MainWindow如何拖动四周改变大小

原来还有winEvent(), x11Event() and macEvent() 这些东西。。。不过貌似还需要找更好的办法,否则就无法跨平台了。

你需要重新处理部分窗体事件,以下代码适用于Windows平台,仅供参考!

bool MainWindow::winEvent(MSG *msg, long *result)
{
    switch (msg->message)
    {
    case WM_NCHITTEST:
    {
        *result = 0;
        const LONG border_width = 8; //in pixels
        RECT winrect;
        GetWindowRect(winId(), &winrect);
        
        long x = GET_X_LPARAM(msg->lParam);
        long y = GET_Y_LPARAM(msg->lParam);
        
        bool resizeWidth = minimumWidth() != maximumWidth();
        bool resizeHeight = minimumHeight() != maximumHeight();
        if(resizeWidth)
        {
            //left border
            if (x >= winrect.left && x < winrect.left + border_width)
            {
                *result = HTLEFT;
            }
            //right border
            if (x < winrect.right && x >= winrect.right - border_width)
            {
                *result = HTRIGHT;
            }
        }
        if(resizeHeight)
        {
            //bottom border
            if (y < winrect.bottom && y >= winrect.bottom - border_width)
            {
                *result = HTBOTTOM;
            }
            //top border
            if (y >= winrect.top && y < winrect.top + border_width)
            {
                *result = HTTOP;
            }
        }
        if(resizeWidth && resizeHeight)
        {
            //bottom left corner
            if (x >= winrect.left && x < winrect.left + border_width &&
                    y < winrect.bottom && y >= winrect.bottom - border_width)
            {
                *result = HTBOTTOMLEFT;
            }
            //bottom right corner
            if (x < winrect.right && x >= winrect.right - border_width &&
                    y < winrect.bottom && y >= winrect.bottom - border_width)
            {
                *result = HTBOTTOMRIGHT;
            }
            //top left corner
            if (x >= winrect.left && x < winrect.left + border_width &&
                    y >= winrect.top && y < winrect.top + border_width)
            {
                *result = HTTOPLEFT;
            }
            //top right corner
            if (x < winrect.right && x >= winrect.right - border_width &&
                    y >= winrect.top && y < winrect.top + border_width)
            {
                *result = HTTOPRIGHT;
            }
        }
        
        if(*result == 0)
            return  QWidget::winEvent(msg,result);
        
        return true;
        break;
    } //end case WM_NCHITTEST
    default:
        return QWidget::winEvent(msg,result);
    }
}

参考:https://forum.qt.io/topic/29398/%E5%B7%B2%E8%A7%A3%E5%86%B3-qt%E6%97%A0%E8%BE%B9%E6%A1%86mainwindow%E5%A6%82%E4%BD%95%E6%8B%96%E5%8A%A8%E5%9B%9B%E5%91%A8%E6%94%B9%E5%8F%98%E5%A4%A7%E5%B0%8F/5

http://blog.csdn.net/kfbyj/article/details/9284923

原文地址:https://www.cnblogs.com/findumars/p/4700007.html