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

  • 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

    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.

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

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

  • Lifetime Qt Champion


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

    There are also examples.

  • @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{
        using QListWidget::QListWidget;
        QMimeData *mimeData(const QList<QListWidgetItem *> items) const
            QMimeData *md = QListWidget::mimeData(items);
            QStringList texts;
            for(QListWidgetItem *item : selectedItems())
                texts << item->text();
            return md;

  • @Mr-Workalot

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

    Thanks for this.

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

     class ListWidget : public QListWidget {
         using QListWidget::QListWidget;
         QMimeData* mimeData(const QList<QListWidgetItem*> items) const
             QMimeData* md = QListWidget::mimeData(items);
             QStringList texts;
             for (QListWidgetItem* item : selectedItems())
                 texts << item->text();
             return md;

    and created my QListWidget using

        ui->block_commands_listwidget = new ListWidget(ui->centralwidget);
        ui->block_commands_listwidget->setMaximumSize(QSize(300, 16777215));

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

  • @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.

  • @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 –

Log in to reply