Remove selected items from listWidget - c++

How to remove selected items from a QListWidget?
I have tried write the following code, but it does not work.
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem item, items){
ui->listWidget->removeItemWidget(item);
}

One way to remove item from QListWidget is to use QListWidget::takeItem which removes and returns the item :
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete ui->listWidget->takeItem(ui->listWidget->row(item));
}
Another way is to qDeleteAll :
qDeleteAll(ui->listWidget->selectedItems());

To give a solution with removeItemWidget:
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem* item, items){
ui->listWidget->removeItemWidget(item);
delete item; // Qt documentation warnings you to destroy item to effectively remove it from QListWidget.
}

Related

Qt Top Item remove

QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete item;
}
The above function deletes the item selected in the qtlist. I want to delete the top item present on the list

How to, inside a QTreeView, delete a selected row?

I tried to delete a selected row of a QTreeView with an event.
This is the function:
bool TargetEventFilter::deleteselect(bool delete)
{
auto select= examplemodel->selectionModel()->currentIndex();
auto parent= select.parent();
int row= select.row();
bool remove = examplemodel->model()->removeRow(row, parent);
return remove ;
}
So my problem is:
The event is working (tried with the debugger).The Parent and the indext is always right. But the problem is that the selected row will not be deleted. In "return remove" i get a "false".
why you do not used CurrentIndex ?
bool remove = examplemodel->model()->removeRow(row, select);

How to access childWidgets in a QTreeView using QModelIndex?

I'm working on an application using the Qt library (version 4.8).
I have a QTreeView with a QStandardItemModel. My widget looks like that:
Item1
subitem11
subitem12
Item2
subitem21
subitem22
Item3
subitem31
subitem32
Here is how I add the items to my QTreeView:
model->setItem(0, 0, item1);
item1->setChild(0, 0, subitem12);
I want to take an action only when the user double cliked an item (and do nothing when he clicked a subitem). So I use the doubleClicked(const QModelIndex & index) signal.
I want to process the information about the item/subitem which was double cliked by the user. So I get the row of my item/subitem:
index.row();
But every time I try to reference to the item/subitem to display its name or check if it has children, I can only access the items:
index.model()->item(row)->text();
My question is: how can I acces the subitems (vbetween items abd subitems) in my slot? Or how can I prevent them from emitting the signal? I can't disable them - it would be too confusing for the user.
Edit: The problem is that every time I click on an item or subitem and execute:
index.model()->item(row)->hasChildren();
or:
index.model()->item(row)->parent() == 0;
I get true as result. So I can reference only the items.
My question is: What is the correct way to reference to subitems?
When you are trying to access model items by row index, the model returns top-level item at that row. Use itemFromIndex instead:
auto item = index.model()->itemFromIndex(index);
if (item && item->hasChildren()){
// item is not a leaf
}
EDIT index.model() returns QAbstractItemModel*, so a cast is also necessary here (or, better, storing a pointer to the standard model somehwere in the code).
I would do it like this:
// Define your custom role to store item type.
enum MyRoles
{
ItemTypeRole = Qt::UserRole + 1
};
// Define item types.
enum ItemType
{
Primary,
Secondary
};
Than set the proper item type for all your items:
QStandardItem* item = new QStandardItem("Item1");
item->setData(Primary, ItemTypeRole);
QStandardItem* subItem = new QStandardItem("SubItem1");
subItem->setData(Secondary, ItemTypeRole);
And in your slot connected to the doubleClicked signal acces the type like this:
ItemType type = static_cast<ItemType>(index.data(ItemTypeRole).toInt());
if (type == Primary)
std::cout << "It's a Primary item!" << std::endl;
else if (type == Secondary)
std::cout << "It's a Secondary item!" << std::endl;
You can't disable the signal for the subitems. However, you can check if an item has a parent. It it doesn't, it is an item and if it does, it's a subitem.
if (item->parent() != 0)
.. //subitem
else
.. //item
An alternative would be to use the data() function to set some special value to distinguish between the two.
item1->setData(QVariant("item"));
subitem1->setData(QVariant("subitem"));
Then query the value in your doubleclick handler:
QVariant var = item->data();
if (var.toString() == "item")
...
else if (var.toString() == "subitem")
...

