Hi guys,

I have two QListWidget and drag n drop is working perfectly. Instead of using the class QListWidgetItem for items, I created a subclass of QListWidgetItem, adding two methods that need a QAction to a store (action) and another to store an integer (idAct).

The problem is when I use the mouse to do the drag / drop, the integer returned is invalid. I already use the AddItem method that I created, which is the same as the drag / drop the mouse, I got the right result. How can I fix this? I'll post the code here so they can understand.

Qt Code:
  1. DLG_EditToolBar::DLG_EditToolBar(QWidget *parent)
  2. :QDialog(parent)
  3. {
  4. ui.setupUi(this);
  5. m_activeList = new ListWidget; // subclass QListWidget
  6. m_inactiveList = new ListWidget; // subclass QListWidget
  7.  
  8. ui.gridLayoutAD->addWidget(m_inactiveList);
  9. ui.gridLayoutAT->addWidget(m_activeList);
  10. this->addAct(m_inactiveList);
  11.  
  12. connect(ui.pushButton_Ad, SIGNAL(clicked()), this, SLOT(addItem()));
  13. connect(ui.pushButton_Rem, SIGNAL(clicked()), this, SLOT(removeItem()));
  14. connect(ui.pushButton_Up, SIGNAL(clicked()), this, SLOT(upItem()));
  15. connect(ui.pushButton_Down, SIGNAL(clicked()), this, SLOT(downItem()));
  16. connect(ui.pushButton_OK, SIGNAL(clicked()), this, SLOT(ok()));
  17. connect(ui.comboBox_BarraFerramentas, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBF(int)));
  18. }
  19.  
  20. DLG_EditToolBar::~DLG_EditToolBar()
  21. {
  22.  
  23. }
  24.  
  25. void DLG_EditToolBar::changeBF(int t)
  26. {
  27. if (t > 0)
  28. {
  29. listItem_toolp.clear(); // type QList<QListWidgetItem *>
  30. int r = m_activeList->count();
  31. for (int i = 0; i < r; i++)
  32. {
  33. listItem_toolp.append(m_activeList->takeItem(0));
  34. }
  35.  
  36. m_activeList->clear();
  37.  
  38. for (int i = 0; i < listItem_toolaux.count(); i++)
  39. {
  40. m_activeList->addItem(listItem_toolaux.at(i));
  41. }
  42. }
  43. else
  44. {
  45. listItem_toolaux.clear(); // type QList<QListWidgetItem *>
  46. int r = m_activeList->count();
  47. for (int i = 0; i < r; i++)
  48. {
  49. listItem_toolaux.append(m_activeList->takeItem(0));
  50. }
  51.  
  52. m_activeList->clear();
  53. for (int i = 0; i < listItem_toolp.count(); i++)
  54. {
  55. m_activeList->addItem(listItem_toolp.at(i));
  56. }
  57. }
  58. }
  59.  
  60. void DLG_EditToolBar::addAct(QListWidget *p)
  61. {
  62. Item *item;
  63. for (int i = 0; i < Actions::listTrAction()->count(); i++)
  64. {
  65. item = new Item(Actions::listTrAction()->at(i), Actions::listTrAction()->at(i)->id(), p);
  66. }
  67. }
  68.  
  69. void DLG_EditToolBar::addItem()
  70. {
  71. m_activeList->addItem(m_inactiveList->takeItem(m_inactiveList->currentRow()));
  72. }
  73.  
  74. void DLG_EditToolBar::ok()
  75. {
  76. foreach (QListWidgetItem *i, listItem_toolp)
  77. {
  78. // here is the problem, it works when using the function AddItem,
  79. // but when it does not drag n drop with the mouse.
  80. qDebug() << static_cast<Item *>(i)->idAct();
  81.  
  82.  
  83. //MainWindow::toolbar->addAction(static_cast<Item *>(i)->action());
  84. }
  85.  
  86. foreach (QListWidgetItem *i, listItem_toolaux)
  87. {
  88. //MainWindow::toolbarAux->addAction(static_cast<Item *>(i)->action());
  89. }
  90.  
  91. this->accept();
  92. }
To copy to clipboard, switch view to plain text mode 

Thanks,

Marcelo E. Geyer