Qt 自定义移动PushButton

/************************************************
 * Class: MoveButton
 *
************************************************/
class MoveButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MoveButton(QWidget *parent = nullptr);

signals:
public slots:

public :

private:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

public:

    QPoint m_stLastPoint;

};
/************************************************
 * Class: MoveButton
 *
************************************************/
MoveButton::MoveButton(QWidget *parent) : QPushButton(parent)
{
    m_stLastPoint = this->pos();
}

void MoveButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
//      m_stLastPoint = event->globalPos() - pos();
        m_stLastPoint.setY(event->globalY() - y());
        event->accept();
    }
}

void MoveButton::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
//      move(event->globalPos() - m_stLastPoint());
        move(m_stLastPoint.x(), event->globalY() - m_stLastPoint.y());
        event->accept();

    }

}

你可能感兴趣的:(Qt 自定义移动PushButton)