I'm developing a program that is based in 2 classes, Custom Label(responsible to emit a signal when a label is hovered, showing its position.It also changes the cursor when a label is clicked) and Widget(responsible to drag and drop functions, besides the ability to add a label when i click in a place on the screen).
When i execute the program, the drag and drop function is disabled. I think that what is causing the problem is the fact that there is the same function (mousePressEvent) in 2 classes at the same time.
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);
}
How can I use the functions at the same time?
How can I fix this?
In your CustomLabel::mousePressEvent(QMouseEvent *event)
method, you can reject the event.
It will be send to the parent and the CustomLabel::mousePressEvent(QMouseEvent *event)
will be called (see 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;
}
You will see in the output panel:
virtual void CustomLabel::mousePressEvent(QMouseEvent*)
virtual void Widget::mousePressEvent(QMouseEvent*)
Do the same thing for the other events you want to broadcast to the parent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.