Search code examples
c++qtqtreewidgetqtreewidgetitem

How do I retrieve the mimeData for a particular item in a QTreeWidget?


I am trying to programmatically "drop" an item onto a QTableWidget using QTableWidget::dropMimeData().

I know which item I want to drop and I know that QTreeWidget has a QTreeWidget::mimeData() function, but I can not use that mimeData() function because it is protected.

Basically, how can I "select" a QTreeWidgetItem, pack up its mimeData, and "drop" that item onto a QTableWidget programmatically (no actual mouse drag/drop)?

Thank you.


As far as actual code:

Lets say I have a QTreeWidget with 3 "levels"

   QTreeWidgetItem *item = ui->treeWidget->child(i)->child(j)->child(k);

gets me my QTreeWidgetItem.

Now lets say I want to drop item onto my QTableWidget programmatically.

I need to use QTableWidget::dropMimeData(row,col,mimeData,action) (right?)

So how do I obtain the mimeData from item (that would be auto packed from a normal drag) so I can place it into the function call for dropMimeData ?


Solution

  • I think you can't use QTableWidget::dropMimeData(row,col,mimeData,action) because it's protected.

    In my opinion, you can use QVariant QTreeWidgetItem::data(int column, int role) const to get your tree node. After that, you could use QTableWidget::setItem(int row, int column, QTableWidgetItem *item) to insert a new item.

    This is an example from documentation to show how to insert a new row into the table:

    QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
            (row+1)*(column+1)));
    tableWidget->setItem(row, column, newItem);