How to get clicked/selected items of a QTreeWidget

I'm currently looking to a way to get the name of the selected items of a QTreeWidget.
I have create multiple QTreeWidgetItems to generate a file browser-like.
I need to know how to get the name of the folder selected.
I have found the
this->MyTree->selectedItems();
to get it but I'm not able to store the feedback which is supposed to be a QList format.
Any examples on how to store the selectedItems list ?
From the Qt documentation: QTreeWidget Class Reference , QTreeWidgetItem Class Reference
selectedItems() is a function of QTreeWidget.
QList QTreeWidget::selectedItems () const
Returns a list of all selected non-hidden items.
text() is a function of QTreeWidgetItem
QString QTreeWidgetItem::text ( int column ) const
Returns the text in the specified column.
Define a list of QTreeWidgetItem to store return value of selectedItems() .
For each item in the list use text() function to get it's string.
QList<QTreeWidgetItem *> itemList;
itemList = this->MyTree->selectedItems();
foreach(QTreeWidgetItem *item, itemList)
{
QString str = item->text();
//str is what you want
}
{
...
connect(treeWidget, &QTreeWidget::itemClicked, this, &MyForm::onItemClicked);
connect(treeWidget, &QTreeWidget::itemActivated, this, &MyForm::onItemClicked);
}
void MyForm::onItemClicked(QTreeWidgetItem *item, int) {
if (item == item0) {
stackedWidget->setCurrentWidget(widget0);
}
else if (item == item1) {
stackedWidget->setCurrentWidget(widget1);
}
}

Hide the checkbox from a QListView item

I wave a QListView that is backed by a QStandardItemModel. Under certain circonstances, the QStandardItem are made checkable. A checkbox gets displayed besides the item's display. At some point, I want to remove hide the QStandardItem checkbox. I set its checkable state to false but it doesn't hide the checkbox (though it cannot be checked anymore).
The only way I have found of hiding the checkbox is to replace the item with a new one. This doesn't seem the proper way to preceed.
This is the code:
MyModel::MyModel(QObject *parent):QStandardItemModel(parent){}
void MyModel::createItem(int row, const QString &text)
{
setItem(row, new QStandardItem(text));
}
void MyModel::setCheckable(int row)
{
item(row)->setCheckState(Qt::Unchecked);
item(row)->setCheckable(true); // A checkbox appears besides the text
}
void MyModel::hideCheckBox(int row)
{
item(row)->setCheckState(Qt::Unchecked);
item(row)->setCheckable(false); // does not work
// I need to completely replace the item for the checkbox to disapear.
// This doesn't seem the proper way to proceed
setItem(row, new QStandardItem(item(row)->data(Qt::DisplayRole).toString()));
}
Is there better way to proceed?
When you call setCheckState or setCheckable, the qt will update the data of list item by adding or setting a Qt::CheckStateRole data. If the Qt::CheckStateRole data is existed, the check icon will be shown. So you need remove it from the data map of the list item.
Finally, the code of hideCheckBox should be:
void MyModel::hideCheckBox(int row)
{
// check the item pointer
QStandardItem* pitem = item(row);
if (pitem == NULL) return;
// find and delete the Qt::CheckStateRole data
QMap<int, QVariant> mdata = itemData(pitem->index());
if (mdata.remove(Qt::CheckStateRole))
{
setItemData(pitem->index(), mdata);
}
}
Hope it useful. :)
I think the presence of the check boxes in items defined by item flags, so that I would write the function in the following way:
void MyModel::hideCheckBox(int row)
{
// Does not set the Qt::ItemIsUserCheckable flag.
item(row)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}