How to detect dialogs close event?

2024/10/4 15:39:28


Hi everyone.

I am making a GUI application using python3.4, PyQt5 in windows 7.

Application is very sample. User clicks a main window's button, information dialog pops up. And when a user clicks information dialog's close button (window's X button), system shows confirm message. This is all.

Here's my code.

# coding: utf-8import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabelclass mainClass(QMainWindow):def __init__(self):super().__init__()self.initUI()def initUI(self):openDlgBtn = QPushButton("openDlg", self)openDlgBtn.clicked.connect(self.openChildDialog)openDlgBtn.move(50, 50)self.setGeometry(100, 100, 200, 200)self.show()def openChildDialog(self):childDlg = QDialog(self)childDlgLabel = QLabel("Child dialog", childDlg)childDlg.resize(100, 100)childDlg.show()if __name__ == "__main__":app = QApplication(sys.argv)mc = mainClass()sys.exit(app.exec_())

Result screen shot is...

enter image description here

enter image description here

In this situation, I've added these code in mainClass class.

def closeEvent(self, event):print("X is clicked")

This code works only when the main window is closed. But what I want is closeEvent function works when childDlg is to closed. Not main window.

What should I do?

Answer

You have added, the method closeEvent in the class mainClass. So you have reimplemented the method closeEvent of your QMainwindow and not the method closeEvent of your childDlg. To do it, you have to subclass your chilDlg like this:

# coding: utf-8import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabelclass ChildDlg(QDialog):def closeEvent(self, event):print("X is clicked")class mainClass(QMainWindow):def __init__(self):super().__init__()self.initUI()def initUI(self):openDlgBtn = QPushButton("openDlg", self)openDlgBtn.clicked.connect(self.openChildDialog)openDlgBtn.move(50, 50)self.setGeometry(100, 100, 200, 200)self.show()def openChildDialog(self):childDlg = ChildDlg(self)childDlgLabel = QLabel("Child dialog", childDlg)childDlg.resize(100, 100)childDlg.show()if __name__ == "__main__":app = QApplication(sys.argv)mc = mainClass()sys.exit(app.exec_())
https://en.xdnf.cn/q/70595.html

Related Q&A

How to Make a Portable Jupyter Slideshow

How do I make a Jupyter slide show portable? I can serve the slideshow locally, but I cant send that to anyone and have it work with all the images, slide animation functionality, etc. I am using jupy…

How to animate a bar char being updated in Python

I want to create an animated, stacked bar chart.There is a great tutorial, which shows how to animate line graphs.However, for animating bar charts, the BarContainer object, does not have any attribute…

Add text to end of line without loading file

I need to store information into a very big file, in form of many dictionaries. Thats not so important, is just to say that I tried to first get all the data into these dictionaries and I run out of me…

How does one use `dis.dis` to analyze performance?

Im trying to use pythons dis library to experiment with & understand performance. Below is an experiment i tried, with the results.import disdef myfunc1(dictionary):t = tuple(dictionary.items())ret…

How do I require HTTPS for this Django view?

(r^login/?$,django.contrib.auth.views.login,{template_name:login.html, authentication_form:CustomAuthenticationForm}),How do I add HTTPS required to this? I usually have a decorator for it..But in th…

How many times a number appears in a numpy array

I need to find a way to count how many times each number from 0 to 9 appears in a random matrix created using np.random.randint()import numpy as np p = int(input("Length of matrix: ")) m = np…

python: How to remove values from 2 lists based on whats in 1 list

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements. Normally, I would jus…

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single columnIndex A Index B 0 A 0 NAN 1 NAN 1 D 2 B 2 …

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…