How to delete an item in QTreeWidget?
-
Hello, I searched the tree and found an item. I want to delete it.
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(), Qt::MatchFlag::MatchExactly, 1);
First I tried delete s.constFirst(); but the program just crashed.
Then I tried
QTreeWidgetItem *item = s.constFirst(); uint16_t row = ui->DeckList->indexFromItem(item, 1).row(); ui->DeckList->topLevelItem(row);
But it won't pass.
C:\Qt\5.10.1\mingw53_32\include\QtWidgets\qtreewidget.h:368: error: 'QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected
QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: removeWhat should I do?
-
@MasterBlade said in How to delete an item in QTreeWidget?:
uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
do you really need .row() at the end?
-
@MasterBlade said in How to delete an item in QTreeWidget?:
uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
do you really need .row() at the end?
@Fuel-0 said in How to delete an item in QTreeWidget?:
@MasterBlade said in How to delete an item in QTreeWidget?:
uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
do you really need .row() at the end?
I need row# to locate the item for ui->DeckList->topLevelItem(row);
Do you have a better suggestion?
-
maybe just
ui->DeckList->removeRow(s.row());
try this to remove a row.
take a look at this http://doc.qt.io/qt-5/qabstractitemmodel.html#removeRows
-
maybe just
ui->DeckList->removeRow(s.row());
try this to remove a row.
take a look at this http://doc.qt.io/qt-5/qabstractitemmodel.html#removeRows
@Fuel-0 said in How to delete an item in QTreeWidget?:
maybe just
ui->DeckList->removeRow(s.row());
try this to remove a row.
take a look at this http://doc.qt.io/qt-5/qabstractitemmodel.html#removeRows
But my problem is that I can't get the row# by
uint16_t row = ui->DeckList->indexFromItem(item, 1).row();
because QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected.
-
You're looking for removeChild: http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
-
Hello, I searched the tree and found an item. I want to delete it.
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(), Qt::MatchFlag::MatchExactly, 1);
First I tried delete s.constFirst(); but the program just crashed.
Then I tried
QTreeWidgetItem *item = s.constFirst(); uint16_t row = ui->DeckList->indexFromItem(item, 1).row(); ui->DeckList->topLevelItem(row);
But it won't pass.
C:\Qt\5.10.1\mingw53_32\include\QtWidgets\qtreewidget.h:368: error: 'QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected
QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const; // ### Qt 6: removeWhat should I do?
@MasterBlade said in How to delete an item in QTreeWidget?:
First I tried delete s.constFirst(); but the program just crashed.
First of all, why did this problem happen? Search it.
Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?Also are you sure
@MasterBlade said in How to delete an item in QTreeWidget?:
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
Qt::MatchFlag::MatchExactly, 1);s is non empty list ?
As documentation says http://doc.qt.io/qt-5/qlist.html#constFirstReturns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function. This function was introduced in Qt 5.6.
-
You're looking for removeChild: http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
@Christian-Ehrlicher said in How to delete an item in QTreeWidget?:
You're looking for removeChild: http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
So I need to find his parent and then delete him as a child?
-
@Christian-Ehrlicher said in How to delete an item in QTreeWidget?:
You're looking for removeChild: http://doc.qt.io/qt-5/qtreewidgetitem.html#removeChild
So I need to find his parent and then delete him as a child?
@MasterBlade
What if it does not have a parent? -
@MasterBlade said in How to delete an item in QTreeWidget?:
First I tried delete s.constFirst(); but the program just crashed.
First of all, why did this problem happen? Search it.
Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?Also are you sure
@MasterBlade said in How to delete an item in QTreeWidget?:
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
Qt::MatchFlag::MatchExactly, 1);s is non empty list ?
As documentation says http://doc.qt.io/qt-5/qlist.html#constFirstReturns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function. This function was introduced in Qt 5.6.
@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade said in How to delete an item in QTreeWidget?:
First I tried delete s.constFirst(); but the program just crashed.
First of all, why did this problem happen? Search it.
Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?I tried but found no relating result.
Actually this is a very simple tree and no item has any children at all.
Could you give me an example on deleting an item from searching?Also are you sure
@MasterBlade said in How to delete an item in QTreeWidget?:
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
Qt::MatchFlag::MatchExactly, 1);s is non empty list ?
As documentation says http://doc.qt.io/qt-5/qlist.html#constFirstReturns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function. This function was introduced in Qt 5.6.
Well actually I tested if s is empty or not. But as it's not important here I just delete it on this forum to make things more clear. The whole function looks like this.
I want to deduct the number(a value) of the item by one. If the number is already 1 before calling this function, remove it instead.
void Deck::RMD(int row) { QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(), Qt::MatchFlag::MatchExactly, 1); if (s.isEmpty()){ qDebug("Can't find the card on Deck::RMD"); return; } else{ uint8_t no = s.constFirst()->text(0).toUInt(); if (no == 1){ QTreeWidgetItem *item = s.constFirst(); QTreeWidgetItem *parent= item->parent(); uint16_t row = ui->DeckList->indexFromItem(item, 1).row(); ui->DeckList->topLevelItem(row); delete item; item = NULL; if (parent != NULL && parent->childCount() == 0){ delete parent; parent = NULL; } } else{ s.constFirst()->setText(0, QString::number(no - 1)); } } }
-
@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade said in How to delete an item in QTreeWidget?:
First I tried delete s.constFirst(); but the program just crashed.
First of all, why did this problem happen? Search it.
Note: If you delete a parent all its child will be deleted. Maybe you are using this item or this childrens somewhere?I tried but found no relating result.
Actually this is a very simple tree and no item has any children at all.
Could you give me an example on deleting an item from searching?Also are you sure
@MasterBlade said in How to delete an item in QTreeWidget?:
QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(),
Qt::MatchFlag::MatchExactly, 1);s is non empty list ?
As documentation says http://doc.qt.io/qt-5/qlist.html#constFirstReturns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty() before calling this function. This function was introduced in Qt 5.6.
Well actually I tested if s is empty or not. But as it's not important here I just delete it on this forum to make things more clear. The whole function looks like this.
I want to deduct the number(a value) of the item by one. If the number is already 1 before calling this function, remove it instead.
void Deck::RMD(int row) { QList<QTreeWidgetItem*> s = ui->DeckList->findItems(ui->CardList->item(row, 0)->text(), Qt::MatchFlag::MatchExactly, 1); if (s.isEmpty()){ qDebug("Can't find the card on Deck::RMD"); return; } else{ uint8_t no = s.constFirst()->text(0).toUInt(); if (no == 1){ QTreeWidgetItem *item = s.constFirst(); QTreeWidgetItem *parent= item->parent(); uint16_t row = ui->DeckList->indexFromItem(item, 1).row(); ui->DeckList->topLevelItem(row); delete item; item = NULL; if (parent != NULL && parent->childCount() == 0){ delete parent; parent = NULL; } } else{ s.constFirst()->setText(0, QString::number(no - 1)); } } }
@MasterBlade said in How to delete an item in QTreeWidget?:
Actually this is a very simple tree and no item has any children at all.
If you have only topLevelItems you dont need
QTreeWidgetItem *parent= item->parent();
I created a test, in my constructor add a 3 parent like this:
ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent")); ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 1")); ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 2"));
okay.. I have 3 topLevelITem "parent", "parent 1" and "parent 2".
void MainWindow::on_pushButton_clicked() { QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchFlag::MatchExactly, 0); qDebug() << s.size(); // == 1 Because I have a only one topLevelItem which name is"parent". delete s.constFirst(); }
my topLevelItem(0) is deleted. I dont have program crash.
Now i have a 2 topLevelItem named "parent 1" and "parent 2", now try to find a items witch contains "parent" with Qt::MatchContains flag.
void MainWindow::on_pushButton_2_clicked() { QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchContains, 0); qDebug() << s.size(); // == 2, Because topLevelItem "parent 1" and "parent 2" contains string "parent". foreach (QTreeWidgetItem *item, s) { delete item; } }
Also its deleted correctly and I dont have program crash.
-
@MasterBlade said in How to delete an item in QTreeWidget?:
Actually this is a very simple tree and no item has any children at all.
If you have only topLevelItems you dont need
QTreeWidgetItem *parent= item->parent();
I created a test, in my constructor add a 3 parent like this:
ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent")); ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 1")); ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() << "parent 2"));
okay.. I have 3 topLevelITem "parent", "parent 1" and "parent 2".
void MainWindow::on_pushButton_clicked() { QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchFlag::MatchExactly, 0); qDebug() << s.size(); // == 1 Because I have a only one topLevelItem which name is"parent". delete s.constFirst(); }
my topLevelItem(0) is deleted. I dont have program crash.
Now i have a 2 topLevelItem named "parent 1" and "parent 2", now try to find a items witch contains "parent" with Qt::MatchContains flag.
void MainWindow::on_pushButton_2_clicked() { QList<QTreeWidgetItem*> s = ui->treeWidget->findItems("parent", Qt::MatchContains, 0); qDebug() << s.size(); // == 2, Because topLevelItem "parent 1" and "parent 2" contains string "parent". foreach (QTreeWidgetItem *item, s) { delete item; } }
Also its deleted correctly and I dont have program crash.
@Taz742 said in How to delete an item in QTreeWidget?:
Also its deleted correctly and I dont have program crash.
Oh I'm sorry it's my fault. I'm still using item after deleting it.
I have one more problem. I'm making children now. But they don't seem to be aligning. Is there a way to remove those spaces before them?
-
@Taz742 said in How to delete an item in QTreeWidget?:
Also its deleted correctly and I dont have program crash.
Oh I'm sorry it's my fault. I'm still using item after deleting it.
I have one more problem. I'm making children now. But they don't seem to be aligning. Is there a way to remove those spaces before them?
@MasterBlade
You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved? -
@MasterBlade
You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade
You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?It was solved because I didn't add any children. As you see in the pic, the first row (MDeck) is good, but its child (001_007) has that problem. Should I reopen that ticket and ask there?
-
@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade
You dont solve this problem https://forum.qt.io/topic/90460/why-is-there-a-space-in-qtreewidget here? Why is a marked solved?It was solved because I didn't add any children. As you see in the pic, the first row (MDeck) is good, but its child (001_007) has that problem. Should I reopen that ticket and ask there?
@MasterBlade
I dont understood whats wrong this picture. Can you post a image what you want exactly? -
@MasterBlade
I dont understood whats wrong this picture. Can you post a image what you want exactly?@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade
I dont understood whats wrong this picture. Can you post a image what you want exactly?The '2' in the first column are not aligning. I want them to be in the same vertical position.
-
@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade
I dont understood whats wrong this picture. Can you post a image what you want exactly?The '2' in the first column are not aligning. I want them to be in the same vertical position.
@MasterBlade
It is not possible. 2 001_007 is a child MDeck. You can add only topLevelItems. -
@Taz742 said in How to delete an item in QTreeWidget?:
@MasterBlade
I dont understood whats wrong this picture. Can you post a image what you want exactly?The '2' in the first column are not aligning. I want them to be in the same vertical position.
@MasterBlade said in How to delete an item in QTreeWidget?:
I want them to be in the same vertical position.
Just set indentation to 0
-
@MasterBlade said in How to delete an item in QTreeWidget?:
I want them to be in the same vertical position.
Just set indentation to 0
@VRonin said in How to delete an item in QTreeWidget?:
@MasterBlade said in How to delete an item in QTreeWidget?:
I want them to be in the same vertical position.
Just set indentation to 0
This is working!!! Many thanks!!!