Important: Please read the Qt Code of Conduct -

MainWindow::event crash when doing textEdit->append

  • Dear Qt experts,

    I appreciate the people who are helping everyone out here with Qt development issues. That's so nice!

    I'm using the static Qt 5.3.2 version for Windows 7 SP1, 64 bit. My compiler is GCC 5.1.0.

    I have a very small Qt program with just a QTextEdit as the central widget. If I reimplement for example the QMainWindow::enterEvent function I can write something to my textEdit widget. But when I reimplement just the event function. Then the program crashes without even displaying the mainwindow briefly. I got an exception code c0000005 then from Windows.

    here are my files:


    #include <QApplication>
    #include <QtPlugin>
    #include "mainwindow.h"
    int main( int argc, char *argv[] )
      QApplication app( argc, argv );
      MainWindow window;
      window.resize(500, 400);;
      return app.exec();


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QTextEdit>
    class MainWindow : public QMainWindow
        explicit MainWindow();
        void enterEvent( QEvent* /* event */ );
        bool event( QEvent* /* event */ );
        QTextEdit* text;


    #include "mainwindow.h"
      text = new QTextEdit( this );
      text->setReadOnly( true );
      text->setMinimumSize( 400, 300 );
      setCentralWidget( text );
      text->append( tr("Initial textedit contents ") );
    void MainWindow::enterEvent( QEvent* event )
      text->append( tr("enterEvent ") ); // this works fine
      QMainWindow::enterEvent( event );
    bool MainWindow::event( QEvent* event )
      //text->append( tr("Event ") ); // this line crashes the program
      return QMainWindow::event(event);

    Am I doing anything wrong here? Would it be possible somehow to append something to the text edit from the event function?

    Best regards,
    Maarten Verhage

  • Qt Champions 2017

    event() function is called repeatedly and finally it crashes. You can put debug an d check in your event() function.

  • Moderators

    @Maarten-Verhage to extend a bit on what @dheerendra said,

    bool MainWindow::event( QEvent* event ) catches all the events, and I believe some actually fire before the initialisation of QMainWindow is finished.

    Try to init the text pointer with 0 and check against it:

        QTextEdit* text = Q_NULLPTR;
    bool MainWindow::event( QEvent* event )
          text->append( tr("Event ") ); 
      return QMainWindow::event(event);

  • Lifetime Qt Champion

    @Maarten-Verhage Strange. Could be that text is not yet initialized when you get first even (it should actually not be the case). You can try:

    // In class MainWindow
    QTextEdit* text = nullptr; // Requires C++11
    bool MainWindow::event( QEvent* event )
      if (text != nullptr)
          text->append( tr("Event ") ); // this line crashes the program
      return QMainWindow::event(event);

  • Dear people,

    Thanks for the suggestions. I tried suggestion from @J-Hilk and @jsulm, both resulted in a mainwindow that actually displays, but immediately crashes as well but now with exception code c00000fd. A quick google tells me that is a stack overflow.

    Putting a virtual keyword in front of my event function in the header like QObject does neither helps.

    I might be useful to see my makefile. Am I missing some compiler flag maybe?

    FLAGS = -std=c++14 -pedantic -Wextra -Wlogical-op
    BINPATHQT = $(QT)\qtbase\bin
    INCPATHQT = -I"$(QT)\qtbase\include" -I"$(QT)\qtbase\include\qtcore" -I"$(QT)\qtbase\include\qtgui" -I"$(QT)\qtbase\include\qtwidgets"
    LIBPATHQT = -L"$(QT)\qtbase\lib" -L"$(QT)\qtbase\plugins\platforms"
    LIBSQT = -l:libqwindows.a -l:libQt5PlatformSupport.a\
    	-l:libQt5Widgets.a -l:libQt5Gui.a -l:libQt5Core.a
    LIBSWIN = -l:libkernel32.a -l:libuser32.a -l:libshell32.a -l:libadvapi32.a\
    	-l:libws2_32.a -l:liboleaut32.a -l:libimm32.a -l:libwinmm.a -l:libole32.a\
    	-l:libuuid.a -l:libopengl32.a -l:libole32.a -l:libgdi32.a
    OBJFILES = main.o\
    all: test.exe
    test.exe : $(OBJFILES)
    	g++ -static -mwindows $(LIBPATHQT) $(OBJFILES) $(LIBSQT) $(LIBSWIN) -o test.exe
    main.o : main.cpp
    	g++ -c $(INCPATHQT) $(FLAGS) main.cpp
    mainwindow.o : mainwindow.cpp
    	g++ -c $(INCPATHQT) $(FLAGS) mainwindow.cpp
    moc_mainwindow.o : moc_mainwindow.cpp
    	g++ -c $(INCPATHQT) $(FLAGS) moc_mainwindow.cpp
    moc_mainwindow.cpp : mainwindow.h
    	$(BINPATHQT)\moc mainwindow.h -o moc_mainwindow.cpp
    	del *.o *.exe

  • Dear people,

    Now I also tried to build it the Qt way. i.e. with a pro file:

    QT += widgets
    HEADERS	    =	mainwindow.h
    SOURCES	    =	mainwindow.cpp \
    CONFIG += release

    then commands:

    qmake -makefile
    mingw32-make -f makefile

    Still the same c00000fd crash. I hope somebody can help me out on this one.

    Oh yeah, CONFIG += debug neither works.
    Regards, Maarten

  • Moderators

    @Maarten-Verhage What is the stack trace from the crash? This should give us a lot of insight as to what is happening.

    It definitely sounds like you are using the text object before it is fully ready.

    You can always add a variable that lets you know when the initialization is complete and it's ok to use the text window.

  • Moderators

    @ambershark I ran it for you and put up a backtrace. It is definitely what everyone thought. The QTextEdit is not ready when you try to append to it.

    Thread 1 "maincrash" received signal SIGSEGV, Segmentation fault.
    0x00007ffff788bad8 in QTextEdit::isReadOnly() const ()
       from /usr/local/Qt/lib/
    (gdb) bt
    #0  0x00007ffff788bad8 in QTextEdit::isReadOnly() const ()
       from /usr/local/Qt/lib/
    #1  0x00007ffff788c1e6 in QTextEdit::append(QString const&) ()
       from /usr/local/Qt/lib/
    #2  0x0000555555556980 in MainWindow::event(QEvent*) ()
    #3  0x00007ffff76f9dcc in QApplicationPrivate::notify_helper(QObject*, QEvent*)
        () from /usr/local/Qt/lib/
    #4  0x00007ffff7701236 in QApplication::notify(QObject*, QEvent*) ()
       from /usr/local/Qt/lib/

    I added a debug message to see the event item and here is the output:

    [shockwave] ~/tmp/maincrash > ./maincrash 
    0x7ffc69b631f0 QEvent::Type(ChildAdded)
    Segmentation fault (core dumped)

    As you can see it is a ChildAdded event, more than likely the one adding the textedit to the mainwindow. Since it's not ready you can't update it.

  • Ok, the first version had the issue that event was called before the text object was fully ready. Now with the check for the nullptr or Q_NULLPTR that is resolved. Then I ran into the issue of the exception c00000fd AKA stack overflow. You know what I think, text->append generates an event on it's own leading to repeated calls to the event function and ultimately causing stack overflow.

    I will check this later today. Maybe by ignoring the event that is caused by the append function.

    Regards, Maarten

  • Lifetime Qt Champion


    Since it's to show a default text at startup, rather than append, what about using setText ?

Log in to reply