Results 1 to 2 of 2

Thread: QListWidgetItem subclass - Crash program in drag/drop

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation QListWidgetItem subclass - Crash program in drag/drop

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidgetItem subclass - Crash program in drag/drop

    Can you show us the code using for setting the data for the drag? I mean the mimeData() method from your widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Program crash when a signal is emitted
    By croscato in forum Qt Programming
    Replies: 7
    Last Post: 22nd November 2008, 22:24
  2. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

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.