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. Program with copy-paste when closeEvent with exit(0) cause program crash
Forum Update on Monday, May 27th 2025

Program with copy-paste when closeEvent with exit(0) cause program crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 2.6k 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.
  • V Offline
    V Offline
    vitasr
    wrote on last edited by A Former User
    #1

    @joeQ,Thanks!!!
    In my program,I put two QLineEdit in my MainWindow,if I do copy and paste between the two widgets,when I click the "X" and enter the closeEvent where I have the function exit(0) when I accept the quit process,just as these below:

    void MainWindow::closeEvent(QCloseEvent *event)
    {
        QMessageBox warningBox(QMessageBox::Warning,tr("Exit"),("Are you sure to exit?"),QMessageBox::Yes | QMessageBox::No);
        if(QMessageBox::No == warningBox.exec() )
        {
            event->ignore();
        }
        else
        {
            event->accept();
    
            exit(0);
    
            //QCoreApplication::exit(0);
            //QApplication::exit(0);
        }
    }
    

    and the program carshed ,just as below:

    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 130c948. Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008", file kernel\qcoreapplication.cpp, line 541
    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
    

    0_1513069960145_picture1.png

    JonBJ joeQJ 2 Replies Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

      Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008

      Are you creating widgets in non-gui threads?

      http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

      "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

      V 1 Reply Last reply
      0
      • V vitasr

        @joeQ,Thanks!!!
        In my program,I put two QLineEdit in my MainWindow,if I do copy and paste between the two widgets,when I click the "X" and enter the closeEvent where I have the function exit(0) when I accept the quit process,just as these below:

        void MainWindow::closeEvent(QCloseEvent *event)
        {
            QMessageBox warningBox(QMessageBox::Warning,tr("Exit"),("Are you sure to exit?"),QMessageBox::Yes | QMessageBox::No);
            if(QMessageBox::No == warningBox.exec() )
            {
                event->ignore();
            }
            else
            {
                event->accept();
        
                exit(0);
        
                //QCoreApplication::exit(0);
                //QApplication::exit(0);
            }
        }
        

        and the program carshed ,just as below:

        ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
        ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 130c948. Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008", file kernel\qcoreapplication.cpp, line 541
        ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
        ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
        

        0_1513069960145_picture1.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @vitasr
        I don't know if it's to do with your crash --- which seems to be about different GUI thread --- but why are you explicitly exiting the app in the handler for main window's close event? Don't you just want to let Qt exit the main event loop when it gets the close (after your message box)?

        P.S.
        If you do choose to exit somewhere in your app, for whatever reason, I'm pretty directly sure using exit() is not a good idea. You should at least use one of the Qt-level "quit"s, to allow it a proper chance to clean up, e.g. see https://stackoverflow.com/questions/8026101/correct-way-to-quit-a-qt-program.

        1 Reply Last reply
        3
        • Vinod KuntojiV Offline
          Vinod KuntojiV Offline
          Vinod Kuntoji
          wrote on last edited by
          #4

          @vitasr ,

          In close event, close the thread first, then close the application.

          C++, Qt, Qt Quick Developer,
          PthinkS, Bangalore

          1 Reply Last reply
          0
          • V vitasr

            @joeQ,Thanks!!!
            In my program,I put two QLineEdit in my MainWindow,if I do copy and paste between the two widgets,when I click the "X" and enter the closeEvent where I have the function exit(0) when I accept the quit process,just as these below:

            void MainWindow::closeEvent(QCloseEvent *event)
            {
                QMessageBox warningBox(QMessageBox::Warning,tr("Exit"),("Are you sure to exit?"),QMessageBox::Yes | QMessageBox::No);
                if(QMessageBox::No == warningBox.exec() )
                {
                    event->ignore();
                }
                else
                {
                    event->accept();
            
                    exit(0);
            
                    //QCoreApplication::exit(0);
                    //QApplication::exit(0);
                }
            }
            

            and the program carshed ,just as below:

            ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
            ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 130c948. Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008", file kernel\qcoreapplication.cpp, line 541
            ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
            ASSERT: "q != 0" in file kernel\qeventdispatcher_win.cpp, line 524
            

            0_1513069960145_picture1.png

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @vitasr Hi,friend.

            Did you work it out?

            Just do it!

            V 1 Reply Last reply
            0
            • VRoninV VRonin

              @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

              Receiver 'lineEdit' (of type 'QLineEdit') was created in thread 13c9008

              Are you creating widgets in non-gui threads?

              http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

              V Offline
              V Offline
              vitasr
              wrote on last edited by
              #6

              @VRonin Thanks,all the work I do in the GUI thread,and I find that if I use qCoreQApplication(0),it works well,but if I used exit(0),It crashed.This only happened when I do copy-paste operation in my program,if I donot do copy-paste,exit(0) also works well,so this is the problem

              1 Reply Last reply
              0
              • joeQJ joeQ

                @vitasr Hi,friend.

                Did you work it out?

                V Offline
                V Offline
                vitasr
                wrote on last edited by joeQ
                #7

                @joeQ Thanks!

                Chinese:

                我的英文很不好,所以,和你对话就采用汉语了,抱歉!
                我发现,在closeEvent()函数中如果我调用QCoreApplication(0)取代exit(0)就不会有问题。
                但是:如果我在我的程序中不进行控件之间的赋值黏贴操作,exit(0)也可以正常工作。否则exit(0)就会出现问题。报错原因好像是sendEvent不能跨线程之类的,但我还是不太明白到底是什么原因。

                English:

                In closeEvent(), if i used QCoreApplication::quit(0), it will be Ok. But use exit(0) it crash.

                It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

                The error message like :sendEvent can not cross-thread. what's mean?

                kshegunovK 1 Reply Last reply
                0
                • V vitasr

                  @joeQ Thanks!

                  Chinese:

                  我的英文很不好,所以,和你对话就采用汉语了,抱歉!
                  我发现,在closeEvent()函数中如果我调用QCoreApplication(0)取代exit(0)就不会有问题。
                  但是:如果我在我的程序中不进行控件之间的赋值黏贴操作,exit(0)也可以正常工作。否则exit(0)就会出现问题。报错原因好像是sendEvent不能跨线程之类的,但我还是不太明白到底是什么原因。

                  English:

                  In closeEvent(), if i used QCoreApplication::quit(0), it will be Ok. But use exit(0) it crash.

                  It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

                  The error message like :sendEvent can not cross-thread. what's mean?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

                  It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

                  You're forcefully killing the application and then wonder why it crashes the way down ... just don't use ::exit!

                  Read and abide by the Qt Code of Conduct

                  JonBJ 1 Reply Last reply
                  2
                  • kshegunovK kshegunov

                    @vitasr said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

                    It also be Ok when i had not any copy and paste operates between widgets that use exit(0).

                    You're forcefully killing the application and then wonder why it crashes the way down ... just don't use ::exit!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @kshegunov
                    I suggested that to OP above, but ...

                    kshegunovK 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @kshegunov
                      I suggested that to OP above, but ...

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @JNBarchan said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

                      I suggested that to OP above, but ...

                      Granted, but you weren't that explicit. (S)he should not use ::exit at all unless handling a SIGSEGV or another signal in a handler.

                      Read and abide by the Qt Code of Conduct

                      JonBJ 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @JNBarchan said in QT program with copy-paste when closeEvent with exit(0) cause program crash:

                        I suggested that to OP above, but ...

                        Granted, but you weren't that explicit. (S)he should not use ::exit at all unless handling a SIGSEGV or another signal in a handler.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @kshegunov
                        I wrote:

                        If you do choose to exit somewhere in your app, for whatever reason, I'm pretty directly sure using exit() is not a good idea. You should at least use one of the Qt-level "quit"s ...

                        Oh, OK! Next time I'll be more forceful ;-)

                        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