1

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 ?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Matthew
  • 13
  • 3
  • I can't help you because I don't know what your code is. What's in your hands and how do you want the final state? – Mohammed Deifallah Jun 19 '20 at 19:06
  • See the new section at the bottom of my question. There isn't much code actually written for this part of the program. I have tried a few different mothods but the closest I found was the mimeData function from QTreeWidget (which is protected, so I can't access/use it from my class). – Matthew Jun 19 '20 at 19:25

2 Answers2

0

the way to do that is in the drop event

 void MyWidget::dropEvent(QDropEvent* e)
 {
      const QMimeData* mimeData = e->mimeData();
      ...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

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);
Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
  • Copying data to a ```QTableWidgetItem``` from the ```QTreeWidgetItem``` and then manually adding that item to the ```QTableWidget``` was how I did it in the end. I still think there should be a way of retrieving the mimeData from an item though. Why not provide a ```getMimeData``` function for ease of use? Oh well, thank you. – Matthew Jun 22 '20 at 13:03