Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how do i pervent QTableWidget cells from having an item dropped on them?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 959 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Contrasty
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Contrasty
        wrote on last edited by
        #3

        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.

        mrjjM 1 Reply Last reply
        0
        • C Contrasty

          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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          C 1 Reply Last reply
          0
          • mrjjM mrjj

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

            C Offline
            C Offline
            Contrasty
            wrote on last edited by
            #5

            @mrjj Hi, I kind of left this code for a while and now I am coming back to it. i tried it and it works so far.
            I have another question if I may:
            Lets say that for the purposes of my program, I want to override the "dropEvent" function similar to what we did with "DragMoveEvent".
            and in order for me to know how to override it the right way, I need to look at the original implementation of the function "dropEvent". because otherwise the drop functinality won't work as intended.
            do you know if that code is available?

            what I want to do with DropEvent in this example is to print out which column and row the item was dropped on.
            any help would be appreciated!

            mrjjM 1 Reply Last reply
            0
            • C Contrasty

              @mrjj Hi, I kind of left this code for a while and now I am coming back to it. i tried it and it works so far.
              I have another question if I may:
              Lets say that for the purposes of my program, I want to override the "dropEvent" function similar to what we did with "DragMoveEvent".
              and in order for me to know how to override it the right way, I need to look at the original implementation of the function "dropEvent". because otherwise the drop functinality won't work as intended.
              do you know if that code is available?

              what I want to do with DropEvent in this example is to print out which column and row the item was dropped on.
              any help would be appreciated!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi and welcome back

              do you know if that code is available?

              All code for Qt is available. You can ask the installer to include it or you can use https://code.woboq.org/qt5/
              to browse it online.

              • I need to look at the original implementation of the function "dropEvent". because otherwise the drop functinality won't work as intended.

              Well yes if you want to change how it works you should reimplemnt it.

              However, if you just want to add to it. then often you can just call
              the base class's dropevent and do your code after.
              Then it will do as always and then you can do what you want.

              1 Reply Last reply
              1
              • C Offline
                C Offline
                Contrasty
                wrote on last edited by
                #7

                alright, Thanks for the advice!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved