[Qt-interest] Display price in QTableView?

Paul Miller paul at fxtech.com
Wed Jan 14 15:04:20 CET 2009


Israel Brewster wrote:
> I am looking for a good solution to display a price in a QTableView.  
> Just using widgets this is easy: I simply use a QDoubleSpinBox, set  
> the prefix to $, and set it to display two decimals. I'm looking for  
> something like the createEditor function of QItemDelegate, but that  
> doesn't appear to exist. I looked at the paint() function, but the  
> example I found used QStyleOptions, the closest of which was just a  
> SpinBox (not DoubleSpinBox), had no way to set a prefix that could  
> find, and didn't work for me anyway. I also considered re-implementing  
> the data function of the underlying QSqlTableModel, but then it would  
> have to return a string to get the "$", which would make editing it  
> more difficult, if not impossible. Is there a better way of doing this  
> that I am missing here? Thanks.

I handled this in my model. Note you can return a different value with 
the DisplayRole than the EditRole. So for the DisplayRole I get the 
value and add locale-specific currency symbol and parenthesis (for 
negative values) as needed, but for the EditRole I just return the 
straight value. You can even override the text color to have negative 
numbers displayed in red.

-- 
Paul Miller | paul at fxtech.com | www.fxtech.com | Got Tivo?




More information about the Qt-interest-old mailing list