PyQt5 -显示来自不同类别的QDialog。

时间:2021-09-22 23:01:04

My app consist of a QMainWindow with a QToolBar. My purpose is to click on a QToolBar element and open in a separate window (QDialog) a calendar.

我的应用程序包括一个QMainWindow和一个QToolBar。我的目的是单击一个QToolBar元素并在一个单独的窗口(QDialog)中打开日历。

I want to create in a separate class a QDialog and call it to be shown from the QMainWindow.

我想在一个单独的类中创建一个QDialog,并调用它从QMainWindow中显示。

This is my QDialog, just a Calendar:

这是我的QDialog,只是一个日历:

class CalendarDialog(QDialog):

    def __init__(self):
        super().__init__(self)
        cal = QCalendarWidget(self)            

Now from a QMainWindow I would like to show the calendar after an action trigger like next:

现在,从一个QMainWindow,我想在一个动作触发后显示日历,如下:

class Example(QMainWindow):
    ...
    calendarAction.triggered.connect(self.openCalendar)
    ...
    def openCalendar(self):
        self.calendarWidget = CalendarDialog(self)
        self.calendarWidget.show()

It isn't working. After the event that calls openCalendar the app is closed without printing any output error. I have put some prints to debug and CalendarDialog.__init__(self) isn't even called.

这不是工作。在调用openCalendar的事件之后,该应用程序关闭,而不会打印任何输出错误。我已经把一些打印出来的打印出来了。

The code regarding to QToolBar is as follow:

关于QToolBar的代码如下:

openCalendarAction = QAction(QIcon(IMG_CALENDAR), "", self)
openCalendarAction.triggered.connect(self.openCalendar)
self.toolbar.addAction(openCalendarAction)

1 个解决方案

#1


0  

The posted code seems almost correct, here a complete working example, I've added some resize to make the widget size "acceptable":

发布的代码看起来几乎是正确的,这里有一个完整的工作示例,我添加了一些调整大小以使小部件的大小“可以接受”:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class CalendarDialog(QDialog):

    def __init__(self, parent):
        super().__init__(parent)
        self.cal = QCalendarWidget(self)

        self.resize(300, 300)
        self.cal.resize(300, 300)

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.resize(400, 200)

        toolBar = QToolBar(self)

        calendarAction = QAction(QIcon('test.png'), 'Calendar', self)
        calendarAction.triggered.connect(self.openCalendar)
        toolBar.addAction(calendarAction)

    def openCalendar(self):
        self.calendarWidget = CalendarDialog(self)
        self.calendarWidget.show()


app = QApplication([])

ex = Example()
ex.show()

app.exec_()

#1


0  

The posted code seems almost correct, here a complete working example, I've added some resize to make the widget size "acceptable":

发布的代码看起来几乎是正确的,这里有一个完整的工作示例,我添加了一些调整大小以使小部件的大小“可以接受”:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class CalendarDialog(QDialog):

    def __init__(self, parent):
        super().__init__(parent)
        self.cal = QCalendarWidget(self)

        self.resize(300, 300)
        self.cal.resize(300, 300)

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.resize(400, 200)

        toolBar = QToolBar(self)

        calendarAction = QAction(QIcon('test.png'), 'Calendar', self)
        calendarAction.triggered.connect(self.openCalendar)
        toolBar.addAction(calendarAction)

    def openCalendar(self):
        self.calendarWidget = CalendarDialog(self)
        self.calendarWidget.show()


app = QApplication([])

ex = Example()
ex.show()

app.exec_()