Search code examples
pythonpython-3.xpyqtpyqt5qdateedit

How do I use the user value set in the in QDateEdit widget in another function?


I'm building a GUI using PyQT5 for some scripts I run which requires a user date input.

I've managed to get to a point where I can select a date using the DateEdit range, and have new date printed to the console every time the user changes it.

What I need to do is use whatever date is in the QDateEdit widget in a function when I click the Run button.

Here's my sample code

import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
        QMenu, QPushButton, QRadioButton, QVBoxLayout, QWidget, QFrame, QDateEdit)

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowIcon(QtGui.QIcon('logo.png'))
        self.setWindowTitle("Test")
        self.resize(400, 300)

        grid = QGridLayout()
        grid.addWidget(self.group1(), 0, 0)
        self.setLayout(grid)



    def group1(self):
        groupBox = QGroupBox("Box 1")

        date = QDate.currentDate().addDays(-1)
        dateSelect = QDateEdit()
        dateSelect.setDate(date)
        dateSelect.dateChanged.connect(self.onDateChanged)

        checkbox1 = QCheckBox("Task 1")
        checkbox2 = QCheckBox("Task 2")
        checkbox3 = QCheckBox("Task 3")

        button1 = QPushButton('Run')
        button1.setMaximumWidth(75)
        button1.clicked.connect(self.btn1_onClicked)

        button2 = QPushButton('Run')
        button2.setMaximumWidth(75)

        separatorLine = QFrame()
        separatorLine.setFrameShape(QFrame.HLine)
        separatorLine.setFrameShadow(QFrame.Sunken)

        vbox = QVBoxLayout()
        vbox.addWidget(dateSelect)
        vbox.addWidget(checkbox1)
        vbox.addWidget(checkbox2)
        vbox.addWidget(button1)
        vbox.addWidget(separatorLine)
        vbox.addWidget(checkbox3)
        vbox.addWidget(button2)
        vbox.addStretch(1)
        groupBox.setLayout(vbox)

        return groupBox


    def btn1_onClicked(self, ):
        date = onDateChanged()


    def onDateChanged(self,newDate):
        date = newDate.toString('yyyy-MM-dd')
        print(date)
        return date


if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = Window()
    clock.show()
    sys.exit(app.exec_())

Solution

  • QDateEdit notifies the change of the date through the dateChanged signal, but in your case you do not want the notification but the current date and for this you must use the date() method.

    import sys
    from PyQt5.QtCore import Qt, QDate
    from PyQt5.QtGui import QIcon
    from PyQt5.QtWidgets import (
        QApplication,
        QCheckBox,
        QGridLayout,
        QGroupBox,
        QPushButton,
        QVBoxLayout,
        QWidget,
        QFrame,
        QDateEdit,
    )
    
    
    class Window(QWidget):
        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
            self.setWindowIcon(QIcon("logo.png"))
            self.setWindowTitle("Test")
            self.resize(400, 300)
    
            grid = QGridLayout(self)
            grid.addWidget(self.group1(), 0, 0)
    
        def group1(self):
            groupBox = QGroupBox("Box 1")
    
            date = QDate.currentDate().addDays(-1)
            self.dateSelect = QDateEdit()
            self.dateSelect.setDate(date)
            self.dateSelect.dateChanged.connect(self.onDateChanged)
    
            checkbox1 = QCheckBox("Task 1")
            checkbox2 = QCheckBox("Task 2")
            checkbox3 = QCheckBox("Task 3")
    
            button1 = QPushButton("Run")
            button1.setMaximumWidth(75)
            button1.clicked.connect(self.btn1_onClicked)
    
            button2 = QPushButton("Run")
            button2.setMaximumWidth(75)
    
            separatorLine = QFrame(frameShape=QFrame.HLine, frameShadow=QFrame.Sunken)
    
            vbox = QVBoxLayout()
            vbox.addWidget(self.dateSelect)
            vbox.addWidget(checkbox1)
            vbox.addWidget(checkbox2)
            vbox.addWidget(button1)
            vbox.addWidget(separatorLine)
            vbox.addWidget(checkbox3)
            vbox.addWidget(button2)
            vbox.addStretch(1)
            groupBox.setLayout(vbox)
    
            return groupBox
    
        def btn1_onClicked(self,):
            print(self.dateSelect.date().toString("yyyy-MM-dd"))
    
        def onDateChanged(self, newDate):
            date = newDate.toString("yyyy-MM-dd")
            print(date)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        clock = Window()
        clock.show()
        sys.exit(app.exec_())