Handle rejected dropEvent



  • I'm implementing drag and drop on a QTableWidget and I want to prevent the user from dropping on some cells (depending on various factors). I can only detect if the drop will be allowed when the user tries to drop on a cartain cell. Then I perform some tests and ignore or accept the event in dropAction(QDropEvent *event).
    Basically, my problem is that I can't detect whether the dropAction was ignored from within the startDrag function.
    I'm really stranged, since I've been developping this on Ubuntu and it worked perfectly. The problem appaired when I tryed to build on Windows.

    Briefly, theese are the functions I have:

    @void TablaCentral::startDrag()
    {
    QTableWidgetItem item = currentItem();
    if (/
    item gathers some conditions */){
    QMimeData *mimeData = new QMimeData;
    ...
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    ...
    if(drag->exec() == Qt::MoveAction){ // <-- <-- <-- I'm stuck here

               // it alywas enters here, don't know how to 
               // recongize an ignored dropEvent
    
        } else {
    
             // never enters here!!
             // I supposed it should enter here if I ignore on dropEvent(...&#41;
    
        }
    }
    

    }

    void TablaCentral::dragEnterEvent(QDragEnterEvent *event)
    {
    if (event->source() == this){
    event->setDropAction(Qt::MoveAction);
    event->accept();
    }
    }

    void TablaCentral::dragMoveEvent(QDragMoveEvent *event)
    {
    if(event->source() == this){
    event->setDropAction(Qt::MoveAction);
    event->accept();
    }
    }

    void TablaCentral::dropEvent(QDropEvent *event)
    {
    if(event->source() == this){
    QTableWidgetItem targetItem = itemAt(event->pos());
    if ( /
    targetItem doesn't gather some conditions */ ){
    event->ignore();
    return;
    }
    if (/some other conditions/){
    event->ignore();
    return;
    } else {
    // do something with the data
    event->accept();
    // this is the only case when I accept
    }
    }@



  • Found it on "this document.":http://qt-project.org/doc/qt-4.8/dnd.html Apparently, event->ignore() wasn't enough. I replaced it every time on dropEvent(QDropEvent *event) whith this:

    @
    event->setDropAction(Qt::IgnoreAction);
    event->ignore();
    @

    Still it's odd to me how the problem didn't show up on Linux...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.