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
QtWS25 Last Chance

Subclass of QTextEdit makes the program to crash

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 2.7k 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.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by davidesalvetti
    #1

    Hi,

    I'm having problem with a subclass of QTextEdit. First of all I had to subclass QTextEdit because I need the 'editingFinish' signal that is not supported by QTextEdit. But now I don't understand why this new class is causing the crash. I'll try to make you understand better with the code.

    qcustomtextedit.h

    class QCustomTextEdit : public QTextEdit
    {
        Q_OBJECT
    public:
        QCustomTextEdit( QWidget *parent = 0):QTextEdit(parent)
        {
    
        }
    
    signals:
        void editingFinished(QString);
    
    protected:
        void focusOutEvent(QFocusEvent *e)
        {
            QTextEdit::focusOutEvent(e);
            emit editingFinished(this->toPlainText());
        }
    };
    

    archivio.h

    #include "qcustomtextedit.h"
    
    namespace Ui {
    class Archivio;
    }
    
    class Archivio : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit Archivio(QWidget *parent = 0);
        ~Archivio();
    
    public slots:
    void on_PushButton_clicked();
    void textedit_editingFinished(QString);
    
    private:
        Ui::Archivio *ui;
        QCustomTextEdit             *textedit;
    
    };
    

    archivio.cpp

    Archivio::Archivio(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Archivio)
    {
        ui->setupUi(this);
        setAttribute(Qt::WA_DeleteOnClose);                                     
        ArchivioAperto   = true;                                                 
    
        textedit = new QCustomTextEdit;
        textedit->setStyleSheet("*{background-color:white; color:black;}");
        QVBoxLayout *layout = new QVBoxLayout(ui->frame_3);
        layout->addWidget(textedit);
        layout->setSpacing(0);
        layout->setMargin(0);
        layout->setContentsMargins(0,0,0,0);
        ui->frame_3->setLayout(layout);
        connect(textedit, SIGNAL(editingFinished(QString)), this, SLOT(textedit_editingFinished(QString)));
    }
    void Archivio::on_PushButton_clicked()
    {
            qDebug() << "HERE4";
            QFile note("C:/Progetti/QT_Project"+ "/Note.txt");
            if(note.open(QIODevice::ReadOnly))
            {
                qDebug() << "HERE5";
                QByteArray baNote = note.readAll();
                qDebug() << "HERE8";
                textedit->setText(QString(baNote));//the program crashes here
                note.close();
                qDebug() << "HERE6";
            }
            else
            {
    
            }
    }
    
    void Archivio::textedit_editingFinished(QString message)
    {
        qDebug() << message;
            QFile o_filenote("C:/Progetti/QT_Project"+ "/Note.txt");
            o_filenote.remove();
            QFile n_filenote("C:/Progetti/QT_Project" + "/Note.txt");
            if(n_filenote.open(QIODevice::WriteOnly))
            {
                n_filenote.write(message.toLatin1());
                n_filenote.close();
            }
    }
    

    I cannot understand how the textedit object can be 0x0 after I have created the Archivio class.
    Have I made some mistake while creating the subclass?

    The interesting thing is that when I launch the debugger everything works fine, I have problems only if I don't launch the program from it.

    Thanks in advance!

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

      Hi,

      From the looks of it, you didn't show all your code. Do you have somewhere another assignment to that variable ?

      On an unrelated note, there's no need for the call to clear since you are calling setText which replaces the content of the text edit.

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

      D 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        From the looks of it, you didn't show all your code. Do you have somewhere another assignment to that variable ?

        On an unrelated note, there's no need for the call to clear since you are calling setText which replaces the content of the text edit.

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

        @SGaist I have updated the code with all the instance of 'textedit', I'm sorry to make you lose time but I can't show all the code. Anyway these are ALL the instances of 'textedit' so it should be enough, am I wrong?

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

        On an unrelated note, there's no need for the call to clear since you are calling setText which replaces the content of the text edit.

        Yes, you are right, I tried it just because maybe the problem was not the textedit object but the textedit->setText() function. I just tried it without the clear but it crashes the same way.

        Maybe is something in the subclass? I can't even try to understand better the problem launching the Debugger engine because with it everything works.

        Can it be a problem of optimizations?

        1 Reply Last reply
        0
        • D davidesalvetti

          Hi,

          I'm having problem with a subclass of QTextEdit. First of all I had to subclass QTextEdit because I need the 'editingFinish' signal that is not supported by QTextEdit. But now I don't understand why this new class is causing the crash. I'll try to make you understand better with the code.

          qcustomtextedit.h

          class QCustomTextEdit : public QTextEdit
          {
              Q_OBJECT
          public:
              QCustomTextEdit( QWidget *parent = 0):QTextEdit(parent)
              {
          
              }
          
          signals:
              void editingFinished(QString);
          
          protected:
              void focusOutEvent(QFocusEvent *e)
              {
                  QTextEdit::focusOutEvent(e);
                  emit editingFinished(this->toPlainText());
              }
          };
          

          archivio.h

          #include "qcustomtextedit.h"
          
          namespace Ui {
          class Archivio;
          }
          
          class Archivio : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit Archivio(QWidget *parent = 0);
              ~Archivio();
          
          public slots:
          void on_PushButton_clicked();
          void textedit_editingFinished(QString);
          
          private:
              Ui::Archivio *ui;
              QCustomTextEdit             *textedit;
          
          };
          

          archivio.cpp

          Archivio::Archivio(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::Archivio)
          {
              ui->setupUi(this);
              setAttribute(Qt::WA_DeleteOnClose);                                     
              ArchivioAperto   = true;                                                 
          
              textedit = new QCustomTextEdit;
              textedit->setStyleSheet("*{background-color:white; color:black;}");
              QVBoxLayout *layout = new QVBoxLayout(ui->frame_3);
              layout->addWidget(textedit);
              layout->setSpacing(0);
              layout->setMargin(0);
              layout->setContentsMargins(0,0,0,0);
              ui->frame_3->setLayout(layout);
              connect(textedit, SIGNAL(editingFinished(QString)), this, SLOT(textedit_editingFinished(QString)));
          }
          void Archivio::on_PushButton_clicked()
          {
                  qDebug() << "HERE4";
                  QFile note("C:/Progetti/QT_Project"+ "/Note.txt");
                  if(note.open(QIODevice::ReadOnly))
                  {
                      qDebug() << "HERE5";
                      QByteArray baNote = note.readAll();
                      qDebug() << "HERE8";
                      textedit->setText(QString(baNote));//the program crashes here
                      note.close();
                      qDebug() << "HERE6";
                  }
                  else
                  {
          
                  }
          }
          
          void Archivio::textedit_editingFinished(QString message)
          {
              qDebug() << message;
                  QFile o_filenote("C:/Progetti/QT_Project"+ "/Note.txt");
                  o_filenote.remove();
                  QFile n_filenote("C:/Progetti/QT_Project" + "/Note.txt");
                  if(n_filenote.open(QIODevice::WriteOnly))
                  {
                      n_filenote.write(message.toLatin1());
                      n_filenote.close();
                  }
          }
          

          I cannot understand how the textedit object can be 0x0 after I have created the Archivio class.
          Have I made some mistake while creating the subclass?

          The interesting thing is that when I launch the debugger everything works fine, I have problems only if I don't launch the program from it.

          Thanks in advance!

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

          @davidesalvetti Hi,

          I don't think the code you show which can cause a crash.
          I have few remarks from your code:
          Don't set a parent to a QWidget or a QLayout when you put them in another layouts.

           textedit = new QCustomTextEdit;
           QVBoxLayout *layout = new QVBoxLayout;
          ...
          layout->addWidget(textedit); //layout becomes the parent of textedit
          ui->frame_3->setLayout(layout); //frame_3 becomes the parent of layout
          
          1. You need to call QWidget::focusOutEvent() in focusOutEvent(), otherwise your textedit will never lose focus
           void focusOutEvent(QFocusEvent *e)
              {
                  QWidget::focusOutEvent(e);
                  emit editingFinished(this->toPlainText());
              }
          
          D 1 Reply Last reply
          1
          • Gojir4G Gojir4

            @davidesalvetti Hi,

            I don't think the code you show which can cause a crash.
            I have few remarks from your code:
            Don't set a parent to a QWidget or a QLayout when you put them in another layouts.

             textedit = new QCustomTextEdit;
             QVBoxLayout *layout = new QVBoxLayout;
            ...
            layout->addWidget(textedit); //layout becomes the parent of textedit
            ui->frame_3->setLayout(layout); //frame_3 becomes the parent of layout
            
            1. You need to call QWidget::focusOutEvent() in focusOutEvent(), otherwise your textedit will never lose focus
             void focusOutEvent(QFocusEvent *e)
                {
                    QWidget::focusOutEvent(e);
                    emit editingFinished(this->toPlainText());
                }
            
            D Offline
            D Offline
            davidesalvetti
            wrote on last edited by
            #5

            @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 Gojir4G 2 Replies Last reply
            0
            • 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