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. Removing and deleting QTreeWidgetItem from QWidget within it.
Qt 6.11 is out! See what's new in the release blog

Removing and deleting QTreeWidgetItem from QWidget within it.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 224 Views
  • 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.
  • Q Offline
    Q Offline
    QJohn
    wrote on last edited by
    #1

    Hi,

    Having read all the available documentation, I cannot conclude if removing (taking) and deleting a QTreeWidgetItem from within one of it's children QWidget, is safe. Attached is a sample of what we have right now.

    cb2fa711-76d8-43df-a7b0-13faa07d18fa-image.png

    Anything we have tried till now, always start as a signal from QPushButton and ends up removing and deleting the QTreeWidget (and so the QWidget that emitted the signal..). And, because we are in the GUI thread, the connection is direct, so the chain starting from the signal of the button till the deletion, is like calling a function. But is something like that safe? Cause, with direct connection, we end up deleting also the object that emitted the signal, before the function returns.. Also there is no deleteLater() in QTreeWidgetItem.

    No crash was witnessed till now also, but this is not something we can base our development on.
    Is the only solution, to make the connection queued, so the QTreeWidgeItem and it's QWidgets are removed, in a later phase of Qt Even Loop?

    Thank you in advance.

    jsulmJ 1 Reply Last reply
    0
    • Q QJohn

      Hi,

      Having read all the available documentation, I cannot conclude if removing (taking) and deleting a QTreeWidgetItem from within one of it's children QWidget, is safe. Attached is a sample of what we have right now.

      cb2fa711-76d8-43df-a7b0-13faa07d18fa-image.png

      Anything we have tried till now, always start as a signal from QPushButton and ends up removing and deleting the QTreeWidget (and so the QWidget that emitted the signal..). And, because we are in the GUI thread, the connection is direct, so the chain starting from the signal of the button till the deletion, is like calling a function. But is something like that safe? Cause, with direct connection, we end up deleting also the object that emitted the signal, before the function returns.. Also there is no deleteLater() in QTreeWidgetItem.

      No crash was witnessed till now also, but this is not something we can base our development on.
      Is the only solution, to make the connection queued, so the QTreeWidgeItem and it's QWidgets are removed, in a later phase of Qt Even Loop?

      Thank you in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @QJohn Use https://doc.qt.io/qt-6/qobject.html#deleteLater instead of directly deleting widgets.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QJohn
        wrote on last edited by QJohn
        #3

        Thank you for your reply.
        Unfortunately, there is no deleteLater in QTreeWidgetItem.. Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?

        Maybe the right way is to remove first the widget from QTreeWidgetItem, then schedule it for deletion with deleteLater and then delete the QTreeWidgetItem?

        JonBJ 1 Reply Last reply
        0
        • Q QJohn

          Thank you for your reply.
          Unfortunately, there is no deleteLater in QTreeWidgetItem.. Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?

          Maybe the right way is to remove first the widget from QTreeWidgetItem, then schedule it for deletion with deleteLater and then delete the QTreeWidgetItem?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @QJohn said in Removing and deleting QTreeWidgetItem from QWidget within it.:

          Does the QTreeWidgetItem calls deleteLater for all the QWidget it holds?

          I do not have a complete answer for the best/safest way to do what you are asking. But for this question it calls void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget) (with widget == nullptr) and that calls oldWidget->deleteLater();.

          I am still unsure about the guarantees or lack of them if deleting a widget inside a signal from that widget, if direct connection is used. Though presumably deleteLater() (but not necessarily delete) should be OK.

          1 Reply Last reply
          0
          • Joe von HabsburgJ Offline
            Joe von HabsburgJ Offline
            Joe von Habsburg
            wrote on last edited by Joe von Habsburg
            #5

            May be you can try

            void btn_clicked(){
                  btn->setParent(nullptr);
                  deleteItem();
                  btn->deleteLater();
            }
            

            But my opinion if you call directly

            void btn_clicked(){
                  deleteItem();
            }
            

            It will work.

            QTreeWidgetItem::~QTreeWidgetItem()
            Destroys this tree widget item.
            The item will be removed from QTreeWidgets to which it has been added. This makes it safe to delete an item at any time.

            1 Reply Last reply
            0

            • Login

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