我正在开发一个基于2个类的程序,自定义标签(负责在标签悬停时发出信号,显示其位置。单击标签时,它还会更改光标(和小部件(负责拖放功能,除了在单击屏幕上的位置时添加标签的功能(。
当我执行程序时,拖放功能被禁用。我认为导致问题的原因是同时在 2 个类中存在相同的函数(mousePressEvent(。
自定义标签.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);
}
小部件.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);
}
如何同时使用这些功能?
我该如何解决这个问题?
在CustomLabel::mousePressEvent(QMouseEvent *event)
方法中,您可以拒绝该事件。
它将被发送到父级,CustomLabel::mousePressEvent(QMouseEvent *event)
将被调用(参见Qt文档(。
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*)
对要广播给父级的其他事件执行相同的操作。