How do I "move" an item from one QListWidget to another?
-
I have two QListWidget lists, leftList and rightList.
leftList is populated something like this:
@// labelList is a QStringList
foreach (QString label, labelList)
{
ui->leftList->addItem(label);
}@I then have a button which is intended to move the selected item in the leftList to the rightList:
This code IMHO should work but does nothing:
@void ListManager::on_addButton_clicked()
{
QListWidgetItem *widget = ui->leftList->currentItem();
ui->leftList->removeItemWidget(widget);
ui->rightList->addItem(widget);
}@However this code works, but is messy and also takeItem(), according to the documentation, doesn't garbage collect the removed item:
@void ListManager::on_addButton_clicked()
{
QListWidgetItem *widget = ui->leftList->currentItem();
int currentRow = ui->leftList->currentRow();
ui->rightList->addItem(widget->text());
ui->leftList->takeItem(currentRow);
}@ -
@
void ListManager::on_addButton_clicked()
{
QListWidgetItem *widget = ui->leftList->currentItem();
ui->leftList->removeItemWidget(widget);
ui->rightList->addItem(widget);
}@Are you sure the widget is not null?
You said that do nothing, so the item not delete in the leftlist? -
If I look at "removeItemWidget":http://doc.trolltech.com/4.7/qlistwidget.html#removeItemWidget it is not stated, whether the item is deleted or not. If you look at the code of QListWidget:
@
inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
{ setItemWidget(aItem, 0); }
@
So the meaning for mee looks more than removing an edit widget from an item than removing the item itself. (I think, that's why it's called removeItem Widget). -
I have checked in the book (C++ GUI Programming with Qt 4) as I have remembered they had similar example. The code there looks like this:
@void ProjectDialog::moveCurrentItem(ProjectListWidget *source,
ProjectListWidget *target)
{
if (source->currentItem()) {
QListWidgetItem *newItem = source->currentItem()->clone();
target->addItem(newItem);
target->setCurrentItem(newItem);
delete source->currentItem();
}
}
@ -
QListWidget is inherited from QListView (similar what we discussed "here":http://developer.qt.nokia.com/forums/viewthread/2524/ about QTableWidget and QTableView), so in every case you have the model, but with Q*Widget classes it is somehow hidden.
-
That's correct, behind the Q*Widgets, there is a QStandardItemModel. But as you have no influence on the model, some things (from my point of view) are more complicated than with model - view (but I'm used to create models).
But if you look at simple displaying situations, it's more effort needed to create a model than to use a Q*Widget, that's what I meant. But if you need editable view, I would always suggest to go the model - view thing, especially as you have more control over the data.
-
takeWidgetItem only removes the widget of the item which was set with setWidgetItem before. It does not remove the QListWidgetItem itself from the view; this is achieved with takeItem.
You can safely transfer a QListWidgetItem from on list widget to another:
@
void ListManager::on_addButton_clicked()
{
if( ui->leftList->count() == 0 )
// nothing to transfer...
return;QListWidgetItem *widget = ui->leftList->takeItem( ui->leftList->currentRow() );
ui->rightList->addItem(widget);
}
@This obviously works only with single selection mode of the list widget. For a multi-item selection mode you will have to do some more work.
-
Not always. If you need a UI toolkit that is perhaps cross platform, what else to use? wxWidgets? it's different, but better? I think no. And you can reduce the fragemntation but just don't use some things. The software we are creating is running 24/7 normally. So you should look at such things a bit, and where you can prevent, you should...
-
For linux, you use valgrin, for windows, I know no free solution, only commercial ones.
But for those tools, ther are already threads "here":http://developer.qt.nokia.com/forums/viewthread/2248 and "here":https://developer.qt.nokia.com/forums/viewthread/1924 -
We had a thread "here":http://developer.qt.nokia.com/forums/viewthread/1924 which had discussed the such tools.
(Sorry for spaming, I was to slow and meantime Gerolf answered the question)