QT實現可移動和改變尺寸的無邊框窗口

實現QT無邊框窗口的拖動,尺寸改變。須要的時候從該類繼承,派生類須要定義爲QWidget。spa

頭文件:code

#ifndef SIZEABLEWIDGET_H
#define SIZEABLEWIDGET_H

#include <QWidget>

class SizeableWidget : public QWidget
{
    Q_OBJECT
public:
    SizeableWidget(QWidget *parent = 0);

    //容許或禁止移動
    bool isMoveable()const;
    void setMoveable(bool moveable);

    //容許或禁止改變大小
    bool isSizeable()const;
    void setSizeable(bool sizeable);

private:
    void mousePressEvent (QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    int calcPosition(const QPoint& pt);
    void setCursorType(int value);

private:
    bool    m_bMoveable;
    bool    m_bSizeable;
    bool    m_bLeftMouseButtonPressed;
    int     m_lastPosition;
    QPoint  m_ptLast;
    Qt::CursorShape m_currentCursor;
};

#endif // SIZEABLEWIDGET_H

源文件:繼承

#include "sizeablewidget.h"
#include <QPainter>
#include <QMouseEvent>

namespace
{
    const int g_padding = 4;

    int helperCalcPosition(int pos, int range)
    {
        return (pos < g_padding) ? 0 : ((pos > (range - g_padding)) ? 2 : 1);
    }
}

SizeableWidget::SizeableWidget(QWidget *parent) :
    QWidget(parent) ,
    m_bMoveable(true) ,
    m_bSizeable(true) ,
    m_bLeftMouseButtonPressed(false) ,
    m_lastPosition(11) ,
    m_currentCursor(Qt::ArrowCursor)
{
    setCursor(m_currentCursor);
    setMinimumSize(QSize(32, 32));
}

bool SizeableWidget::isMoveable()const
{
    return m_bMoveable;
}

void SizeableWidget::setMoveable(bool moveable)
{
    m_bMoveable = moveable;
}

bool SizeableWidget::isSizeable()const
{
    return m_bSizeable;
}

void SizeableWidget::setSizeable(bool sizeable)
{
    m_bSizeable = sizeable;
}

int SizeableWidget::calcPosition(const QPoint& pt)
{
    int row = helperCalcPosition(pt.y(), rect().height());
    int col = helperCalcPosition(pt.x(), rect().width());
    return row * 10 + col;
}

void SizeableWidget::mousePressEvent(QMouseEvent* event)
{
    m_bLeftMouseButtonPressed = true;
    m_ptLast = event->globalPos();
    m_lastPosition = calcPosition(event->pos());
    QWidget::mousePressEvent(event);
}

void SizeableWidget::mouseReleaseEvent(QMouseEvent* event)
{
    m_bLeftMouseButtonPressed = false;
    QWidget::mouseReleaseEvent(event);
}

void SizeableWidget::mouseMoveEvent(QMouseEvent* event)
{
    if(m_bLeftMouseButtonPressed)
    {
        QPoint ptNew = event->globalPos();
        ptNew -= m_ptLast;
        if(11 == m_lastPosition)    //拖動
        {
            if (m_bMoveable)
            {
                ptNew += pos();
                move(ptNew);
            }
        }
        else    //調整大小
        {
            if (m_bSizeable)
            {
                QRect rectWindow = geometry();
                switch(m_lastPosition)
                {
                case 00:
                    rectWindow.setTopLeft(rectWindow.topLeft() + ptNew);
                    break;
                case 02:
                    rectWindow.setTopRight(rectWindow.topRight() + ptNew);
                    break;
                case 20:
                    rectWindow.setBottomLeft(rectWindow.bottomLeft() + ptNew);
                    break;
                case 22:
                    rectWindow.setBottomRight(rectWindow.bottomRight() + ptNew);
                    break;
                case 10:
                    rectWindow.setLeft(rectWindow.left() + ptNew.x());
                    break;
                case 12:
                    rectWindow.setRight(rectWindow.right() + ptNew.x());
                    break;
                case 01:
                    rectWindow.setTop(rectWindow.top() + ptNew.y());
                    break;
                case 21:
                    rectWindow.setBottom(rectWindow.bottom() + ptNew.y());
                    break;
                default:
                    Q_ASSERT(0);
                }
                setGeometry(rectWindow);
            }
        }
        m_ptLast  = event->globalPos();
    }
    else
    {
        if (m_bSizeable)
            setCursorType(calcPosition(event->pos()));
    }
    QWidget::mouseMoveEvent(event);
}

void SizeableWidget::setCursorType(int value)
{
    Qt::CursorShape cursor;

    switch(value)
    {
    case 00:
    case 22:
        cursor = Qt::SizeFDiagCursor;
        break;
    case 02:
    case 20:
        cursor = Qt::SizeBDiagCursor;
        break;
    case 10:
    case 12:
        cursor = Qt::SizeHorCursor;
        break;
    case 01:
    case 21:
        cursor = Qt::SizeVerCursor;
        break;
    case 11:
        cursor = Qt::ArrowCursor;
        break;
    default:
        Q_ASSERT(0);
        break;
    }
    if(cursor != m_currentCursor)
    {
        m_currentCursor = cursor;
        setCursor(m_currentCursor);
    }
}
相關文章
相關標籤/搜索