1. 程式人生 > >QWidget無邊框無標題欄窗體 可拖動可拉伸

QWidget無邊框無標題欄窗體 可拖動可拉伸

    通常我們不想使用QMainWindow提供的標題欄,或者是QWidget的標題欄、邊框,這時候我們使用了一個普通的QWidget,沒有標題欄沒有邊框,但我們又需要有標題欄拖動的功能,邊框改變大小的功能,看上去是個很矛盾的想法,所以...我們只能自己實現了。

    直接貼程式碼吧:

    CMainWindow.h

class CMainWindow : public QWidget
{
    Q_OBJECT

public:
    CMainWindow(QWidget *parent = 0);
    ~CMainWindow();

private:
    void initMainWindow();
    void initWidget();
    void assembleWidget();

    enum tagCursorCtrlStyle
    {
        eCursorNormal = 0,    // 普通滑鼠
        eCursorHor,           // 水平拉伸
        eCursorVer,           // 垂直拉伸
        eCursorHorVer         // 水平和垂直拉伸
    };

    // custom title bar
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    int setCursorStyle(const QPoint& curPoint);    // 當前位置設定滑鼠樣式

    QPoint m_MousePressPos;    // 滑鼠點選位置
    QPoint m_MouseMovePos;     // 滑鼠移動位置
    bool   m_bMousePressed;    // 滑鼠是否按下
    int    m_nMouseResize;     // 滑鼠設定大小
    bool   m_bMouseResizePressed;  // 設定大小的按下
    QPoint m_ResizePressPos;   // 設定大小滑鼠按下的點

    // title
    QWidget*         m_pTitleWidget;    // 標題欄
};

    CMainWindow.cpp

</pre><p><pre name="code" class="cpp">#include "MainWindow.h"

CMainWindow::CMainWindow(QWidget *parent)
    : QWidget(parent)
    , m_bMousePressed(false)
    , m_nMouseResize(eCursorNormal)
    , m_bMouseResizePressed(false)
    , m_pTitleWidget(NULL)
{
    initMainWindow();
    initWidget();
    assembleWidget();
}

CMainWindow::~CMainWindow()
{
}

void CMainWindow::initMainWindow()
{
    setWindowFlags(Qt::FramelessWindowHint);    // 設定視窗標誌
    setMinimumSize(600, 400);    // 設定最小尺寸
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);    // 設定尺寸屬性
    setMouseTracking(true);    // 介面拉伸需要這個屬性
}

void CMainWindow::initWidget()
{
    m_pTitleWidget = new QWidget(this);
}

void CMainWindow::assembleWidget()
{
    m_pTitleWidget->setFixedHeight(40);
    m_pTitleWidget->setMinimumWidth(600);

    m_pTitleWidget->move(0, 0);
}

void CMainWindow::mousePressEvent(QMouseEvent *event)
{
    if( event->button() == Qt::LeftButton &&
        m_pTitleWidget->rect().contains(event->globalPos() - this->frameGeometry().topLeft()))
    {
        m_MousePressPos = event->globalPos();
        m_bMousePressed = true;
    }

    if (eCursorNormal != m_nMouseResize)
    {
        m_bMouseResizePressed = true;
        m_ResizePressPos = event->pos();
    }
    event->ignore();    //表示繼續向下傳遞事件,其他的控制元件還可以去獲取
}

void CMainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if( event->button() == Qt::LeftButton )
    {
        m_bMousePressed = false;
        m_bMouseResizePressed = false;
        m_ResizePressPos.setX(0);
        m_ResizePressPos.setY(0);
        m_nMouseResize = setCursorStyle(event->pos());
    }
    event->ignore();
}

void CMainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if( m_bMousePressed )
    {
        m_MouseMovePos = event->globalPos();
        this->move( this->pos() + m_MouseMovePos - m_MousePressPos );
        m_MousePressPos = m_MouseMovePos;
        return;
    }

    QPoint curPoint = event->pos();

    if (!m_bMouseResizePressed)
    {
        m_nMouseResize = setCursorStyle(curPoint);
    }

    if (m_bMouseResizePressed && !m_ResizePressPos.isNull())
    {
        switch (m_nMouseResize)
        {
        case eCursorHor:
            this->resize(curPoint.x(), this->height());
            break;
        case eCursorVer:
            this->resize(this->width(), curPoint.y());
            break;
        case eCursorHorVer:
            this->resize(curPoint.x(), curPoint.y());
            break;
        default:
            break;
        }
    }

    event->ignore();
}

int CMainWindow::setCursorStyle(const QPoint& curPoint)
{
    int nCurWidth = this->width();
    int nCurHeight = this->height();
    int nRes = eCursorNormal;

    if ( (nCurWidth - curPoint.x() <= 3) && (nCurHeight - curPoint.y() <= 3) )
    {
        setCursor(Qt::SizeFDiagCursor);
        nRes = eCursorHorVer;
    }
    else if (nCurWidth - curPoint.x() <= 3)
    {
        setCursor(Qt::SizeHorCursor);
        nRes = eCursorHor;
    }
    else if (nCurHeight - curPoint.y() <= 3)
    {
        setCursor(Qt::SizeVerCursor);
        nRes = eCursorVer;
    } 
    else
    {
        setCursor(Qt::ArrowCursor);
        nRes = eCursorNormal;
    }

    return nRes;
}

    原本想百度一個,後來實在找不到合適的,就自己隨便寫了一個,如有更簡單的方法,希望指出,大家一起討論學習。