Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why is closeEvent() not invoked for my QWidget ?
Qt 6.12 LTS is out! See what's new in the release blog

Why is closeEvent() not invoked for my QWidget ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.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.
  • T Offline
    T Offline
    tamirmichael
    wrote on last edited by tamirmichael
    #1

    Hello all and thanks for reading this post,

    I recently started using Qt for desktop application development using VS2017.
    I am creating a class that derives from QWidget, but the closeEvent() callback of that class is not called once click on the "x" of the newly created window (it is called for the main window class). To be clear: I have a main window that spawns an additional widget, whose class does not respond to closeEvent().
    My code is posted below.
    Thanks in advance!

    // this code is executed by the main window class thread to create the new widget/window
    
    widget_ptr = new QWidget(parent_ptr) ;
    
    vertical_layout_ptr = new QVBoxLayout(widget_ptr) ;
    vertical_layout_ptr->setSpacing(6) ;
    vertical_layout_ptr->setContentsMargins(16, 16, 16, 16) ;
    
    custom_plot_ptr = new QCustomPlot(widget_ptr) ;
    
    vertical_layout_ptr->addWidget(custom_plot_ptr) ;
    
    QSize size(600, 300) ;
    widget_ptr->resize(size) ;
    widget_ptr->setWindowFlag(Qt::Window) ;
    
    QSizePolicy size_policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding) ;
    widget_ptr->setSizePolicy(size_policy) ;
    
    widget_ptr->show() ;
    
    J.HilkJ 1 Reply Last reply
    0
    • T tamirmichael

      Hello all and thanks for reading this post,

      I recently started using Qt for desktop application development using VS2017.
      I am creating a class that derives from QWidget, but the closeEvent() callback of that class is not called once click on the "x" of the newly created window (it is called for the main window class). To be clear: I have a main window that spawns an additional widget, whose class does not respond to closeEvent().
      My code is posted below.
      Thanks in advance!

      // this code is executed by the main window class thread to create the new widget/window
      
      widget_ptr = new QWidget(parent_ptr) ;
      
      vertical_layout_ptr = new QVBoxLayout(widget_ptr) ;
      vertical_layout_ptr->setSpacing(6) ;
      vertical_layout_ptr->setContentsMargins(16, 16, 16, 16) ;
      
      custom_plot_ptr = new QCustomPlot(widget_ptr) ;
      
      vertical_layout_ptr->addWidget(custom_plot_ptr) ;
      
      QSize size(600, 300) ;
      widget_ptr->resize(size) ;
      widget_ptr->setWindowFlag(Qt::Window) ;
      
      QSizePolicy size_policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding) ;
      widget_ptr->setSizePolicy(size_policy) ;
      
      widget_ptr->show() ;
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @tamirmichael and welcome.

      you may have overwritten the closeEvent, but from what you posted, you create a default QWidget.

      You will have to use an instance of your modified class


      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.

      T 1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        hi @tamirmichael and welcome.

        you may have overwritten the closeEvent, but from what you posted, you create a default QWidget.

        You will have to use an instance of your modified class

        T Offline
        T Offline
        tamirmichael
        wrote on last edited by tamirmichael
        #3

        @J.Hilk

        Thanks for responding. That makes perfect sense, I may have missed there!
        I will to correct this once home, will post the results here.
        Thanks again.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tamirmichael
          wrote on last edited by
          #4

          Yes that was it. How could I not have seen that :-)

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