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. QTableView deleting causes a fault.
Forum Updated to NodeBB v4.3 + New Features

QTableView deleting causes a fault.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 505 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I've created an instance of QTableView:

    mptvTrecs = new QTableView(this);
    mptvTrecs->setSelectedBehaviour(QAbstractItemView::SelectRows);
    mptvTrecs->setStyleSheet(DataSets::mscszTableViewStyle);
    mpsiTmodel = new QStandardItemModel(this);
    mptvTrecs->setModel(mpsiTModel);
    

    The actual source that adds rows to the table hasn't been called until a Tab is selected in the dialog. If I close the dialog, the clean up looks like this:

    if ( mptvTrecs != nullptr ) {
        delete mptvTrecs;
    }
    if ( mpsiTmodel != nullptr ) {
        delete mpsiTmodel;
    }
    

    An exception is raised with a fault as soon as the line:

    delete mptvTrecs;
    

    Is called, why? Looks like a bug to me. I'm using Qt 5.9.2 on Windows 10 with Qt Creator 4.4.1

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you delete it at all? You properly gave a parent so no need to delete it by yourself.
      And the check for != nullptr is superfluous.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        It's hard to guess what exactly is failing. I suspect 2 things: dandling pointer or, since it is a visual element, even loop might still want to handle some signals or slots for it. Try:

        if (QPointer<QTableView>(mptvTrecs).isNull() == false) {
          mptvTrecs->deleteLater();
          mptvTrecs = nullptr;
        }
        

        The QPointer will make sure that object really, really is valid, and not a dangling pointer.

        (Z(:^

        1 Reply Last reply
        0
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #4

          I will remove the calls to delete, is the deletion of all Qt related objects taken care of by Qt ?

          Kind Regards,
          Sy

          KroMignonK JoeCFDJ 2 Replies Last reply
          0
          • SPlattenS SPlatten

            I will remove the calls to delete, is the deletion of all Qt related objects taken care of by Qt ?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @SPlatten said in QTableView deleting causes a fault.:

            Qt related objects taken care of by Qt ?

            Extract from Qt documentation =>https://doc.qt.io/qt-5/objecttrees.html

            When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            SPlattenS 1 Reply Last reply
            2
            • KroMignonK KroMignon

              @SPlatten said in QTableView deleting causes a fault.:

              Qt related objects taken care of by Qt ?

              Extract from Qt documentation =>https://doc.qt.io/qt-5/objecttrees.html

              When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #6

              @KroMignon , if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?

              Kind Regards,
              Sy

              KroMignonK 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @SPlatten said in QTableView deleting causes a fault.:

                if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?

                When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.

                Isn't this obvious enough?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                SPlattenS 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @SPlatten said in QTableView deleting causes a fault.:

                  if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?

                  When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.

                  Isn't this obvious enough?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher , if a widget is added to a layout using append, does this also mean it will be deleted by the layout?

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @SPlatten said in QTableView deleting causes a fault.:

                    does this also mean it will be deleted by the layout?

                    Not by the layout but by the widget where the layout is set to since the added widget gets re-parented to them.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    4
                    • SPlattenS SPlatten

                      @KroMignon , if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #10

                      @SPlatten said in QTableView deleting causes a fault.:

                      if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?

                      You can delete the timer when ever you want (but I will recommend to use deleteLater() to ensure it will be destroyed in this working thread).
                      If you do not delete it, it will be delete during parent destructor.

                      I am just paraphrasing what is already written in documentation (link from my previous post).

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        I will remove the calls to delete, is the deletion of all Qt related objects taken care of by Qt ?

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by JoeCFD
                        #11

                        @SPlatten Deleting a widget should work as well if it is not used anymore. But if crash is triggered by doing so, something is wrong. Sometimes, you may need to figure out why since it could be critical.

                        1 Reply Last reply
                        1

                        • Login

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