PDA

View Full Version : QListWidgetItem subclass - Crash program in drag/drop



estanisgeyer
16th April 2009, 02:55
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.



DLG_EditToolBar::DLG_EditToolBar(QWidget *parent)
:QDialog(parent)
{
ui.setupUi(this);
m_activeList = new ListWidget; // subclass QListWidget
m_inactiveList = new ListWidget; // subclass QListWidget

ui.gridLayoutAD->addWidget(m_inactiveList);
ui.gridLayoutAT->addWidget(m_activeList);
this->addAct(m_inactiveList);

connect(ui.pushButton_Ad, SIGNAL(clicked()), this, SLOT(addItem()));
connect(ui.pushButton_Rem, SIGNAL(clicked()), this, SLOT(removeItem()));
connect(ui.pushButton_Up, SIGNAL(clicked()), this, SLOT(upItem()));
connect(ui.pushButton_Down, SIGNAL(clicked()), this, SLOT(downItem()));
connect(ui.pushButton_OK, SIGNAL(clicked()), this, SLOT(ok()));
connect(ui.comboBox_BarraFerramentas, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBF(int)));
}

DLG_EditToolBar::~DLG_EditToolBar()
{

}

void DLG_EditToolBar::changeBF(int t)
{
if (t > 0)
{
listItem_toolp.clear(); // type QList<QListWidgetItem *>
int r = m_activeList->count();
for (int i = 0; i < r; i++)
{
listItem_toolp.append(m_activeList->takeItem(0));
}

m_activeList->clear();

for (int i = 0; i < listItem_toolaux.count(); i++)
{
m_activeList->addItem(listItem_toolaux.at(i));
}
}
else
{
listItem_toolaux.clear(); // type QList<QListWidgetItem *>
int r = m_activeList->count();
for (int i = 0; i < r; i++)
{
listItem_toolaux.append(m_activeList->takeItem(0));
}

m_activeList->clear();
for (int i = 0; i < listItem_toolp.count(); i++)
{
m_activeList->addItem(listItem_toolp.at(i));
}
}
}

void DLG_EditToolBar::addAct(QListWidget *p)
{
Item *item;
for (int i = 0; i < Actions::listTrAction()->count(); i++)
{
item = new Item(Actions::listTrAction()->at(i), Actions::listTrAction()->at(i)->id(), p);
}
}

void DLG_EditToolBar::addItem()
{
m_activeList->addItem(m_inactiveList->takeItem(m_inactiveList->currentRow()));
}

void DLG_EditToolBar::ok()
{
foreach (QListWidgetItem *i, listItem_toolp)
{
// here is the problem, it works when using the function AddItem,
// but when it does not drag n drop with the mouse.
qDebug() << static_cast<Item *>(i)->idAct();


//MainWindow::toolbar->addAction(static_cast<Item *>(i)->action());
}

foreach (QListWidgetItem *i, listItem_toolaux)
{
//MainWindow::toolbarAux->addAction(static_cast<Item *>(i)->action());
}

this->accept();
}



Thanks,

Marcelo E. Geyer

wysota
17th April 2009, 09:24
Can you show us the code using for setting the data for the drag? I mean the mimeData() method from your widget.