Python Forum
[PyQt] Whole application is closed by subwindows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Whole application is closed by subwindows
#1
Hello, I am almost done writing a sticky note program with PyQt. The main object (mother) is parent of the trayIcon and all notes windows (child) which are of type QWidget. I added a new gui to customize style (styleDialog), which is a QDialog. To the later I added the windowFlag Qt.SubWindow (see line 120) because else it finish the whole application when closed. But doing so, I cannot use the QColorDialog object from that window anymore (lines 151, 157), as it crash the application when closed.  I was wondering if it is a Qt bug, or rather a problem with the structure of the program itself. Especially, I was wondering if it is ok to have the mother as a regular object, and if it is usual to manually add Qt.SubWindow to children widgets. 

I tried adding various window flags and attributes on both QDialog and QColorDialog, but I could not get it to work. I would appreciate some help as this is the only detail left to finish that project! Also, I use this program nearly everyday, so I hope you will like it too!

The code is available on github: https://github.com/willbelr/qtpad/blob/master/qtpad.py

Thank you!
Reply
#2
I found the problem by trial and error. It turned out the application closed only when mother is not shown. The reason is that the default behavior is to quit the app when the last window close. As the mother class is used as a daemon, this line must be added to change that behavior:

app.setQuitOnLastWindowClosed(False)

The SubWindow flag is no longer necessary. It end up being a very easy fix, but I still suspect that there is something wrong with my structure. As I was trying to fix the problem I tried to explicitly assign parenting for all widgets, but when I do so they simply don't show. All widgets seems to require "super().__init__()" to be shown.

What am I doing wrong here?

#!/usr/bin/python3
import os
import sys
import time
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import Qt, QThread, QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QSystemTrayIcon
import gui_child, gui_style
LOCAL = os.path.dirname(os.path.realpath(__file__)) + '/'
ICON_DIR = os.path.dirname(os.path.realpath(__file__)) + '/icons/'

class styleDialog(QtWidgets.QDialog):
    def __init__(self, parent):
        super().__init__()
        #super(styleDialog, self).__init__(parent)
        print("styleDialog parent: " + str(self.parent()))
        #self.setWindowFlags(Qt.SubWindow)

        self.background = ""
        self.ui = gui_style.Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.backgroundOpt.clicked.connect(self.pickBackgroundColor)
        self.exec()

    def pickBackgroundColor(self):
        #cw = QtWidgets.QColorDialog()
        #cw.setParent(self)
        #color = cw.exec()
        color = QtWidgets.QColorDialog.getColor()
        if color:
            self.background = color.name()

#class mother(object):
#class mother(QtWidgets.QApplication):
class mother(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        #super().__init__()
        super(mother, self).__init__()
        print("mother parent: " + str(self.parent()))

        self.trayIcon = QSystemTrayIcon()
        self.trayIcon.activated.connect(self.pickColor)
        self.trayIcon.setIcon(QtGui.QIcon(ICON_DIR + "tray.svg"))
        self.trayIcon.show()
        self.children = {}
        self.children["example"] = child(self)

    def pickColor(self):
        style = styleDialog(self)
        if style.result():
            print(style.background)

class child(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__()
        #super(child, self).__init__(parent)
        print("child parent: " + str(self.parent()))
        #self.setWindowFlags(Qt.SubWindow | Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint)
        self.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint)

        self.ui = gui_child.Ui_Form()
        self.ui.setupUi(self)
        self.show()

if __name__== '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    main = mother()
    sys.exit(app.exec())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [closed] "checked" variable (attribute?) origin? paul18fr 4 362 Mar-05-2024, 04:20 PM
Last Post: deanhystad
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 3,701 Aug-23-2019, 09:01 PM
Last Post: woooee
  [WxPython] how to reopen a closed tab of wx.aui.Notebook? royer14 2 3,224 Feb-18-2019, 12:31 AM
Last Post: royer14
  [Tkinter] Displaying an image that can't be closed nicochulo 4 3,558 Feb-15-2018, 10:27 AM
Last Post: nicochulo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020