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
#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


Messages In This Thread
RE: QColorDialog Crash when called from a SubWindow - by Alfalfa - Oct-03-2017, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [closed] "checked" variable (attribute?) origin? paul18fr 4 561 Mar-05-2024, 04:20 PM
Last Post: deanhystad
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 3,831 Aug-23-2019, 09:01 PM
Last Post: woooee
  [WxPython] how to reopen a closed tab of wx.aui.Notebook? royer14 2 3,296 Feb-18-2019, 12:31 AM
Last Post: royer14
  [Tkinter] Displaying an image that can't be closed nicochulo 4 3,637 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