【问题标题】:Drag and drop from a QListWidget to a QLineEdit从 QListWidget 拖放到 QLineEdit
【发布时间】:2013-07-18 04:03:24
【问题描述】:

我有一个带有简单文本行的 QListWidget。我想设置它,以便用户可以将此数据拖放到一些 QLineEdit 对象中。

我已经为这两种类型打开了拖放功能,但它不允许我从 QListWidget 拖放到 QLineEdit。但是,它确实允许我从一个 QLineEdit 拖动到另一个 QLineEdit。

到目前为止,我没有重新实现任何方法。我所做的只是在编辑视图中打开拖放功能。

如何启用从 QListWidget 到 QLineEdit 的拖放功能?我需要重新实现哪些方法?

谢谢,

【问题讨论】:

    标签: qt drag-and-drop qlineedit


    【解决方案1】:

    我想通了。

    仅供参考:

    您需要继承 QLineEdit 并重新实现 dragEnterEvent 和 dropEvent。

    void MyLineEdit::dragEnterEvent(QDragEnterEvent *e){
        if(e->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")){
            e->acceptProposedAction();
        }
    }
    
    void MyLineEdit::dropEvent(QDropEvent *e){
        QByteArray encoded = e->mimeData()->data("application/x-qabstractitemmodeldatalist");
        QDataStream strm(&encoded, QIODevice::ReadOnly);
        while(!strm.atEnd()){
            int row, col;
            QMap<int,  QVariant> data;
            strm >> row >> col >> data;
            this->setText(data[0].toString());
        }
    }
    

    【讨论】:

      【解决方案2】:

      由于您尝试从不同类型的 dnd 中拖动不同的 mime 数据。因此 QLineEdit 不知道如何处理被丢弃的数据。子类化您的小部件并实现 dropEvent(),最终您还需要 dragEnterEvent()、dragLeaveEvent() 和 dragMoveEvent()。

      【讨论】:

      • QLineEdit 或 QListWidget 或两者都需要它们吗?
      • sry,工作很忙。正如你所写的 QLineEdit 需要 dropEvent/EnterDragEvent
      【解决方案3】:

      为了回答的完整性:您需要继承 QLineEdit(创建 MyLineEdit 类,它继承自 QLineEdit),并将 QLineEdit 的用法替换为 MyLineEdit(通常在 xxxx.ui 文件中):

      创建文件:MyLineEdit.h:

      #include <QLineEdit>
       
       
      class MyLineEdit : public QLineEdit
      {
       
      public:
        MyLineEdit(QWidget *parent = 0);
        ~MyLineEdit();
        void dragEnterEvent(QDragEnterEvent *e);
        void dragMoveEvent(QDragMoveEvent *e);
        void dropEvent(QDropEvent *e);
      };
       
      

      创建文件:MyLineEdit.cpp:

      #include "MyLineEdit.h"
      #include <QDragMoveEvent>
      #include <QDropEvent>
      #include <QUrl>
      #include <QFileInfo>
       
       
      MyLineEdit::MyLineEdit(QWidget *parent)
       : QLineEdit(parent)
      {}
       
      MyLineEdit::~MyLineEdit()
      {}
       
      void MyLineEdit::dragEnterEvent(QDragEnterEvent *e){
          if (e->mimeData()->hasUrls()) {
              e->acceptProposedAction();
          }
      }
       
      void MyLineEdit::dragMoveEvent(QDragMoveEvent *e){
          if (e->mimeData()->hasUrls()) {
              e->acceptProposedAction();
          }
      }
       
      void MyLineEdit::dropEvent(QDropEvent *event){
          const QMimeData* mimeData = event->mimeData();
       
          if (mimeData->hasUrls())
          {
              QStringList pathList;
              QList<QUrl> urlList = mimeData->urls();
       
              for (int i = 0; i < urlList.size() && i < 32; ++i)
              {
                  QString str = urlList.at(i).toLocalFile();
                  if (!str.isEmpty())
                  {
                      // support txt/csv files only
                      QString suffix = QFileInfo(str).suffix();
                      if (suffix == "txt" || suffix == "csv")  // place here whatever suffix you want
                      {
                          // action - set the file path field to the name of the dropped file
                          this->setText(str);
                          event->acceptProposedAction();
                          break;
                      }
                  }
              }
          }
      }
      

      并改变用法,例如,从此:

      <widget class="QLineEdit" name="lineEdit_xxxx">
      

      到这里:

      <widget class="MyLineEdit" name="lineEdit_xxxx">
      

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 2011-05-08
        • 2014-10-25
        • 2018-10-01
        • 1970-01-01
        • 2020-10-10
        • 2020-06-18
        • 2020-12-29
        相关资源
        最近更新 更多