Re: [PyQt] QPyNullVariant

2012-07-26 Thread Wilbert Berendsen
Op Mon, 16 Jul 2012 12:11:11 +0100
Phil Thompson p...@riverbankcomputing.com schreef:

 On Fri, 13 Jul 2012 15:49:48 +0200, Rodolfo Zitellini
 xhero...@gmail.com wrote:
  The offending code is in setup.py, when it reads the language:
  language = QSettings().value(language, )
  print language
  if not language:
  try:
  language = locale.getdefaultlocale()[0]
  except ValueError:
  pass
  if not language:
  language = C
  return language
  
  in linux, language prints None, on osx it prints QPyNullVariant. it
  will then evaluate False in if not language and then crash when it
  arrives in __init__.py.
  
  This used to work with qt 4.7 on osx. Just upgrading to 4.8 breaks
  everything.
  Any ideas?
 
 It would suggest that there is a null QVariant stored in the settings
 for language. If there was no value stored then  would be
 returned. A workaround would therefore be to update or delete the
 settings data either manually or with a little script.
 
 Phil

I'm the author of Frescobaldi.

I was not expecting QPyNullVariant() to be returned when I specify a
default value. Why is it that bool(the returned QPyNullVariant)
defaults to True?

What is the best way to check for a possible QPyNullVariant to be
returned from the QSettings().value() method? (I.e. it should also work
on PyQt-4.7 systems).

My code uses QSettings().value almost everywhere. I was happy with the
simple interface, although it would be nice if the returned value would
be forced in the type of the default value if specified.

When I must check for more possible types to be returned, I must create
a wrapper function for it, I guess


Wilbert




-- 
Wilbert Berendsen
(http://www.wilbertberendsen.nl)

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] QPyNullVariant

2012-07-26 Thread Phil Thompson
On Thu, 26 Jul 2012 14:21:57 +0200, Wilbert Berendsen wbs...@xs4all.nl
wrote:
 Op Mon, 16 Jul 2012 12:11:11 +0100
 Phil Thompson p...@riverbankcomputing.com schreef:
 
 On Fri, 13 Jul 2012 15:49:48 +0200, Rodolfo Zitellini
 xhero...@gmail.com wrote:
  The offending code is in setup.py, when it reads the language:
  language = QSettings().value(language, )
  print language
  if not language:
  try:
  language = locale.getdefaultlocale()[0]
  except ValueError:
  pass
  if not language:
  language = C
  return language
  
  in linux, language prints None, on osx it prints QPyNullVariant. it
  will then evaluate False in if not language and then crash when it
  arrives in __init__.py.
  
  This used to work with qt 4.7 on osx. Just upgrading to 4.8 breaks
  everything.
  Any ideas?
 
 It would suggest that there is a null QVariant stored in the settings
 for language. If there was no value stored then  would be
 returned. A workaround would therefore be to update or delete the
 settings data either manually or with a little script.
 
 Phil
 
 I'm the author of Frescobaldi.
 
 I was not expecting QPyNullVariant() to be returned when I specify a
 default value. Why is it that bool(the returned QPyNullVariant)
 defaults to True?

It no longer does...

http://www.riverbankcomputing.com/static/Docs/PyQt4/html/incompatibilities.html#pyqt-v4-9-2

 What is the best way to check for a possible QPyNullVariant to be
 returned from the QSettings().value() method? (I.e. it should also work
 on PyQt-4.7 systems).

You will have to do something that explicitly handles the different PyQt
versions.

The best thing to do would be to ensure you never write a null QVariant to
the settings in the first place.

 My code uses QSettings().value almost everywhere. I was happy with the
 simple interface, although it would be nice if the returned value would
 be forced in the type of the default value if specified.

Try the optional type keyword argument to QVariant.value().
Unfortunately there was a bug in the PyQt documentation builder that means
that section of the documentation that covers this is replaced by the
QSettings class reference. You can still read it in the Sphinx sources
though.

 When I must check for more possible types to be returned, I must create
 a wrapper function for it, I guess
 
 
 Wilbert

Phil
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


[PyQt] Model/view: values of type Decimal not shown

2012-07-26 Thread Sibylle Koczian

Hello,

I don't know how to get numeric data from my model to my GUI, if they 
aren't floats but decimal.Decimal. In the following example the last 
column of the view and the last QLineEdit of the form both stay empty. 
The model data are printed to the console and it can be seen that the 
model contains the correct data for the last column and that they have 
the right type. But the GUI doesn't show them. What should I change?


