0

I am trying to write a simple code where a user picks a date, and the seleced date is displayed using the Qlabel function. However, I am making a mistake in transmitting the signal and wanted to ask if someone could help me with my code. Thank you.

from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.dateEdit = QtGui.QDateEdit(self)
        self.lbl = QtGui.QLabel()
        self.dateEdit.setDateTime(QtCore.QDateTime.currentDateTime())
        self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
        self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
        self.dateEdit.setCalendarPopup(True)

        layout = QGridLayout()
        layout.addWidget(self.dateEdit,0,0)
        layout.addWidget(self.lbl,0,1)
        self.setLayout(layout)


        self.connect(self.dateEdit, SIGNAL("dateChanged()"), self.updateUi)

    def updateUi(self):
        date1 = self.QtGui.QDateTimeEdit.QDate.date(self)
        self.lbl.setText(date1.toString())



if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())
Raptor776
  • 190
  • 1
  • 5
  • 15

2 Answers2

3

I recommend using the new style signal and slots, as it's easier to write and read:

self.dateEdit.dateChanged.connect(self.onDateChanged)

The definition of the signal is void dateChanged (const QDate&), with QDate being the new date. Therefore the slot should look like:

def onDateChanged(self,newDate):
    print("The new date is "+newDate.toString())

On a side note, you don't need to use the two import * (see Should wildcard import be avoided?). Just replace QGridLayout() by QtGui.QGridLayout() and the code will work without them.

Community
  • 1
  • 1
Mel
  • 5,837
  • 10
  • 37
  • 42
1

try the following code:

from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.dateEdit = QtGui.QDateEdit(self)
        self.lbl = QtGui.QLabel()
        self.dateEdit.setDateTime(QtCore.QDateTime.currentDateTime())
        self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
        self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
        self.dateEdit.setCalendarPopup(True)

        layout = QGridLayout()
        layout.addWidget(self.dateEdit,0,0)
        layout.addWidget(self.lbl,0,1)
        self.setLayout(layout)


        self.connect(self.dateEdit, SIGNAL("dateChanged(QDate)"), self.updateUi)

        # or new style:
        # self.dateEdit.dateChanged.connect(self.updateUi)

    def updateUi(self):
        date1 = self.dateEdit.date()
        self.lbl.setText(date1.toString())



if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())   
West
  • 86
  • 4
  • Thank you for the new code, I guess it was too much text before the date() line. The code works correctly now, thank you! – Raptor776 Dec 30 '15 at 15:46