QGroupBox dropEvent never called



  • Hey guys,

    I try to drop a widget on a QGroupBox. For that purpose, I installed a event filter in the containing widget:

    QGroupBox *gTemp = new QGroupBox("shift" + QString::number(i + 1), this);
            gTemp->setFlat(true);
            gTemp->setAcceptDrops(true);
            gTemp->installEventFilter(this);
    

    The code of the event filter is as follows:

    bool Fachbereich::eventFilter(QObject *object, QEvent *event)
    {
        qDebug() << "Fachbereich::eventFilter(" << object << ", " << event << ")";
    
        if (event->type() == QEvent::Drop)
        {
            QGroupBox *gTemp = qobject_cast<QGroupBox*>(object);
            qDebug() << gTemp->title();
        }
    
        return QObject::eventFilter(object, event);
    }
    
    

    The QGroupBox's DragEnterEvent works, as the output of qDebug() shows:

    Fachbereich::eventFilter( QGroupBox(0x3335a8e0) ,  QDragEnterEvent(dropAction=MoveAction, proposedAction=MoveAction, possibleActions=MoveAction, posF=7,53, answerRect=QRect(7,53 1x1), formats=("text/plain"), LeftButton )
    

    The QDropEvent on the other hand is never called. Do you have any suggestions?

    Thanks
    Sebastian



  • Ok, solved the "problem". I just derived my own class of QGroupBox. Here is the code, just in case it can help somebody facing the same ideas.

    dropablegroupbox.h

    #ifndef DROPABLEGROUBOX_H
    #define DROPABLEGROUBOX_H
    
    #include <QGroupBox>
    
    class DropableGrouBox : public QGroupBox
    {
    public:
        DropableGrouBox(const QString &title);
    
    private:
        void dragEnterEvent(QDragEnterEvent *event);
        void dragMoveEvent(QDragMoveEvent *event);
        void dropEvent(QDropEvent *event);
    };
    
    #endif // DROPABLEGROUBOX_H
    

    dropablegroupbox.cpp

    #include "dropablegroubox.h"
    #include <QDebug>
    #include <QDragEnterEvent>
    #include <QDropEvent>
    #include <QMimeData>
    
    DropableGrouBox::DropableGrouBox(const QString &title)
    {
        QGroupBox::setTitle(title);
        setAcceptDrops(true);
    }
    
    void DropableGrouBox::dragEnterEvent(QDragEnterEvent *event)
    {
        qDebug() << "DropableGrouBox::dragEnterEvent(" << event << ")";
    
        if (event->mimeData()->hasFormat("text/plain"))
        {
                event->acceptProposedAction();
        }
    }
    
    void DropableGrouBox::dragMoveEvent(QDragMoveEvent *event)
    {
        qDebug() << "DropableGrouBox::dragMoveEvent(" << event << ")";
    
        if (event->mimeData()->hasFormat("text/plain"))
        {
                event->acceptProposedAction();
        }
    }
    
    void DropableGrouBox::dropEvent(QDropEvent *event)
    {
        qDebug() << "DropableGrouBox::dropEvent(" << event << ")";
    
        QString ID = event->mimeData()->text();
    
        if (ID.toInt() != 0)
        {
            event->acceptProposedAction();
        }
    }
    

    In the code:

    DropableGrouBox *gTemp = new DropableGrouBox("Schicht " + QString::number(i + 1));
            gTemp->setFlat(true);
            gTemp->setAcceptDrops(true);
    

    Best regards
    Sebastian
    https://www.five-s.de


Log in to reply
 

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