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. Drag and drop to a Pushbutton
Forum Updated to NodeBB v4.3 + New Features

Drag and drop to a Pushbutton

Scheduled Pinned Locked Moved Solved General and Desktop
33 Posts 3 Posters 4.2k Views 1 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.
  • JonBJ JonB

    @dencla said in Drag and drop to a Pushbutton:

    so why does the query model allow calls to edit it?

    Because QSqlQueryModel is derived from QAbstractTableModel if you are asking about compilation. At runtime we have already said it returns an unsuccessful result if you call it. Some model types allow editing, some do not.

    When I create a ui->CustomTableView nothing is displayed, but when I create a normal tableView it does display the database.

    I don't know what this means. If a QTableView works but a CustomTableView derived from that does not look at your code differences in the custom view. I don't know what this has to do with QSqlQueryModel vs QSqlTableModel, nor with drag and drop.

    D Offline
    D Offline
    dencla
    wrote on last edited by
    #22

    @JonB It was a typo sorry. I figured it out. On to the pushbutton.

    1 Reply Last reply
    0
    • JonBJ JonB

      @dencla said in Drag and drop to a Pushbutton:

      so why does the query model allow calls to edit it?

      Because QSqlQueryModel is derived from QAbstractTableModel if you are asking about compilation. At runtime we have already said it returns an unsuccessful result if you call it. Some model types allow editing, some do not.

      When I create a ui->CustomTableView nothing is displayed, but when I create a normal tableView it does display the database.

      I don't know what this means. If a QTableView works but a CustomTableView derived from that does not look at your code differences in the custom view. I don't know what this has to do with QSqlQueryModel vs QSqlTableModel, nor with drag and drop.

      D Offline
      D Offline
      dencla
      wrote on last edited by
      #23

      @JonB I can Drag and drop from a ui->tableView, now I need to drop to a CustomPushButton. I have promoted a ui->QPushButton to a ui->CustomPushButton. When I drag the drop indicator goes away and the drop fails. I am not sure what triggers the event.

      Below is the code for the CustomPushButton::

      #include <QPushButton>
      #include <QMimeData>
      #include <QDataStream>
      #include <QDropEvent>
       
      class CustomPushButton : public QPushButton {
      public:
          CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) {
              setAcceptDrops(true);
          }
      
          // Override the virtual method to handle drop events
          void dropEvent(QDropEvent *event) override {
              const QMimeData *mimeData = event->mimeData();
              if (mimeData && mimeData->hasText()) {
                  setText(mimeData->text());
                  event->acceptProposedAction();
              }
          }
      };
      
      Any Ideas
      
      
      SGaistS 1 Reply Last reply
      0
      • D dencla

        @JonB I can Drag and drop from a ui->tableView, now I need to drop to a CustomPushButton. I have promoted a ui->QPushButton to a ui->CustomPushButton. When I drag the drop indicator goes away and the drop fails. I am not sure what triggers the event.

        Below is the code for the CustomPushButton::

        #include <QPushButton>
        #include <QMimeData>
        #include <QDataStream>
        #include <QDropEvent>
         
        class CustomPushButton : public QPushButton {
        public:
            CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) {
                setAcceptDrops(true);
            }
        
            // Override the virtual method to handle drop events
            void dropEvent(QDropEvent *event) override {
                const QMimeData *mimeData = event->mimeData();
                if (mimeData && mimeData->hasText()) {
                    setText(mimeData->text());
                    event->acceptProposedAction();
                }
            }
        };
        
        Any Ideas
        
        
        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #24

        @dencla you also need to implement the dragEnterEvent and dragMoveEvent methods as they handle the dragging until you drop.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 2 Replies Last reply
        1
        • SGaistS SGaist

          @dencla you also need to implement the dragEnterEvent and dragMoveEvent methods as they handle the dragging until you drop.

          D Offline
          D Offline
          dencla
          wrote on last edited by
          #25

          @SGaist OK I'll give that a shot.
          Thanks

          1 Reply Last reply
          0
          • SGaistS SGaist

            @dencla you also need to implement the dragEnterEvent and dragMoveEvent methods as they handle the dragging until you drop.

            D Offline
            D Offline
            dencla
            wrote on last edited by
            #26

            @SGaist This is what I have now. It still does not register the drag on the CustomPushButton.

            #include <QPushButton>
            #include <QMimeData>
            #include <QDataStream>
            #include <QDropEvent>
            #include <QDebug>
            
            class CustomPushButton : public QPushButton {
            public:
                CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) {
                    setAcceptDrops(true); // Enable drop events for the push button
                }
            
            protected:
                void dragEnterEvent(QDragEnterEvent *event) override {
                    if (event->mimeData()->hasFormat("application/vnd.text.list"))
                        event->accept();
                    else
                        event->ignore();
                }
            
                void dragMoveEvent(QDragMoveEvent *event) override {
                    if (event->mimeData()->hasFormat("application/vnd.text.list")) {
                        event->setDropAction(Qt::CopyAction);
                        event->accept();
                    } else {
                        event->ignore();
                    }
                }
            
                void dropEvent(QDropEvent *event) override {
                    const QMimeData *mimeData = event->mimeData();
                    if (!mimeData->hasFormat("application/vnd.text.list")) {
                        event->ignore();
                        return;
                    }
            
                    QByteArray encodedData = mimeData->data("application/vnd.text.list");
                    QDataStream stream(&encodedData, QIODevice::ReadOnly);
            
                    QStringList items;
                    while (!stream.atEnd()) {
                        QString text;
                        stream >> text;
                        items.append(text);
                    }
            
                    qDebug() << "Dropped Items:" << items;
            
                    // Perform actions based on the dropped items
                    if(items.size() > 0) {
                        setText(items.at(0));
                        qDebug() << "inserted data";
                    }
                    // Call the base class implementation to handle other drop events
                    QPushButton::dropEvent(event);
                }
            };
            
            
            
            SGaistS 1 Reply Last reply
            0
            • D dencla

              @SGaist This is what I have now. It still does not register the drag on the CustomPushButton.

              #include <QPushButton>
              #include <QMimeData>
              #include <QDataStream>
              #include <QDropEvent>
              #include <QDebug>
              
              class CustomPushButton : public QPushButton {
              public:
                  CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) {
                      setAcceptDrops(true); // Enable drop events for the push button
                  }
              
              protected:
                  void dragEnterEvent(QDragEnterEvent *event) override {
                      if (event->mimeData()->hasFormat("application/vnd.text.list"))
                          event->accept();
                      else
                          event->ignore();
                  }
              
                  void dragMoveEvent(QDragMoveEvent *event) override {
                      if (event->mimeData()->hasFormat("application/vnd.text.list")) {
                          event->setDropAction(Qt::CopyAction);
                          event->accept();
                      } else {
                          event->ignore();
                      }
                  }
              
                  void dropEvent(QDropEvent *event) override {
                      const QMimeData *mimeData = event->mimeData();
                      if (!mimeData->hasFormat("application/vnd.text.list")) {
                          event->ignore();
                          return;
                      }
              
                      QByteArray encodedData = mimeData->data("application/vnd.text.list");
                      QDataStream stream(&encodedData, QIODevice::ReadOnly);
              
                      QStringList items;
                      while (!stream.atEnd()) {
                          QString text;
                          stream >> text;
                          items.append(text);
                      }
              
                      qDebug() << "Dropped Items:" << items;
              
                      // Perform actions based on the dropped items
                      if(items.size() > 0) {
                          setText(items.at(0));
                          qDebug() << "inserted data";
                      }
                      // Call the base class implementation to handle other drop events
                      QPushButton::dropEvent(event);
                  }
              };
              
              
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #27

              @dencla are you sure your overrides are called ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 3 Replies Last reply
              0
              • SGaistS SGaist

                @dencla are you sure your overrides are called ?

                D Offline
                D Offline
                dencla
                wrote on last edited by
                #28

                @SGaist No I do't think they are, but I'm not sure. I will trace through the program but I don't think I will see anything. What would cause that. I am on ios apple mini.

                1 Reply Last reply
                0
                • SGaistS SGaist

                  @dencla are you sure your overrides are called ?

                  D Offline
                  D Offline
                  dencla
                  wrote on last edited by
                  #29

                  @SGaist It definitely is not calling them.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @dencla are you sure your overrides are called ?

                    D Offline
                    D Offline
                    dencla
                    wrote on last edited by
                    #30

                    @SGaist I recompiled the project in Linux and it works.
                    I don't know why it doesn't work on my apple computer.

                    Thanks

                    SGaistS 1 Reply Last reply
                    0
                    • D dencla has marked this topic as solved on
                    • D dencla

                      @SGaist I recompiled the project in Linux and it works.
                      I don't know why it doesn't work on my apple computer.

                      Thanks

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #31

                      @dencla which version of macOS are you using ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      D 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @dencla which version of macOS are you using ?

                        D Offline
                        D Offline
                        dencla
                        wrote on last edited by
                        #32

                        @SGaist macOS Monterey 12.7.2

                        SGaistS 1 Reply Last reply
                        0
                        • D dencla

                          @SGaist macOS Monterey 12.7.2

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #33

                          @dencla I tested on Ventura with both 5.15.2 and 6.7.0 and your code is working. I just changed the mime type to handle plain text for testing and it's working as expected.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

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