c++ - Qt拖放按钮;下降未检测

标签 c++ qt drag-and-drop

我正在 QT 中创建一个 2D 游戏,我正在尝试在我的程序中执行拖放操作。 由于某种原因,丢弃未注册: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
}

enter image description here

编辑; qDebug() 没有问题我只是用它来测试它们我在拖动事件中..我不是

最佳答案

在您的 mouseReleaseEvent 中,您传递给 QGraphicsItem::mousePressEvent 而不是 QGraphicsItem::mouseReleaseEvent

编辑:我不知道这是否重要,但在构造函数中初始化 QGraphicsItem

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

关于c++ - Qt拖放按钮;下降未检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13931705/

相关文章:

c++ - CMake 标志更改调试配置

c++ - 有没有办法在 Sming IDE (ESP8266) 中使用 std::map?

c++ - hello world 在 gcc 5.3.0/bin/ld : invalid option -- 'p' 上失败

wpf - 在其 winforms 父级中拖动托管 WPF 控件吞噬的事件

javascript - jQuery Draggable 只允许拖动 1 个图像

android - 通过拖放可重新排序的 ListView

c++ - boost 属性树 毫无意义的错误路径

c++ - Qt 图形被另一个遮挡,但仍然导致绘图更新

mysql - 如何使用 MSVC 和 QMake 设置 MySQL?

c++ - 在带有 CLion 的 Windows 中使用 CMake (mingw) 在 C++ 中未定义的 Qt5 引用