【问题标题】:PyQt5 accessing data from child widget to be printed on parent windowPyQt5 从子小部件访问数据以打印在父窗口上
【发布时间】:2019-10-18 19:27:53
【问题描述】:

我需要帮助从 OpenSelectDateTimeWindow 小部件中的函数 userChosenDate() 访问值。在单击来自我的OpenSelectDateTimeWindow 子小部件的pushButtonConfirm 之后,我需要将此值打印在我的主窗口上的 QLabel 对象printCurrentDate 上。我已设法打印出从子小部件中的日历小部件中选择的默认选定日期,但我无法弄清楚如何在从我的子小部件中选择另一个日期后让文本自行更新。

小部件和主窗口的 UI 都是从 QtDesigner 生成的,代码未受影响。这些是我添加的用于实例化 UI 的以下代码。

这是小部件的代码:

class OpenSelectDateTimeWindow(QtWidgets.QWidget, Ui_SelectDateTime):
    # initialise GUI and window
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        # when Back button is clicked, window is closed
        self.pushButtonBack.clicked.connect(self.close)
        # when Confirm button is clicked, return day chosen by the user (print date, followed by day)
        self.pushButtonConfirm.clicked.connect(self.userChosenDate)
        self.pushButtonConfirm.clicked.connect(self.close)

    # 2 functions to return selected date and time on the
    # MainWindow UI / New Window UI where stall's information
    # will be printed according to the user chosen date and time

    # this function is to return user chosen date from the calendarWidget widget
    def userChosenDate(self):
        self.date = self.calendarWidget.selectedDate().toString("dd-MM-yyyy, dddd")
        print(self.date)  # for checking in terminal
        return self.date

这是我的主窗口的代码。

class OpenMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.setDateAndTimeButton.clicked.connect(self.DateTimeWindow)

    def DateTimeWindow(self):
        self.SelectDateTimeWindow = OpenSelectDateTimeWindow()
        # initialise GUI and window
        self.SelectDateTimeWindow.show()
        self.printCurrentDate.setText(self.SelectDateTimeWindow.userChosenDate())
        ################need help here###########################
        #while self.SelectDateTimeWindow.pushButtonConfirm.clicked():     
            #dateString = self.SelectDateTimeWindow.userChosenDate()
            #self.printCurrentDate.setText(dateString)

【问题讨论】:

  • data = self.SelectDateTimeWindow.userChosenDate()
  • 非常感谢您的帮助!但是,这会导致在打开 SelectDateTimeWindow 后将日期打印在 textLabel 上,并且在我单击窗口的“确认”按钮后日期不会改变,这是我想要实现的。
  • @luminoustan 更改OpenSelectDateTimeWindow,使其继承自QDialog(在Qt Designer 以及您的代码中)。然后你可以做self.SelectDateTimeWindow.exec(),它会一直阻塞直到用户关闭对话框。您还应该将按钮连接更改为self.pushButtonBack.clicked.connect(self.reject)self.pushButtonConfirm.clicked.connect(self.accept)exec() 调用返回一个Dialog Code,可用于判断用户是否确认/接受。
  • 好的,我试试,非常感谢!!

标签: python-3.x user-interface pyqt pyqt5


【解决方案1】:

我发现在我设置文本的函数上方添加@pyqtSlot() 将允许我想要工作。虽然我希望有人能解释 @pyqtSlot() 的真正作用,因为我不太熟悉它。

【讨论】:

  • 我认为你错了——它并不能真正解决你的问题。
  • Hmm.. 我设法通过创建此函数在主窗口中打印它``` @pyqtSlot() def ConfirmClicked(self): self.printCurrentDate.setText(self.SelectDateTimeWindow.userChosenDate() ) self.printCurrentDate.adjustSize() ``` 然后用 DateTimeWindow 函数中的最后一行替换这个函数
  • pyqtSlot 虽然声明为不必要,但实际上是必要的,如果您不希望偶尔出现隐藏的问题 - 这里的关键是任何使用 .connect(functionname) 的东西都在实现与有问题的对象和每个信号都应该由插槽接收,以防止发生怪癖。所以是的,只要实现正确的@pyqtSlot() 实际上就足以解决一些问题
  • 但请记住,它必须是正确的@pyqtSlot( ),这可能意味着它需要添加可变参数,例如@pyqtSlot(int),表明它接收到一个带有发送的整数值的信号它或@pyqtSlot(str)@pyqtSlot(object)@pyqtSlot(int, str) 或等等...
猜你喜欢
  • 2020-01-29
  • 1970-01-01
  • 2020-10-17
  • 2010-09-26
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多