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 drag item from QListWIdget and drop to QPlainTextEdit ?
Forum Updated to NodeBB v4.3 + New Features

How do i drag item from QListWIdget and drop to QPlainTextEdit ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 767 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.
  • M Offline
    M Offline
    Mr-Workalot
    wrote on last edited by
    #1

    query.PNG

    As shown in the image on the left side i have a QListWidget named "my_listwidget" populated with 3 commands and on the right side i have a QPlainTextEdit named "my_textedit".

    i am able to drag from QListWidget by using this code

    ui->my_listwidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->my_listwidget>setDragEnabled(true);
    ui->my_listwidget->setDragDropMode(QAbstractItemView::DragDrop);
    ui->my_listwidget->viewport()->setAcceptDrops(false);
    ui->my_listwidget->setDropIndicatorShown(true);```
    
    
    But i am not able to drop into my QPlainTextEdit, i guess because when i drag, its of "item type" and when im trying to drop into textbox, the QPlainTextEdit accepts only Text but not item type. How do i do this ? Thanks for going through this.
    B 1 Reply Last reply
    0
    • M Mr-Workalot

      query.PNG

      As shown in the image on the left side i have a QListWidget named "my_listwidget" populated with 3 commands and on the right side i have a QPlainTextEdit named "my_textedit".

      i am able to drag from QListWidget by using this code

      ui->my_listwidget->setSelectionMode(QAbstractItemView::SingleSelection);
      ui->my_listwidget>setDragEnabled(true);
      ui->my_listwidget->setDragDropMode(QAbstractItemView::DragDrop);
      ui->my_listwidget->viewport()->setAcceptDrops(false);
      ui->my_listwidget->setDropIndicatorShown(true);```
      
      
      But i am not able to drop into my QPlainTextEdit, i guess because when i drag, its of "item type" and when im trying to drop into textbox, the QPlainTextEdit accepts only Text but not item type. How do i do this ? Thanks for going through this.
      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      @Mr-Workalot You need to subclass QListWidget and reimplement your own mimeData function.
      The default one does not have a "text" value.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mr-Workalot
        wrote on last edited by
        #3

        Can you please provide the code fore that ?, Sorry , im really new to this.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Did you read the Drag & Drop chapter of Qt's documentation ?

          There are also examples.

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

          M 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Did you read the Drag & Drop chapter of Qt's documentation ?

            There are also examples.

            M Offline
            M Offline
            Mr-Workalot
            wrote on last edited by
            #5

            @SGaist

            now, i understood it a bit.
            QPlaintTextEdit does not recognize the mimetype that the QListWidget sends by default, so the solution is to override the mimeData() method adding the text of the selected items as plain text.
            and i have this code segment to do that

            class ListWidget: public QListWidget{
            public:
                using QListWidget::QListWidget;
            protected:
                QMimeData *mimeData(const QList<QListWidgetItem *> items) const
                {
                    QMimeData *md = QListWidget::mimeData(items);
                    QStringList texts;
                    for(QListWidgetItem *item : selectedItems())
                        texts << item->text();
                    md->setText(texts.join(QStringLiteral("\n")));
                    return md;
                }
            };```
            M 1 Reply Last reply
            0
            • M Mr-Workalot

              @SGaist

              now, i understood it a bit.
              QPlaintTextEdit does not recognize the mimetype that the QListWidget sends by default, so the solution is to override the mimeData() method adding the text of the selected items as plain text.
              and i have this code segment to do that

              class ListWidget: public QListWidget{
              public:
                  using QListWidget::QListWidget;
              protected:
                  QMimeData *mimeData(const QList<QListWidgetItem *> items) const
                  {
                      QMimeData *md = QListWidget::mimeData(items);
                      QStringList texts;
                      for(QListWidgetItem *item : selectedItems())
                          texts << item->text();
                      md->setText(texts.join(QStringLiteral("\n")));
                      return md;
                  }
              };```
              M Offline
              M Offline
              Mr-Workalot
              wrote on last edited by
              #6

              @Mr-Workalot

              Should i add this code segment in my main.cpp or mainwindow.cpp or mainwindow.h ??

              Thanks for this.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr-Workalot
                wrote on last edited by
                #7

                Yes, its working now , Thanks , i subclassed it using

                 class ListWidget : public QListWidget {
                 public:
                     using QListWidget::QListWidget;
                 protected:
                     QMimeData* mimeData(const QList<QListWidgetItem*> items) const
                     {
                         QMimeData* md = QListWidget::mimeData(items);
                         QStringList texts;
                         for (QListWidgetItem* item : selectedItems())
                             texts << item->text();
                         md->setText(texts.join(QStringLiteral("\n")));
                         return md;
                     }
                 };
                

                and created my QListWidget using

                
                    ui->block_commands_listwidget = new ListWidget(ui->centralwidget);
                    ui->block_commands_listwidget->setObjectName(QString::fromUtf8("block_commands_listwidget"));
                    ui->block_commands_listwidget->setMaximumSize(QSize(300, 16777215));
                

                but now i am not able to use itemclicked and item double clicked meathods.

                B 1 Reply Last reply
                0
                • M Mr-Workalot

                  Yes, its working now , Thanks , i subclassed it using

                   class ListWidget : public QListWidget {
                   public:
                       using QListWidget::QListWidget;
                   protected:
                       QMimeData* mimeData(const QList<QListWidgetItem*> items) const
                       {
                           QMimeData* md = QListWidget::mimeData(items);
                           QStringList texts;
                           for (QListWidgetItem* item : selectedItems())
                               texts << item->text();
                           md->setText(texts.join(QStringLiteral("\n")));
                           return md;
                       }
                   };
                  

                  and created my QListWidget using

                  
                      ui->block_commands_listwidget = new ListWidget(ui->centralwidget);
                      ui->block_commands_listwidget->setObjectName(QString::fromUtf8("block_commands_listwidget"));
                      ui->block_commands_listwidget->setMaximumSize(QSize(300, 16777215));
                  

                  but now i am not able to use itemclicked and item double clicked meathods.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @Mr-Workalot
                  This is not the right way to use custom widgets in ui files.
                  You should promote the listWidget in the ui to your new class.

                  M 1 Reply Last reply
                  2
                  • B Bonnie

                    @Mr-Workalot
                    This is not the right way to use custom widgets in ui files.
                    You should promote the listWidget in the ui to your new class.

                    M Offline
                    M Offline
                    Mr-Workalot
                    wrote on last edited by
                    #9

                    @Bonnie Thanks , its working now , but now i want to show "Pause Command" in QListwidget but after i drag "Pause Command" from QListWidget, i want "\n<Pause seconds="10" />" to appear in my QTextedit, , so how can i achieve this , so far i know that i can use a Key-Value pair Map and i have this code

                    map<string, string> commands;
                    commands["Pause Command"] = "\n<Pause seconds=\"10\" />";     
                    map<string, string>::iterator it;   
                    it = commands.find(item->text());   
                    return it->second;
                    

                    any way to tweak the class above posted before and acheive this, Thanks –

                    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