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. MainWindow::event crash when doing textEdit->append
QtWS25 Last Chance

MainWindow::event crash when doing textEdit->append

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 3.3k 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.
  • M Offline
    M Offline
    Maarten Verhage
    wrote on last edited by
    #1

    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:

    main.cpp

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

    mainwindow.h

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

    mainwindow.cpp

    #include "mainwindow.h"
    
    MainWindow::MainWindow()
    {
      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

    J.HilkJ jsulmJ 2 Replies Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • M Maarten Verhage

        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:

        main.cpp

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

        mainwindow.h

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

        mainwindow.cpp

        #include "mainwindow.h"
        
        MainWindow::MainWindow()
        {
          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

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

        @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:

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

        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.

        1 Reply Last reply
        2
        • M Maarten Verhage

          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:

          main.cpp

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

          mainwindow.h

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

          mainwindow.cpp

          #include "mainwindow.h"
          
          MainWindow::MainWindow()
          {
            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

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

          @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);
          }

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

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Maarten Verhage
            wrote on last edited by
            #5

            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\
            	mainwindow.o\
            	moc_mainwindow.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
            
            clean:
            	del *.o *.exe
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              Maarten Verhage
              wrote on last edited by Maarten Verhage
              #6

              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 \
              		main.cpp
              CONFIG += release
              

              then commands:

              qmake -makefile test.pro
              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

              A 1 Reply Last reply
              0
              • M Maarten Verhage

                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 \
                		main.cpp
                CONFIG += release
                

                then commands:

                qmake -makefile test.pro
                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

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

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

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                A 1 Reply Last reply
                1
                • A ambershark

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

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by ambershark
                  #8

                  @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/libQt5Widgets.so.5
                  (gdb) bt
                  #0  0x00007ffff788bad8 in QTextEdit::isReadOnly() const ()
                     from /usr/local/Qt/lib/libQt5Widgets.so.5
                  #1  0x00007ffff788c1e6 in QTextEdit::append(QString const&) ()
                     from /usr/local/Qt/lib/libQt5Widgets.so.5
                  #2  0x0000555555556980 in MainWindow::event(QEvent*) ()
                  #3  0x00007ffff76f9dcc in QApplicationPrivate::notify_helper(QObject*, QEvent*)
                      () from /usr/local/Qt/lib/libQt5Widgets.so.5
                  #4  0x00007ffff7701236 in QApplication::notify(QObject*, QEvent*) ()
                     from /usr/local/Qt/lib/libQt5Widgets.so.5
                  

                  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.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  5
                  • M Offline
                    M Offline
                    Maarten Verhage
                    wrote on last edited by
                    #9

                    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

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

                      Hi,

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

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

                      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