Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:40:47+00:00 2026-06-12T03:40:47+00:00

I have created a custom QDialog class and overridden the closeEvent to just hide

  • 0

I have created a custom QDialog class and overridden the closeEvent to just hide the dialog as it is a child of another widget. My dialog must only close when its parent is closed and not when it’s accepted, rejected or the user clicks the close button.

This all works fine but now I need to open a connection to a database and only close it when the dialog is destroyed not just when it’s closed.

My code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

def Log_Closed():
    print "Bye bye"

class My_dlg(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__( self, parent )

        #self.conn = open_connection()
        print "Connection Opened"

        close_btn  = QPushButton("Actually Close")
        QVBoxLayout(self).addWidget(close_btn)

        close_btn.clicked.connect(self.Actually_Close)
        self.destroyed.connect(Log_Closed)

    def Actually_Close(self):
        print "Actually Close"
        self.parent().close()

    def closeEvent(self, event):
        if event.type() == QEvent.Close:
            event.ignore()
            self.hide()
            print "hidden"

    # And I guess I need something like
    def destroyEvent(self, event):
        #self.conn.close()
        print "Connection Closed"
        event.accept()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main= QMainWindow()
    tsd = My_dlg(main)
    tsd.show()
    sys.exit(app.exec_())

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:40:49+00:00Added an answer on June 12, 2026 at 3:40 am

    To receive notification when a QObject is deleted, connect to its destroyed(QObject*) signal.

    However, in Python, object deletion is less predictable than it is in C++ because objects are garbage collected. For example, see all of the caveats associated with __del__() in the Python documentation. They may not be deleted when the program exits, which is probably why you’re not receiving the signal.

    Instead of relying on deletion of the dialog, you could manage the database connection explicitly. In this simple example, you could even use a context manager for slightly nicer code.


    Either way, here’s a version of your code that behaves as you expect: https://gist.github.com/3827718

    The changes I made were:

    • Set app.setQuitOnLastWindowClosed to make sure that the application doesn’t quit when the dialog closes. I presume this is the behavior you want, because otherwise this question doesn’t make sense.

    • Set Qt.WA_DeleteOnClose to False on the dialog to prevent it from deleting itself when closed. This is preferable to overriding closeEvent.

    • In Actually_Close(), the dialog deletes itself (which will also close it). This triggers the destroyed signal.

    With this code, the output is as you expect when you click the button:

    Connection Opened
    Actually Close
    Bye bye
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom jQuery UI widget called uiPopover, very similar to UI-dialog (in
I have created a custom attribute that I am using on my class MyClass
I have created a custom class for my dates and need move dates forward
I have created custom workflow and successfully deployed it on Alfresco. I just copied
I have created custom exception class public class Web2PDFException : Exception { public Web2PDFException(string
I have created Custom Dialog for my application. While i run that application in
I have created custom result class to serialize json data to xml. I want
I have created a custom JTree. That tree could be filtered to show only
I have created a custom dialog and a layout xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout
I have created custom aplication resource: class Custom_Entitymanager extends Zend_Application_Resource_ResourceAbstract{ //... public function init

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.