Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how do i pervent QTableWidget cells from having an item dropped on them?



  • Hello everyone, noobie here.

    i am making a program where i am using the QtableWidget's internal drag and drop functionality to drag certain items from one table and drop it into another.
    however, i want certain cells in the table not to accept drops while still being able drop stuff on other cells of the same table.
    does anyone have an idea of how to do that? i have already tried some stuff like this:

    ui->twEmployees->item(1,0)->setFlags(ui->twEmployees->item(1,0)->flags() ^ (Qt::ItemIsDropEnabled));
    

    and it works... partially. the item cannot be dropped on the specific cell. but instead it creates a new row and drops it on that new row. which does not solve the problem at all.
    any kind of help would be appreciated.


  • Lifetime Qt Champion

    Hi and welcome to the forums.
    You have to subclass the TableWidget and help it a bit

    You could do like

    #ifndef MYTABLEWIDGET_H
    #define MYTABLEWIDGET_H
    
    #include <QTableWidget>
    #include <QDragMoveEvent>
    #include <QDebug>
    
    class MyTableWidget : public QTableWidget
    {
        Q_OBJECT
    public:
        explicit MyTableWidget(QWidget *parent = nullptr) : QTableWidget(parent) {}
    protected:
        void dragMoveEvent(QDragMoveEvent *event) override
        {
            // get item at mouse cursor 
            QTableWidgetItem *item = itemAt( event->pos() );
            if (!item ) return;
            if (item->flags() & Qt::ItemIsDropEnabled) { // is item ItemIsDropEnabled
                qDebug() << "ALL OK.";
                event->acceptProposedAction(); // then say yes 
                return; // leave
            }
    
            event->ignore(); // else we just ignore the drop
    
        }
    };
    
    #endif // MYTABLEWIDGET_H
    

    If you use UI forms, then have a look at Creators promotion feature to easily
    use such subclass with Designer.
    https://doc.qt.io/qt-5/designer-using-custom-widgets.html

    Then it works fine. The Blue as ^ (Qt::ItemIsDropEnabled) so it wont accept.

    alt text


  • Lifetime Qt Champion

    Hi and welcome to the forums.
    You have to subclass the TableWidget and help it a bit

    You could do like

    #ifndef MYTABLEWIDGET_H
    #define MYTABLEWIDGET_H
    
    #include <QTableWidget>
    #include <QDragMoveEvent>
    #include <QDebug>
    
    class MyTableWidget : public QTableWidget
    {
        Q_OBJECT
    public:
        explicit MyTableWidget(QWidget *parent = nullptr) : QTableWidget(parent) {}
    protected:
        void dragMoveEvent(QDragMoveEvent *event) override
        {
            // get item at mouse cursor 
            QTableWidgetItem *item = itemAt( event->pos() );
            if (!item ) return;
            if (item->flags() & Qt::ItemIsDropEnabled) { // is item ItemIsDropEnabled
                qDebug() << "ALL OK.";
                event->acceptProposedAction(); // then say yes 
                return; // leave
            }
    
            event->ignore(); // else we just ignore the drop
    
        }
    };
    
    #endif // MYTABLEWIDGET_H
    

    If you use UI forms, then have a look at Creators promotion feature to easily
    use such subclass with Designer.
    https://doc.qt.io/qt-5/designer-using-custom-widgets.html

    Then it works fine. The Blue as ^ (Qt::ItemIsDropEnabled) so it wont accept.

    alt text



  • thank you sooo much! i have been struggling with this for way too long.
    i will try to implement this and see how it works out for me.


  • Lifetime Qt Champion

    @Contrasty
    You are welcome. Should work good for your use case if i did understand it correctly. :)


Log in to reply