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. Subclass of QTextEdit makes the program to crash
Forum Updated to NodeBB v4.3 + New Features

Subclass of QTextEdit makes the program to crash

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 2.9k Views 1 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.
  • D davidesalvetti

    @Gojir4 Thank you for your answer! I modify my code with your advice, but the problem is still there...

    What should it be if not the 'textedit'? I mean, if I comment the line of 'textedit->setText()' everything works fine.
    The last qDebug message is "HERE8".
    Have you got any other idea?
    I don't know why with the debugger engine evrything works, this is a strange thing, don't you think?

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

    @davidesalvetti said in Subclass of QTextEdit makes the program to crash:

    textedit->setText()

    Set a break point at this line and start debugging. If it stops at that line check the values of the variables, then execute the line and check what exactly is the crash (SIGSEGV, ...?). You can post the stacktrace after crash here.

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

    D 1 Reply Last reply
    2
    • D davidesalvetti

      @Gojir4 Thank you for your answer! I modify my code with your advice, but the problem is still there...

      What should it be if not the 'textedit'? I mean, if I comment the line of 'textedit->setText()' everything works fine.
      The last qDebug message is "HERE8".
      Have you got any other idea?
      I don't know why with the debugger engine evrything works, this is a strange thing, don't you think?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #7

      @Gojir4 said in Subclass of QTextEdit makes the program to crash:

      QWidget::focusOutEvent(e);

      I was wrong here, you have to call QTextEdit::focusOutEvent(e);, sorry

      @davidesalvetti Actually I have tested your QCustomTextEdit class and I don't have any crash with it, so, as @SGaist suggested, I think it's coming from somewhere else in your code too.

      1 Reply Last reply
      3
      • jsulmJ jsulm

        @davidesalvetti said in Subclass of QTextEdit makes the program to crash:

        textedit->setText()

        Set a break point at this line and start debugging. If it stops at that line check the values of the variables, then execute the line and check what exactly is the crash (SIGSEGV, ...?). You can post the stacktrace after crash here.

        D Offline
        D Offline
        davidesalvetti
        wrote on last edited by
        #8

        @jsulm It would be the right solution if the crash would still remain also after starting the debugging.
        When I use the debugging everything works fine, no crash. That's why I think it's some kind of optimization that is causing the problem.

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

          I think it's some kind of optimization that is causing the problem.

          Don't blame the compiler, we all do and it's never its fault

          • ui->frame_3->setLayout(layout); is useless. remove it
          • before QVBoxLayout *layout = new QVBoxLayout(ui->frame_3); add Q_ASSERT(!ui->frame_3->layout()); to make sure you don't have a layout already
          • after qDebug() << "HERE8"; add if(!textedit) qDebug("textedit is NULL!");
          • in the constructor add connect(textedit,&QObject::destroyed,[](){qDebug("textedit was destroyed!");}); to check if it gets deleted

          "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

          D 1 Reply Last reply
          4
          • VRoninV VRonin

            I think it's some kind of optimization that is causing the problem.

            Don't blame the compiler, we all do and it's never its fault

            • ui->frame_3->setLayout(layout); is useless. remove it
            • before QVBoxLayout *layout = new QVBoxLayout(ui->frame_3); add Q_ASSERT(!ui->frame_3->layout()); to make sure you don't have a layout already
            • after qDebug() << "HERE8"; add if(!textedit) qDebug("textedit is NULL!");
            • in the constructor add connect(textedit,&QObject::destroyed,[](){qDebug("textedit was destroyed!");}); to check if it gets deleted
            D Offline
            D Offline
            davidesalvetti
            wrote on last edited by
            #10

            @VRonin Thanks for your answer.
            I updated my code with your changes, this is the debug output:

            HERE4
            HERE5
            

            and then the crash...I don't understand :/
            It crashes every time I call the textedit object. After "HERE5" there is the code line

            if(!textedit)
                            qDebug() << "textedit is NULL!";
            qDebug() << "HERE8" << textedit;
            

            But these message are not shown in the application output.

            J.HilkJ 1 Reply Last reply
            0
            • D davidesalvetti

              @VRonin Thanks for your answer.
              I updated my code with your changes, this is the debug output:

              HERE4
              HERE5
              

              and then the crash...I don't understand :/
              It crashes every time I call the textedit object. After "HERE5" there is the code line

              if(!textedit)
                              qDebug() << "textedit is NULL!";
              qDebug() << "HERE8" << textedit;
              

              But these message are not shown in the application output.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #11

              @davidesalvetti

              create your Pointer with a nullptr set

              private:
                  Ui::Archivio *ui;
                  QCustomTextEdit             *textedit = nullptr;
              

              then it's created pointed to 0 and only changes when you new it, in case your function is called before the customTextEdit could be created.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              D 1 Reply Last reply
              5
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #12

                As an experiment you could also just try with a normal QTextEdit rather than a QCustomTextEdit

                "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

                D 1 Reply Last reply
                3
                • J.HilkJ J.Hilk

                  @davidesalvetti

                  create your Pointer with a nullptr set

                  private:
                      Ui::Archivio *ui;
                      QCustomTextEdit             *textedit = nullptr;
                  

                  then it's created pointed to 0 and only changes when you new it, in case your function is called before the customTextEdit could be created.

                  D Offline
                  D Offline
                  davidesalvetti
                  wrote on last edited by
                  #13

                  @J.Hilk Thanks for your answer. I tried but no changes, it's still crashing.

                  Anyway I don't know if this could be usefull but I've seen that sometimes it works right just for the first time I click the button, then I close the 'Archivio' and then I reopen it and clicking again on the button makes the crash.

                  J.HilkJ 1 Reply Last reply
                  0
                  • D davidesalvetti

                    @J.Hilk Thanks for your answer. I tried but no changes, it's still crashing.

                    Anyway I don't know if this could be usefull but I've seen that sometimes it works right just for the first time I click the button, then I close the 'Archivio' and then I reopen it and clicking again on the button makes the crash.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #14

                    @davidesalvetti
                    from the code example I can see that you have a ui-file for your class,

                    why don't you Promote your CustomWidget in the Desginer and let Qt's internal manage the creation and destruction of the object?


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    D 1 Reply Last reply
                    4
                    • VRoninV VRonin

                      As an experiment you could also just try with a normal QTextEdit rather than a QCustomTextEdit

                      D Offline
                      D Offline
                      davidesalvetti
                      wrote on last edited by
                      #15

                      @VRonin I just tried it. Creating the QTextEdit object from the constructor doesn't make change to the behaviour of the program. Is it possible that the problem is in how I create and put into the layout the Object?

                      If I create the QTexteEdit from the layout it works perfectly.

                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @davidesalvetti
                        from the code example I can see that you have a ui-file for your class,

                        why don't you Promote your CustomWidget in the Desginer and let Qt's internal manage the creation and destruction of the object?

                        D Offline
                        D Offline
                        davidesalvetti
                        wrote on last edited by
                        #16

                        @J.Hilk That made the trick! I din't know about promoting custom widget, but it worked!
                        So there must be something wrong in the way I've put the object into the layout and into the frame.
                        If somebody knows what is the problem is welcome so that I can avoid it the next times.

                        Anyway the topic is solved, thank you all.

                        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