Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget missing clearContents()?
Qt 6.11 is out! See what's new in the release blog

QTreeWidget missing clearContents()?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    Unusually, for a certain task I am using a QTableWidget and a QTreeWidget where I might more usually use QTableView & QTreeView, for simplicity of my needs.

    I note that QTableWidget has 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() in QTreeWidget it does not exist and there is only clear()

    Clears the tree widget by removing all of its items and selections.

    Why this asymmetry? A QTreeWidget has a header just as much as QTableWidget does it not, so why does the latter offer separate clearance for data versus headers while QTreeWidget() does not? Does QTreeWidget::clear() equate to QTableWidget::clearContents() and no QTableWidget::clear() equivalent? It does not affect any header and you have to deal with that yourself if you want it cleared for a QTreeWidget? Or have I misunderstood something?

    1 Reply Last reply
    1
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #3

      QTableWidget::clearContents() leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure. clear() removes headers and then calls clearnContent().

      QTreeWidget::clear() behaves like QTreWidget::clearContent().
      Unfortunately we're locked in BIC contracts. IMHO the QTableWidget implementation 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();
      

      Software Engineer
      The Qt Company, Oslo

      JonBJ 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #2

        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's clear does indeed look like QTableWidget's clearContents method.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #3

          QTableWidget::clearContents() leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure. clear() removes headers and then calls clearnContent().

          QTreeWidget::clear() behaves like QTreWidget::clearContent().
          Unfortunately we're locked in BIC contracts. IMHO the QTableWidget implementation 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();
          

          Software Engineer
          The Qt Company, Oslo

          JonBJ 1 Reply Last reply
          2
          • Axel SpoerlA Axel Spoerl

            QTableWidget::clearContents() leaves the headers untouched, which let the class expect new data in the same 2-dimensional structure. clear() removes headers and then calls clearnContent().

            QTreeWidget::clear() behaves like QTreWidget::clearContent().
            Unfortunately we're locked in BIC contracts. IMHO the QTableWidget implementation 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();
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Axel-Spoerl
            Indeed. It's the first time I had used a QTreeWidget and just found it inconsistent with QTableWidget. As we both noted, QTreeWidget::clear() == QTableWidget::clearContents() and it does not have a single method corresponding to QTableWidget::clear(). As it happens I was only wanting to clear the content so QTreeWidget::clear() was good for me.

            1 Reply Last reply
            1
            • JonBJ JonB has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved