Results 1 to 4 of 4

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

  1. #1
    Join Date
    Apr 2020
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default 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

    ui->my_listwidget->setSelectionMode(QAbstractItemView::SingleSelecti on);
    ui->my_listwidget>setDragEnabled(true);
    ui->my_listwidget->setDragDropMode(QAbstractItemView:ragDrop);
    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2020
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

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

    Quote Originally Posted by d_stranz View Post
    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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. class ListWidget : public QListWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. // ...
    8. };
    To copy to clipboard, switch view to plain text mode 

    Please use CODE tags when posting code. See my signature below.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 7th July 2015, 19:38
  2. Replies: 2
    Last Post: 30th January 2014, 07:46
  3. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 05:29
  4. Replies: 3
    Last Post: 10th June 2010, 16:13
  5. Replies: 1
    Last Post: 22nd October 2009, 01:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.