如何在QT的同时在2个不同的类中使用mousePressEvent

时间:2019-04-08 19:21:38

标签: c++ qt qt5

我正在开发一个基于2类的程序,“自定义标签”(自定义标签(悬停标签时负责发出信号,显示其位置,单击标签时还改变光标))和“小部件”(负责拖放功能,以及单击屏幕上的位置时添加标签的功能。

当我执行程序时,拖放功能被禁用。我认为导致问题的原因是,在两个类中同时存在相同的函数(mousePressEvent)。

customlabel.cpp

#include "customlabel.h"
#include <QMouseEvent>

CustomLabel::CustomLabel(QWidget *parent):QLabel(parent)
{
    this->setMouseTracking(true);
    this->setCursor(Qt::OpenHandCursor);
}

void CustomLabel::mouseMoveEvent(QMouseEvent *event)
{
   QPoint mouse_pos=event->pos();


    if(mouse_pos.x()<=this->size().width() && mouse_pos.y()<=this->size().height()){

    if(mouse_pos.x()>0 && mouse_pos.y()>0){
          emit sendMousePosition(this->pos());
       }
    }
}

void CustomLabel::mousePressEvent(QMouseEvent *event)
{
   this->setCursor(Qt::ClosedHandCursor);
}

void CustomLabel::mouseReleaseEvent(QMouseEvent *event){
    this->setCursor(Qt::OpenHandCursor);
}

widget.cpp

void Widget::showMousePosition(const QPoint &pos)
{

    ui->label_show->setText("x"+QString::number(pos.x())+"y"+QString::number(pos.y()));
}


void Widget::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
        if (event->source() == this) {
            event->setDropAction(Qt::MoveAction);
            event->accept();
        } else {
            event->acceptProposedAction();
        }
    } else {
        event->ignore();
    }
}

void Widget::dragMoveEvent(QDragMoveEvent *event)
{
    if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
        if (event->source() == this) {
            event->setDropAction(Qt::MoveAction);
            event->accept();
        } else {
            event->acceptProposedAction();
        }
    } else {
        event->ignore();
    }
}

void Widget::dropEvent(QDropEvent *event)
{
    if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
        QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
        QDataStream dataStream(&itemData, QIODevice::ReadOnly);

        QPixmap pixmap;
        QPoint offset;
        dataStream >> pixmap >> offset;

        CustomLabel *newIcon = new CustomLabel(this);
        newIcon->setPixmap(pixmap);
        newIcon->move(event->pos() - offset);
        newIcon->show();
        newIcon->setAttribute(Qt::WA_DeleteOnClose);
        connect(newIcon, &CustomLabel::sendMousePosition, this, &Widget::showMousePosition);

        if (event->source() == this) {
            event->setDropAction(Qt::MoveAction);
            event->accept();
        } else {
            event->acceptProposedAction();
        }
    } else {
        event->ignore();
    }
}


void Widget::mousePressEvent(QMouseEvent *event)
{


        if(event->button()==Qt::LeftButton){
            CustomLabel *child = static_cast<CustomLabel*>(childAt(event->pos()));

            if (!child)
                return;

            QPixmap pixmap = *child->pixmap();

            QByteArray itemData;
            QDataStream dataStream(&itemData, QIODevice::WriteOnly);
            dataStream << pixmap << QPoint(event->pos() - child->pos());

            QMimeData *mimeData = new QMimeData;
            mimeData->setData("application/x-dnditemdata", itemData);

            QDrag *drag = new QDrag(this);
            drag->setMimeData(mimeData);
            drag->setPixmap(pixmap);
            drag->setHotSpot(event->pos() - child->pos());

            QPixmap tempPixmap = pixmap;
            QPainter painter;
            painter.begin(&tempPixmap);
            painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
            painter.end();

            child->setPixmap(tempPixmap);

            if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
                child->close();
                connect(child, &CustomLabel::sendMousePosition, this, &Widget::showMousePosition);
            } else {
                child->show();
                child->setPixmap(pixmap);
                connect(child, &CustomLabel::sendMousePosition, this, &Widget::showMousePosition);
            }
        }
        else if(event->button()==Qt::RightButton)
        {

            CustomLabel *child = new CustomLabel(this);
            child->setPixmap(QPixmap("C:/Users/ILHA4/Desktop/paint5/trafo_png.png"));
            child->move(event->x(),event->y());
            child->show();
            connect(child, &CustomLabel::sendMousePosition, this, &Widget::showMousePosition);

        }

如何同时使用这些功能?

我该如何解决?

1 个答案:

答案 0 :(得分:0)

在您的CustomLabel::mousePressEvent(QMouseEvent *event)方法中,您可以拒绝该事件。

它将发送给父级,并且将调用CustomLabel::mousePressEvent(QMouseEvent *event)(请参阅Qt documentation)。

void CustomLabel::mousePressEvent(QMouseEvent *event)
{
    qDebug() << Q_FUNC_INFO;
    this->setCursor(Qt::ClosedHandCursor);
    event->ignore();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    qDebug() << Q_FUNC_INFO;
}

您将在输出面板中看到:

virtual void CustomLabel::mousePressEvent(QMouseEvent*)
virtual void Widget::mousePressEvent(QMouseEvent*)

对要广播给父母的其他事件执行相同的操作。