1 Attachment(s)
How do i drag item from QListWIdget and drop to QPlainTextEdit ?
Attachment 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::SingleSelecti on);
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.
Re: How do i drag item from QListWIdget and drop to QPlainTextEdit ?
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.
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
Re: How do i drag item from QListWIdget and drop to QPlainTextEdit ?
Quote:
Originally Posted by
d_stranz
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.
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_l istwidget"));
ui->block_commands_listwidget->setMaximumSize(QSize(300, 16777215));
but now i am not able to use itemclicked and item double clicked meathods.
Re: How do i drag item from QListWIdget and drop to QPlainTextEdit ?
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.
Code:
{
Q_OBJECT
public:
// ...
};
Please use CODE tags when posting code. See my signature below.