使用QT进行简单的控件拖拽

  • Post author:
  • Post category:其他



QT中进行控件的拖拽,需要定义一个继承该控件的类,然后在类中重写事件函数,如mousePressEvent和mouseMoveEvent,然后在需要使用的地方进行调用即可



QPushbutton的简单拖拽

#include <QPushButton>
#include <QWidget>
#include <QPoint>
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>
#include <QApplication>

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

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

private:
    QPoint mDragPos;

};
#include "custombutton.h"

CustomButton::CustomButton(QWidget *parent) :
    QPushButton(parent)
{
}

void CustomButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
//        mDragPos = event->globalPos() - frameGeometry().topLeft();
        mDragPos = event->globalPos() - pos();
    }
//    QPushButton::mousePressEvent(event);
}

void CustomButton::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - mDragPos);
    }
//    QPushButton::mouseMoveEvent(event);
}

主函数中进行调用

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    CustomButton *button = new CustomButton(this);
    button->move(50,50);
    button->setText("Drag me");
}



QLabel的简单拖拽

QLabel的拖拽移动类似QPushbutton

#ifndef DRAGLABEL_H
#define DRAGLABEL_H

#include <QPushButton>
#include <QWidget>
#include <QPoint>
#include <QEvent>
#include <QMoveEvent>
#include <QLabel>
#include <QDrag>
#include <QApplication>
#include <QMimeData>

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

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

private:
    QPoint mDragPos;

};

#endif // DRAGLABEL_H

在这里插入代码片
#include "draglabel.h"

DragLabel::DragLabel(QWidget *parent) :
    QLabel(parent)
{
}

void DragLabel::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
//                mDragPos = event->pos();
                mDragPos = event->globalPos() - frameGeometry().topLeft();
    }
//        mDragPos = event->globalPos() - frameGeometry().topLeft();
//        event->accept();
//    QPushButton::mousePressEvent(event);
}

void DragLabel::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - mDragPos);
//        int distance = (event->pos() - mDragPos).manhattanLength();
//        qDebug()<<distance<<QApplication::startDragDistance();
//        if(distance >= QApplication::startDragDistance())
//        {
//            QDrag *drag = new QDrag(this);
//            QMimeData *mime = new QMimeData;
//            mime->setText(text());
//            drag->setMimeData(mime);
//            drag->exec(Qt::CopyAction | Qt::MoveAction);
//        }
    }
//    QPushButton::mouseMoveEvent(event);
}

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    DragLabel *label = new DragLabel(this);
    label->move(20,20);
    label->setText("Drag me");
}



版权声明:本文为weixin_43699794原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。