Qt Top Item remove - c++

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

Related

QT add object pointer in a pointer list

I would like to add my parentWidget in a pointer list QList<QGraphicsWidget*> items from another class.
How can I add parentWidget in the items list and delete the parentWidget right after ?
Here is my code:
void DiagramScene::insertWidget(DiagramItem::DiagramType diagramtype)
{
QGraphicsWidget * parentWidget = new QGraphicsWidget;
//some code
connect(this,SIGNAL(sendToItemList(QGraphicsWidget*)),diagramitem,SLOT(addToItemList(QGraphicsWidget*)));
emit this->sendToItemList(parentWidget);
}
and this is my slot:
void DiagramItem::addToItemList(QGraphicsWidget* widget)
{
items.append(widget);
}
My actual code gives me this by each insertWidget call :
first call: items = 1 item
second call: items = 3 items
third call: items = 6 items
fourth call: items = 10 items

Get the all the children from a Sitecore item in a parent-child relation

The following code should return all the children and sub-children in a list, but it's not keeping the ordering of parent-child relation. It returns all the parent items first, and then all the child items.
Is it possible somehow to keep the items in order, and get them in a list in the same order as they are in Sitecore?
MainItem contains the items in the parent-child relation:
Item mainItem= Sitecore.Context.Database.GetItem(Settings.GetSetting("MainItem"));
mainItem.Axes.GetDescendants().ToList();
You could add items to a list recursively:
public void AppendItems(List<Item> itemList, Item item)
{
foreach(Item childItem in item.Children)
{
itemList.Add(childItem);
AppendItems(itemList, childItem);
}
}
Usage:
var list = new List<Item>();
list.Add(rootItem);
AppendItems(list, rootItem);

empty row in QTreeWidget after item delete

I have created a QTreeWidget with Qt Creator and filled it.
When i delete an item from the tree,an empty line moves down to the end of the tree.
When i then add to the tree, the item adds below the empty line.
How do i get rid of this empty line?
Image: http://picpaste.com/gpa-GNtd84Ev.PNG
void Gpa::on_removeButton_clicked()
{
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item = ui->treeWidget->currentItem();
QString txt = item->text(0);
//search vector for item
int i;
for(i = 0; i < vec.size(); i++)
if(vec.at(i)->getCode() == txt)
break;
vec.erase(vec.begin() + i);
int index = ui->treeWidget->indexOfTopLevelItem(item);
ui->treeWidget->takeTopLevelItem(index);
}
If you are removing a top level item, you should take a look at this method:
QTreeWidgetItem * QTreeWidget::takeTopLevelItem ( int index )
Otherwise, if you are removing a child you should get a pointer to the top level item (or a child of a top level item) and use:
QTreeWidgetItem * QTreeWidgetItem::takeChild ( int index )
EDIT (after OP has posted his code)
I believe the problem lies here:
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item = ui->treeWidget->currentItem();
You are allocating a new QTreeWidgetItem, and then you assign the item you want to remove to that pointer. So basically, you are creating new empty item and you remove the old one. Try to change your code to:
QTreeWidgetItem *item;
item = ui->treeWidget->currentItem();

Remove selected items from listWidget

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.
}

Qt C++ Get data from a selected row of a table view

I have a table view populated with data of Vehicle objects stored in a QList<Vehicle> cars; On this cars list i have to make some actions: search, add, edit, delete; After every action of search, i store the founded objects in another list to populate the table view only with this objects, keeping the original list intact. The original list gets modified only on add, edit or delete. Here comes the problem: i search for an object that i want to edit, it is shown in the table view, i select it and press the edit button; i am using QModelIndexList to get the index of the row. The index of the row will give me the position from the "founded" list, and i need to modify the object in the original list. Below is my implementation.
Is there any other way to this thing without the temporary list? How can i modify the original object using Iterator, instead that for loop?
void MainWindow::on_actionEdit_triggered()
{
QMessageBox msgBox;
QModelIndexList id = ui->tableView->selectionModel()->selectedIndexes();
if(id.isEmpty()){
msgBox.setWindowTitle("Message");
msgBox.setText("Please select a row");
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
} else{
int row = id.at(0).row();
QUuid carId = temp.at(row).getVehicleID(); // temp -> the "the founded" list
for(int i = 0; i < cars.size(); i++){
Vehicle& current = cars[i];
Vehicle& currentTemp = temp[row];
if(carId == current.getVehicleID() && carId == currentTemp.getVehicleID()){
addDialog = new AddEditDialog(this);
addDialog->loadVehicleToEdit(current);
addDialog->exec();
if(addDialog->getIsEdited()){
current = addDialog->getVehicleToAdd();
currentTemp = addDialog->getVehicleToAdd();
currentTemp.setVehicleId(carId);
current.setVehicleId(carId);
}
}
}
}
//create header
createHeader(model);
//set data to the table view
populate(temp);
}
May be you use proxy model and you need to call mapToSource?