Thanks for hints,
Sibylle


# modeltest.py

import sys
import decimal
import datetime
from PyQt4 import QtCore
from PyQt4 import QtGui

RAWDATA = [(1, First, datetime.date(2012, 7, 15), 1.25, 
decimal.Decimal(2.5)),

   (2, Second, datetime.date(2011, 6, 10), 3.77,
decimal.Decimal(7.54)),
   (3, Third, datetime.date(2010, 5, 27), 5.03,
decimal.Decimal(10.06))]

class MyDialog(QtGui.QDialog):

def __init__(self, data, parent=None):
super(MyDialog, self).__init__(parent)
self.myModel = self.setupModel(data)
self.showModelData()
view = self.setupView()
form = self.setupForm()
view.selectionModel().currentRowChanged.connect(
self.mapper.setCurrentModelIndex)
layout = QtGui.QVBoxLayout()
layout.addWidget(view)
layout.addWidget(form)
self.setLayout(layout)
#self.mapper.toFirst()
view.selectRow(0)

def setupModel(self, data):
md = QtGui.QStandardItemModel(len(data), 5)
md.setHorizontalHeaderLabels([Int, String, Date, Float,
  Decimal])
for (i, row) in enumerate(data):
# Integer
item = QtGui.QStandardItem()
item.setData(row[0], QtCore.Qt.EditRole)
md.setItem(i, 0, item)
# String
md.setItem(i, 1, QtGui.QStandardItem(row[1]))
item = QtGui.QStandardItem()
# First try: use datetime.date as is. Result:
# Date column in view empty, QDateEdit shows 1.1.2000
# item.setData(row[2], QtCore.Qt.EditRole)
# Second try: convert to QDate via string
dt = QtCore.QDate.fromString(row[2].isoformat(),
 QtCore.Qt.ISODate)
item.setData(dt, QtCore.Qt.EditRole)
md.setItem(i, 2, item)
# Float
item = QtGui.QStandardItem()
item.setData(row[3], QtCore.Qt.EditRole)
md.setItem(i, 3, item)
# Decimal
item = QtGui.QStandardItem()
item.setData(row[4], QtCore.Qt.EditRole)
md.setItem(i, 4, item)
return md

def setupView(self):
view = QtGui.QTableView()
view.setModel(self.myModel)
view.setAlternatingRowColors(True)
view.setSelectionMode(QtGui.QTableView.SingleSelection)
view.setSelectionBehavior(QtGui.QTableView.SelectRows)
view.resizeColumnsToContents()
return view

def setupForm(self):
# set up the widgets
intBox = QtGui.QSpinBox()
strEdit = QtGui.QLineEdit()
dateEdit = QtGui.QDateEdit()
floatEdit = QtGui.QLineEdit()
decEdit = QtGui.QLineEdit()
# set up the mapper
self.mapper = QtGui.QDataWidgetMapper(self)
self.mapper.setModel(self.myModel)
self.mapper.addMapping(intBox, 0)
self.mapper.addMapping(strEdit, 1)
self.mapper.addMapping(dateEdit, 2)
self.mapper.addMapping(floatEdit, 3)
self.mapper.addMapping(decEdit, 4)
# set up the layout
form = QtGui.QWidget()
layout = QtGui.QFormLayout()
layout.addRow(Int:, intBox)
layout.addRow(String:, strEdit)
layout.addRow(Date:, dateEdit)
layout.addRow(Float:, floatEdit)
layout.addRow(Decimal:, decEdit)
form.setLayout(layout)
return form

def showModelData(self):
print(--- Model data: ---)
for i in range(self.myModel.rowCount()):
inr = self.myModel.data(self.myModel.index(i, 0))
desc = self.myModel.data(self.myModel.index(i, 1))
day = self.myModel.data(self.myModel.index(i, 2))
fl = self.myModel.data(self.myModel.index(i, 3))
dec = self.myModel.data(self.myModel.index(i, 4))
print({0}: {1} / {2}, {3}, {4} ({5}).format(inr, desc, 
day, fl,

  dec, type(dec)))
print(--- End of model data ---)

app = QtGui.QApplication(sys.argv)
dia = MyDialog(RAWDATA)
dia.show()
app.exec_()
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt