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. setModelData crashes program
Forum Updated to NodeBB v4.3 + New Features

setModelData crashes program

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 8 Posters 8.2k Views 3 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #20

    If you want to use it in code that should still be present in release mode you can use Q_ASSUME

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    JonBJ 1 Reply Last reply
    0
    • VRoninV VRonin

      If you want to use it in code that should still be present in release mode you can use Q_ASSUME

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

      @VRonin said in setModelData crashes program:

      If you want to use it in code that should still be present in release mode you can use Q_ASSUME

      Um, I don't think so are you sure: http://doc.qt.io/qt-5/qtglobal.html#Q_ASSUME states it's a compile-time hint, and you get "undefined behaviour" if the condition is false. We need a runtime check of the return result of qobject_cast<>.

      EDIT: Unless the docs are misleading, and the true implementation is via https://git.merproject.org/faenil/qtbase/commit/3b0ed624351441a2d7be45cf9582fd36955ae860 , which uses Q_ASSERT_X?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #22

        Q_ASSUME in debug mode is like Q_ASSERT in release mode, if whatComboEditor is null the program would crash anyway so you can't do much worse.

        The undefined behaviour comes from when you use the return value: if(Q_ASSUME(false)) is undefined behaviour. if it just wraps an operation you do not check anyway, Q_ASSUME does nothing to your compiled binary in release mode

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        JonBJ 1 Reply Last reply
        0
        • VRoninV VRonin

          Q_ASSUME in debug mode is like Q_ASSERT in release mode, if whatComboEditor is null the program would crash anyway so you can't do much worse.

          The undefined behaviour comes from when you use the return value: if(Q_ASSUME(false)) is undefined behaviour. if it just wraps an operation you do not check anyway, Q_ASSUME does nothing to your compiled binary in release mode

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

          @VRonin
          That wasn't quite the point, though. I was talking about writing the single-line statement (if Qt won't offer a qboject_assert_cast<>):

          Q_ASSERT(whatComboEditor = qobject_cast<>)

          versus

          Q_ASSUME(whatComboEditor = qobject_cast<>)

          The point being that it does the assignment. So we do need the expression to evaluate, just we don't need/care to do the "assertion" part of testing the result in release build. This would not work for Q_ASSERT as (I believe) it's a macro which produces nothing (no code at all) in release build. That was when you suggested Q_ASSUME.

          VRoninV 1 Reply Last reply
          0
          • JonBJ JonB

            @VRonin
            That wasn't quite the point, though. I was talking about writing the single-line statement (if Qt won't offer a qboject_assert_cast<>):

            Q_ASSERT(whatComboEditor = qobject_cast<>)

            versus

            Q_ASSUME(whatComboEditor = qobject_cast<>)

            The point being that it does the assignment. So we do need the expression to evaluate, just we don't need/care to do the "assertion" part of testing the result in release build. This would not work for Q_ASSERT as (I believe) it's a macro which produces nothing (no code at all) in release build. That was when you suggested Q_ASSUME.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #24
            • Q_ASSERT(statement);
              • Debug: if(!statement) triggerAnAssertion();
              • Release: ;
            • Q_ASSUME(statement);
              • Debug: if(!statement) triggerAnAssertion();
              • Release: statement;

            Is the difference clearer now?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            G JonBJ 2 Replies Last reply
            1
            • VRoninV VRonin
              • Q_ASSERT(statement);
                • Debug: if(!statement) triggerAnAssertion();
                • Release: ;
              • Q_ASSUME(statement);
                • Debug: if(!statement) triggerAnAssertion();
                • Release: statement;

              Is the difference clearer now?

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #25

              @VRonin
              I added

               Q_ASSERT(whatComboEditor);
              

              but it behaves the same.

              1 Reply Last reply
              0
              • VRoninV VRonin

                @gabor53 said in setModelData crashes program:

                What can I add to prevent the data from disappearing?

                Sounds like a model bug. What is fixModel?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #26

                @VRonin said in setModelData crashes program:

                Sounds like a model bug. What is fixModel?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                G 2 Replies Last reply
                0
                • VRoninV VRonin

                  @VRonin said in setModelData crashes program:

                  Sounds like a model bug. What is fixModel?

                  G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #27

                  @VRonin

                  QSqlTableModel* fixModel = new QSqlTableModel(this);
                    fixModel->setTable ("Items");
                    fixModel->setEditStrategy (QSqlTableModel::OnRowChange);
                    fixModel->setSort (2, Qt::DescendingOrder);
                  
                    fixModel->select ();
                  
                  1 Reply Last reply
                  0
                  • VRoninV VRonin
                    • Q_ASSERT(statement);
                      • Debug: if(!statement) triggerAnAssertion();
                      • Release: ;
                    • Q_ASSUME(statement);
                      • Debug: if(!statement) triggerAnAssertion();
                      • Release: statement;

                    Is the difference clearer now?

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

                    @VRonin

                    Is the difference clearer now?

                    Yep! :) Tx.

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @VRonin said in setModelData crashes program:

                      Sounds like a model bug. What is fixModel?

                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by gabor53
                      #29

                      @VRonin
                      Any way to go around the problem?
                      Would it be possible to just redraw the row in which the field was changed?

                      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