【问题标题】:Qt drag & drop button; drop not detectingQt拖放按钮;跌落未检测
【发布时间】:2012-12-18 11:13:20
【问题描述】:

我正在 QT 中创建一个 2D 游戏,我正在尝试在我的程序中实现拖放操作。 由于某种原因,drop 没有注册:qDebug 应该在 drop 时打印一条消息,但这不会发生。

#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() 没有问题我只是用它来测试它们我在拖动事件中..我不是

【问题讨论】:

  • 您在移动拖动的项目数量时是否看到其他调试?
  • 不,但我可以移动对象并出现放置光标
  • 你是否使用目标选择器来选择调试模式?
  • makeDrag() 是否定义为头文件中的槽? public slot: void makeDrag(void);

标签: c++ qt drag-and-drop


【解决方案1】:

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

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

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

【讨论】:

  • 是一个错误但仍然没有改变;我的鼠标释放和按下事件正在工作;但拖放事件不是
  • 好的,你的mousePress/ReleaseEvents 可以正常工作。但是,drop 事件是由QGraphicsItem::mouseReleaseEvent 调度的,所以我认为这是问题的一部分。我再看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多