QT5 实现鼠标拖拽事件

先给最终效果:


简单的来说就是重写了一下鼠标事件:

void camdWidget::dragEnterEvent(QDragEnterEvent *event)
{
    toolbox* source = qobject_cast(
                event->source());
    if(source != NULL)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
    camdWidget* source1 = qobject_cast(
                event->source());
    if(source1 != NULL)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }

}

void camdWidget::dragMoveEvent(QDragMoveEvent *event)
{
    toolbox* source = qobject_cast(
                event->source());
    if(source != NULL)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
    camdWidget* source1 = qobject_cast(
                event->source());
    if(source1 != NULL)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
}

void camdWidget::dropEvent(QDropEvent *event)
{
    toolbox* source = qobject_cast(
                event->source());
    if(source != NULL)
    {
        const QMimeData* data = event->mimeData();
        QString text = data->text();
        addUnit(text);
        event->setDropAction(Qt::MoveAction);
        event->accept();
        return;
    }

    camdWidget* source1 = qobject_cast(
                event->source());
    if(source1 != NULL)
    {
        const QMimeData* data = event->mimeData();
        QString index = data->text();

        if(!r->is_item_recy(index.toInt())) return;
        event->setDropAction(Qt::MoveAction);
        event->accept();
        if(lastPoints.length()<1) return;
        ptr_P end = new P();
        end->p = QPoint(event->pos());
        end->num = (end->p.y()-10)/40;
        endPoints.append(end);
        qDebug()<<"[99]last length"<num<num;
        r->set_re(tmp->num, end->num);
        update();
    }
}

void camdWidget::mousePressEvent(QMouseEvent *event){
    unit *child = static_cast(childAt(event->pos()));
//    qDebug()<< child->objectName();
    if (!child) return;
    QStringList text_index = child->objectName().split('_');
//    qDebug()<<"press:"<is_item_recy(text_index.back().toInt());
    if(r->is_item_recy(text_index.back().toInt())){
        beginPos = event->pos();
        ptr_P last = new P();
        last->p = QPoint(event->pos());
        last->num = (last->p.y()-10)/40;
        append_lastPoints(last);
        qDebug()<<"[169]last length"<(childAt(beginPos));
    if(!child) return;
    QString childName = child->objectName();

    if(e->buttons() & Qt::LeftButton)
    {
        QStringList text_index = childName.split('_');
        QString index = text_index.back();
//        qDebug()<< child->objectName()<is_item_recy(index.toInt());
        if(r->is_item_recy(index.toInt())){
            if((e->pos() - beginPos).manhattanLength() >= QApplication::startDragDistance()){
                QMimeData *mimeData = new QMimeData;
                mimeData->setText(index);
                QDrag *drag = new QDrag(this);
                drag->setMimeData(mimeData);
                drag->exec(Qt::MoveAction);
            }//if
        }
    }//if
}

void camdWidget::paintEvent(QPaintEvent *)
{
    QPainter pp(this);
    QPen pen;                                 //创建一个画笔
    pen.setColor(Qt::yellow);
    pen.setWidth(2);
    pp.setPen(pen);

    for(int i = 0; ip+QPoint(70,0);
        QPoint y_extension = QPoint(extension.x(),endPoints[i]->p.y());
        pp.drawLine(lastPoints[i]->p, extension);
        pp.drawLine(extension, y_extension);
        pp.drawLine(endPoints[i]->p, y_extension);
    }
}

整个效果就是:

选择模式界面:

QT5 实现鼠标拖拽事件_第1张图片

循环判断界面:

QT5 实现鼠标拖拽事件_第2张图片

QT5 实现鼠标拖拽事件_第3张图片

条件判断界面:

QT5 实现鼠标拖拽事件_第4张图片

QT5 实现鼠标拖拽事件_第5张图片

本项目的(源代码+UI+mac 安装包+ 文档)的下载地址:https://download.csdn.net/download/iwanaabigdrumstick/10541943



你可能感兴趣的:(QT5,c++从入门到放弃)