QTreeWidget missing clearContents()?
-
Unusually, for a certain task I am using a
QTableWidgetand aQTreeWidgetwhere I might more usually useQTableView&QTreeView, for simplicity of my needs.I note that
QTableWidgethas both clear()Removes all items in the view. This will also remove all selections and headers. If you don't want to remove the headers, use QTableWidget::clearContents(). The table dimensions stay the same.
and clearContents() methods. I will use
clearContents()when new data as I have designed the headers in Designer:Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.
Great. But when I look for same
clearContents()inQTreeWidgetit does not exist and there is only clear()Clears the tree widget by removing all of its items and selections.
Why this asymmetry? A
QTreeWidgethas a header just as much asQTableWidgetdoes it not, so why does the latter offer separate clearance for data versus headers whileQTreeWidget()does not? DoesQTreeWidget::clear()equate toQTableWidget::clearContents()and noQTableWidget::clear()equivalent? It does not affect any header and you have to deal with that yourself if you want it cleared for aQTreeWidget? Or have I misunderstood something? -
QTableWidget::clearContents()leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure.clear()removes headers and then callsclearnContent().QTreeWidget::clear()behaves likeQTreWidget::clearContent().
Unfortunately we're locked in BIC contracts. IMHO theQTableWidgetimplementation makes more sense, but changing it lands us neck deep in regressions.
That said....// Reset contents of a tree widget and a table widget, while keeping header structure treeWidget->clear(); tableWidget->clearContents(); // Fully reset both treeWidget->clear(); treeWidget->setColumnCount(0); tableWidget->clear(); -
Hi,
You may have just found discrepancy between the two APIs. Have you already checked the bug report system for that ?
To answer your question, from the looks of the implementation,
QTreeWidget'scleardoes indeed look likeQTableWidget'sclearContentsmethod. -
QTableWidget::clearContents()leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure.clear()removes headers and then callsclearnContent().QTreeWidget::clear()behaves likeQTreWidget::clearContent().
Unfortunately we're locked in BIC contracts. IMHO theQTableWidgetimplementation makes more sense, but changing it lands us neck deep in regressions.
That said....// Reset contents of a tree widget and a table widget, while keeping header structure treeWidget->clear(); tableWidget->clearContents(); // Fully reset both treeWidget->clear(); treeWidget->setColumnCount(0); tableWidget->clear(); -
QTableWidget::clearContents()leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure.clear()removes headers and then callsclearnContent().QTreeWidget::clear()behaves likeQTreWidget::clearContent().
Unfortunately we're locked in BIC contracts. IMHO theQTableWidgetimplementation makes more sense, but changing it lands us neck deep in regressions.
That said....// Reset contents of a tree widget and a table widget, while keeping header structure treeWidget->clear(); tableWidget->clearContents(); // Fully reset both treeWidget->clear(); treeWidget->setColumnCount(0); tableWidget->clear();@Axel-Spoerl
Indeed. It's the first time I had used aQTreeWidgetand just found it inconsistent withQTableWidget. As we both noted,QTreeWidget::clear()==QTableWidget::clearContents()and it does not have a single method corresponding toQTableWidget::clear(). As it happens I was only wanting to clear the content soQTreeWidget::clear()was good for me. -
J JonB has marked this topic as solved on