[PySide] Setting QDate to blank value

1,633 views
Skip to first unread message

Stuart McGraw

unread,
Aug 2, 2011, 9:45:55 AM8/2/11
to pys...@lists.pyside.org
Hello,

Sorry for is probably really dumb, simple question,
but I've just started with Pyside/Qt. I'm rewriting
a small wxPython app in Pyside and am stuck on this
point. (I am going through Summerfield's book and
using the Pyside/Qt docs but have not seen an answer
although I could easily have missed it.)

My app gets data from a RDMS and thus many data items
may be null rather than a domain value.
For example a date field may be <2011-08-01> or null.
I expected a null date (which is represented as a Python
None within my program) could be used to set a QDate
widget and would result in a blank field being shown
but
my_date_widget.setDate (None)
produces a TypeError.

The Qt docs are not clear (to me) but I think say that
a null QDate is the same as an invalid QDate so I tried
the following
qdateNull = QtCore.QDate (0, 0, 0) # An invalid QDate object.
# 'due' is a Python datetime.date object or None.
if not due:
self.due.setDate (qdateNull)
else:
self.due.setDate (Qdate (due.year, due.month, due.day))
However, the "Due" field on the form shows "2000-01-01"
when the value of 'due' is None.

How the heck do I get a Qdate field to be blank when the
corresponding date value is None?

I am using Pyside 1.0.2 as that is what is currently
provided with Fedora-15. My question is not restricted
to QDate objects -- the same problem arises with any
object other than ones with text values (where I can
substitute "" for None) so I'd like to know the general
way of dealing with this issue.

Thanks.

_______________________________________________
PySide mailing list
PyS...@lists.pyside.org
http://lists.pyside.org/listinfo/pyside

Lauro Moura

unread,
Aug 2, 2011, 10:45:08 AM8/2/11
to Stuart McGraw, pys...@lists.pyside.org

You can use QDate(), which builds an invalid QDate().

Converting None to QDate() "under the hoods" could be implemented
kinda easily. What do you guys think?

--
Lauro Moura
INdT - Instituto Nokia de Tecnologia

Stuart McGraw

unread,
Aug 2, 2011, 2:34:31 PM8/2/11
to pys...@lists.pyside.org

Thanks for that info. It seems the problem is
not so much with QDate but with QDateEdit, since
the following script still shows "2000-01-01" in
the QDateEdit widget:

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

class Form (QDialog):
def __init__ (self, parent=None):
super (Form, self).__init__ (parent)
de = QDateEdit()
de.setDate (QDate())
lo = QVBoxLayout()
lo.addWidget (de)
self.setLayout (lo)

if __name__ == '__main__':
app = QApplication (sys.argv)
frame = Form()
frame.show()
app.exec_()

Someone pointed me to
https://bugreports.qt.nokia.com//browse/QTBUG-277

Sadly, this is a show-stopper for me.

Tiziano Lattisi

unread,
Aug 2, 2011, 2:51:46 PM8/2/11
to Stuart McGraw, pys...@lists.pyside.org
2011/8/2 Stuart McGraw <smcg...@frii.com>:

Actually isn't possible to set a null value to QDateEdit.
A workaround may be a custom widget with a QLineEdit and
a popup calendar, something like that
http://darcs.pypapi.org/cgi-bin/darcsweb.cgi/darcsweb.cgi?r=pypapi;a=headblob;f=/lib/pypapi/ui/widgets/nullabledateedit.py

Other better solutions?

Tiziano

Reply all
Reply to author
Forward
0 new messages