Qt drag & amp; кнопка сброса; падение не обнаруживает

Я создаю 2D-игру в QT, и я пытаюсь реализовать перетаскивание & загляните в мою программу.
По какой-то причине удаление не зарегистрировано: qDebug должен напечатать сообщение об удалении, но этого не происходит.

#include "dialog.h"#include "ui_dialog.h"#include "world.h"#include <vector>

Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);

scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);

MySquare *item;
QGraphicsRectItem *enemyItem;
World *myWorld = new World();
std::vector<Tile*> tiles = myWorld->createWorld(":/texture.jpg");

int count = 0;
foreach (Tile *tile, tiles){
count++;
item = new MySquare(tile->getXPos()*4,tile->getYPos()*4,4,4);
item->setBrush(QColor(tile->getValue()*255,tile->getValue()*255,tile->getValue()*255));
item->setAcceptDrops(true);
scene->addItem(item);
}player = new MySquare(10,20,10,10);
player->setAcceptDrops(true);
scene->addItem(player);

//drag & drop part
QPushButton *pushButton = new QPushButton("Click Me",this);
connect(pushButton,SIGNAL(pressed()),this,SLOT(makeDrag()));
setAcceptDrops(true);
}

void Dialog::makeDrag()
{
QDrag *dr = new QDrag(this);
// The data to be transferred by the drag and drop operation is contained in a QMimeData object
QMimeData *data = new QMimeData;
data->setText("This is a test");
// Assign ownership of the QMimeData object to the QDrag object.
dr->setMimeData(data);
// Start the drag and drop operation
dr->start();
}

mysquare.cpp

#include "mysquare.h"MySquare::MySquare(int _x,int _y, int _w, int _h)
{
isPlayer=false;
Pressed=false;
setFlag(ItemIsMovable);
setFlag(ItemIsFocusable);
setAcceptDrops(true);

color=Qt::red;
color_pressed = Qt::green;

x = _x;
y = _y;
w = _w;
h = _h;
}

QRectF MySquare::boundingRect() const
{
return QRectF(x,y,w,h);
}

void MySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = boundingRect();
QBrush brush(color);

if (Pressed){
brush.setColor(color);

} else {
brush.setColor(color_pressed);
}

painter->fillRect(rec,brush);
painter->drawRect(rec);
}void MySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Pressed=true;
update();
QGraphicsItem::mousePressEvent(event);
qDebug() << "mouse Pressed";
}

void MySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Pressed=false;
update();
QGraphicsItem::mousePressEvent(event);
qDebug() << "mouse Released";
}void MySquare::keyPressEvent(QKeyEvent *event){
int x = pos().x();
int y = pos().y();

//key handling

QGraphicsItem::keyPressEvent(event);

}

void MySquare::dropEvent(QDropEvent *event)
{
qDebug("dropEvent - square");
// Unpack dropped data and handle it the way you want
qDebug("Contents: %s", event->mimeData()->text().toLatin1().data());
}

void MySquare::dragMoveEvent(QDragMoveEvent *event){
qDebug("dragMoveEvent - square ");
event->accept();
}

void MySquare::dragEnterEvent(QDragEnterEvent *event){
event->setAccepted(true);
qDebug("dragEnterEvent - square");
event->acceptProposedAction();
}

void MySquare::setBrush(QColor _color){
color = _color;
color_pressed = _color;
update(); //repaint
}

введите описание изображения здесь

редактировать; нет никаких проблем с qDebug (), я просто использую его для проверки их, я нахожусь внутри событий перетаскивания .. которые я не

1

Решение

В вашем mouseReleaseEventпереходите к QGraphicsItem::mousePressEvent вместо QGraphicsItem::mouseReleaseEvent

Изменить: я не знаю, если это имеет значение, но инициализировать QGraphicsItem в вашем конструкторе

MySquare::MySquare(int _x,int _y, int _w, int _h) : QGraphicsItem()
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector