当前位置:   article > 正文

QWidget无边框无标题栏窗体 可拖动可拉伸_qeidget 无标题

qeidget 无标题

通常我们不想使用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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181

作者:永远的魔术1号
来源:CSDN
原文:https://blog.csdn.net/zhango5/article/details/52729866
版权声明:本文为博主原创文章,转载请附上博文链接!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/234891
推荐阅读
相关标签
  

闽ICP备14008679号