Qt子控件拖动窗口

背景

写一个移动窗口demo,对大家来说应该so easy。于是各位有没有碰到这种情况:在窗口上有不同的控件,去拖动控件却不能让窗口跟着移动;只有拖动除控件的其他位置才能拖动窗口。

这里作者实现了此功能并开源,虽说需求很少 但指引思想可揣摩。

效果

Qt子控件拖动窗口_第1张图片

源码

class PushButton : public QPushButton
{
    Q_OBJECT
public:
    PushButton(QWidget *aa = 0);

    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);

signals:
    void ActionCurrentPos(const bool &,const QPoint &point = QPoint(0,0));
};

Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);

    cursorCurrentPointF = QPoint(0,0);
    cursouPresss = actionActive = false;
    connect(ui->pushButton,SIGNAL(ActionCurrentPos(const bool &,const QPoint &)),
        this,SLOT(ActionCurrentPos(const bool &,const QPoint &)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::ActionCurrentPos(const bool &b, const QPoint &point)
{
    if(b)
    {
        this->move(this->pos() + (this->cursor().pos() - cursorCurrentPointF));
        cursorCurrentPointF = this->cursor().pos();
    }
    else
    {
        cursorCurrentPointF = point;
    }
    update();
}

void Widget::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton && !cursouPresss)
    {
        cursorCurrentPointF = this->cursor().pos();
        cursouPresss = true;
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
    e->button() == Qt::LeftButton ? cursouPresss ? cursouPresss = false : 0 : 0;
}

void Widget::mouseMoveEvent(QMouseEvent *)
{
    if(cursouPresss)
    {
        this->move(this->pos() + (this->cursor().pos() - cursorCurrentPointF));
        cursorCurrentPointF = this->cursor().pos();
    }
    update();
}

// PushButton.cpp:
PushButton::PushButton(QWidget *aa) : QPushButton(aa)
{

}

void PushButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton) 
        emit ActionCurrentPos(false,this->cursor().pos());
}

void PushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton) emit ActionCurrentPos(false,QPoint(0,0));
}

void PushButton::mouseMoveEvent(QMouseEvent *)
{
    emit ActionCurrentPos(true,this->cursor().pos());
}

//widget.cpp:
Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);

    cursorCurrentPointF = QPoint(0,0);
    cursouPresss = actionActive = false;
    connect(ui->pushButton,SIGNAL(ActionCurrentPos(const bool &,const QPoint &)),
        this,SLOT(ActionCurrentPos(const bool &,const QPoint &)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::ActionCurrentPos(const bool &b, const QPoint &point)
{
    if(b)
    {
        this->move(this->pos() + (this->cursor().pos() - cursorCurrentPointF));
        cursorCurrentPointF = this->cursor().pos();
    }
    else
    {
        cursorCurrentPointF = point;
    }
    update();
}

void Widget::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton && !cursouPresss)
    {
        cursorCurrentPointF = this->cursor().pos();
        cursouPresss = true;
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
    e->button() == Qt::LeftButton ? cursouPresss ? cursouPresss = false : 0 : 0;
}

void Widget::mouseMoveEvent(QMouseEvent *)
{
    if(cursouPresss)
    {
        this->move(this->pos() + (this->cursor().pos() - cursorCurrentPointF));
        cursorCurrentPointF = this->cursor().pos();
    }
    update();
}
  • 源码链接稍后上传、或查看下面公众号获取。
关注

微信公众号搜索"Qt_io_"或"Qt开发者中心"了解更多关于Qt、C++开发知识.。

笔者 - jxd

你可能感兴趣的:(Qt,Qt,拖动窗口,子控件)