我的應用由QMainWindow
與QToolBar
組成。我的目的是點擊一個QToolBar
元素並在一個單獨的窗口(QDialog
)中打開一個日曆。PyQt5 - 顯示不同類別的QDialog
我想創建一個單獨的類QDialog
並稱它從QMainWindow
顯示。
這是我QDialog
,只是一個日曆:
class CalendarDialog(QDialog):
def __init__(self):
super().__init__(self)
cal = QCalendarWidget(self)
現在從QMainWindow
我想顯示的動作觸發條件後,旁邊的日曆:
class Example(QMainWindow):
...
calendarAction.triggered.connect(self.openCalendar)
...
def openCalendar(self):
self.calendarWidget = CalendarDialog(self)
self.calendarWidget.show()
它不工作。在調用openCalendar
的事件之後,應用程序關閉而不打印任何輸出錯誤。我已經把一些打印調試,並且CalendarDialog.__init__(self)
甚至沒有被調用。
關於向QToolBar
的代碼如下:
openCalendarAction = QAction(QIcon(IMG_CALENDAR), "", self)
openCalendarAction.triggered.connect(self.openCalendar)
self.toolbar.addAction(openCalendarAction)
你不是在這行'self.calendarWidget = SMCalendarWidget(self)'創建'CalendarDialog','SMCalendarWidget'是否存在? – Ceppo93
是的,你是對的。這是一個轉錄錯誤。我已更正了代碼。 – user2607702
好的,你可以在「創建」toolBar時分享代碼嗎?提供的似乎幾乎是正確的,除了'CalendarDialog .__ init __(self)'不帶任何參數(self是隱含的),並且你用一個參數調用它'CalendarDialog(self)',可能你想指定一個'parent'參數'__init__'。 – Ceppo93