I've added QDateEdit delegates to QTableView cells and want to update them with the date, but I can't figure out the correct connect syntax. dateChanged.connect(item.setText) doesn't work, because QDateEdit returns a QDate object, which needs needs to be converted to a string with toString, but I don't know where and how to squeeze it in the connect() statement.
This is what I have so far:
main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>239</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>341</width>
<height>231</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="1">
<widget class="QPushButton" name="btnPopulate">
<property name="text">
<string>Populate Table</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from PyQt5 import uic
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtWidgets import QDialog, QApplication, QDateEdit
from PyQt5.QtCore import QDate
class GUI(QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'main.ui'), self)
# button
self.btnPopulate.clicked.connect(self.populate)
# table model
self.header = ['col1', 'col2', 'col3']
self.QSModel = QStandardItemModel()
self.QSModel.setColumnCount(3)
self.QSModel.setHorizontalHeaderLabels(self.header)
self.tableView.setModel(self.QSModel)
def populate(self):
row = self.QSModel.rowCount()
for x in range(7):
self.QSModel.insertRow(row)
self.QSModel.setData(self.QSModel.index(row, 0), 'data')
self.QSModel.item(row, 0).setEditable(True)
self.QSModel.setData(self.QSModel.index(row, 1), 'data')
self.QSModel.item(row, 1).setEditable(True)
if x % 2 == 0:
self.QSModel.setData(self.QSModel.index(row, 2), '2018-01-06')
item = self.tableView.model().item(row, 2)
date = self.QSModel.data(self.QSModel.index(row, 2))
deDelegate = QDateEdit()
deDelegate.setCalendarPopup(True)
deDelegate.setDisplayFormat('yyyy-MM-dd')
myQDate = QDate.fromString(date, 'yyyy-MM-dd')
deDelegate.setDate(myQDate)
self.tableView.setIndexWidget(item.index(), deDelegate)
#deDelegate.dateChanged.connect(item.setText) doesn't work
else:
self.QSModel.setData(self.QSModel.index(row, 2), 'data')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
I've got the following questions:
What's the correct connect() syntax to achieve what I want.
AFAIK, yyyy-MM-dd is the same as Qt.ISODate format, but I can't figure out the proper import syntax. (I tried importing it from PyQt5.QtCore, but it didn't work.) If I could use Qt.ISODate instead of yyyy-MM-dd: what's the correct import syntax?
What's the correct connect() syntax to achieve what I want.
The dateChanged
signal returns an object of type QDate
, it is not a string so you can not assign it to setText()
, for this you must use toString()
:
deDelegate.dateChanged.connect(lambda date, it=item: it.setText(date.toString(Qt.ISODate)))
Or:
deDelegate.dateChanged.connect(lambda date, it=item: it.setText(date.toString("yyyy-MM-dd")))
AFAIK, yyyy-MM-dd is the same as Qt.ISODate format, but I can't figure out the proper import syntax. (I tried importing it from PyQt5.QtCore, but it didn't work.) If I could use Qt.ISODate instead of yyyy-MM-dd: what's the correct import syntax?
Qt
is part of QtCore
, so you must import it as follows: from PyQt5.QtCore import Qt
, example:
from PyQt5.QtCore import QDate, Qt
if __name__ == '__main__':
today = QDate.currentDate()
print(today.toString(Qt.ISODate))