2012-12-18 126 views
1

我正在QT中創建一個2D遊戲,我試圖實現一個拖動到我的程序中的&。 由於某些原因,丟棄未註冊:qDebug應在丟棄時打印消息,但這不會發生。Qt拖放按鈕;滴沒有檢測

#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

編輯;有我只是用它來測試他們,我events..which我不是

+0

您是否看到其他調試,而您正在移動拖動的項目數量? – cmannett85

+0

不,但我可以移動對象,並出現光標移動 – Thomas

+0

您是否使用目標slector選擇調試模式? – prehistoricpenguin

回答

1

在你mouseReleaseEvent拖內)與qDebug沒有問題(中,傳遞給QGraphicsItem::mousePressEvent而不是QGraphicsItem::mouseReleaseEvent

編輯:我不知道這是否重要,但初始化您的構造函數中的QGraphicsItem

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

是一個錯誤,但仍然沒有改變;我的鼠標釋放和新聞事件正在運行;但拖放事件不是 – Thomas

+0

好的,是的,你的'mousePress/ReleaseEvents'可以正常工作。然而,drop事件由'QGraphicsItem :: mouseReleaseEvent'分派,所以我認爲這是問題的一部分。我會再看看。 – Constantin