Qt World Summit: Submit your Presentation

Subclass of QTextEdit makes the program to crash

  • 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.


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


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


    Archivio::Archivio(QWidget *parent) :
        ui(new Ui::Archivio)
        ArchivioAperto   = true;                                                 
        textedit = new QCustomTextEdit;
        textedit->setStyleSheet("*{background-color:white; color:black;}");
        QVBoxLayout *layout = new QVBoxLayout(ui->frame_3);
        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");
                qDebug() << "HERE5";
                QByteArray baNote = note.readAll();
                qDebug() << "HERE8";
                textedit->setText(QString(baNote));//the program crashes here
                qDebug() << "HERE6";
    void Archivio::textedit_editingFinished(QString message)
        qDebug() << message;
            QFile o_filenote("C:/Progetti/QT_Project"+ "/Note.txt");
            QFile n_filenote("C:/Progetti/QT_Project" + "/Note.txt");

    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!

  • Lifetime Qt Champion


    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.

  • @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?

  • @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)
            emit editingFinished(this->toPlainText());

  • @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?

  • Lifetime Qt Champion

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


    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.

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


    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.

  • @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.

  • 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

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


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

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

    But these message are not shown in the application output.

  • Moderators


    create your Pointer with a nullptr set

        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.

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

  • @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.

  • Moderators

    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?

  • @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.

  • @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.

Log in to reply