0

Hello I have the problem, that my Dialog does not open when I instantiate it in the constructor of a subclass. When I instantiate it in my main function and then call the show function everything works fine. Why does it not work when I instantiate it in the constructor of a subclass. There is no error. The Window does not pop to screen.

This is my gui class

from PyQt5.QtWidgets import QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QCheckBox, QDialog, QGridLayout, QHBoxLayout)


class TestWidget(QDialog):
    def __init__(self, parent=None):
        super(TestWidget, self).__init__(parent)
        disableWidgetsCheckBox = QCheckBox("&Disable widgets")
        topLayout = QHBoxLayout()
        topLayout.addWidget(disableWidgetsCheckBox)
        mainLayout = QGridLayout()
        mainLayout.addLayout(topLayout, 0, 0, 1, 2)
        mainLayout.setRowStretch(1, 1)
        mainLayout.setRowStretch(2, 1)
        mainLayout.setColumnStretch(0, 1)
        mainLayout.setColumnStretch(1, 1)
        self.setLayout(mainLayout)
        self.setWindowTitle("Styles")

class Gui():    
    def __init__(self, app):
        trayIcon = QSystemTrayIcon(QIcon('test.png'), parent=app)
        trayIcon.setToolTip('Check out my tray icon')
        trayIcon.show()
        menu = QMenu()
        exitAction = menu.addAction('Exit')
        exitAction.triggered.connect(app.quit)
        trayIcon.setContextMenu(menu)
        gallery = TestWidget()      # Here is the problem (line1)
        gallery.show()              # Here is the problem (line2)

This is my main.py:

import sys
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from gui import Gui
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QComboBox
from gui import TestWidget

app = QApplication(sys.argv)
gui = Gui(app)
# gallery = TestWidget()    # This would work
# gallery.show()            # This would work


sys.exit(app.exec_())
otto
  • 1,461
  • 3
  • 32
  • 54
  • 2
    In the first example the dialog is garbage collected as soon as the local variable `gallery` goes out of focus . To get around it you could create a more persistent reference to the dialog for example by assigning it to a instance variable, i.e. `self.gallery=TestWidget()` – Heike Jun 05 '21 at 11:11

0 Answers0