PDA

View Full Version : How do i drag item from QListWIdget and drop to QPlainTextEdit ?



Mr_workalota787
1st June 2020, 07:50
13460

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::SingleSelectio n);
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.

d_stranz
1st June 2020, 16:00
When you start the drag, you need to create a mime type that is compatible with what the text edit will accept. I would start by looking at the Qt Drag and Drop examples. (https://doc.qt.io/qt-5/examples-draganddrop.html)

If the QListWidget does not create a QDrag with the right type of mime data, then you may have to install an event handler on the list widget to intercept the mouse press event and create the correct drag and mime objects. I am little surprised that a list widget would not create a mime object containing text

Mr_workalota787
4th June 2020, 01:08
When you start the drag, you need to create a mime type that is compatible with what the text edit will accept. I would start by looking at the Qt Drag and Drop examples. (https://doc.qt.io/qt-5/examples-draganddrop.html)

If the QListWidget does not create a QDrag with the right type of mime data, then you may have to install an event handler on the list widget to intercept the mouse press event and create the correct drag and mime objects. I am little surprised that a list widget would not create a mime object containing text

----------------------

Mr-Workalot less than a minute ago
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.

d_stranz
4th June 2020, 18:09
If this is your actual code, you are missing the Q_OBJECT macro at the top of the class declaration, so none of the signals / slots will work.



class ListWidget : public QListWidget
{
Q_OBJECT

public:

// ...
};


Please use CODE tags when posting code. See my signature below.