I am not sure how you are setting the data but you could try something like this:
Qt Code:
To copy to clipboard, switch view to plain text mode
I am not sure how you are setting the data but you could try something like this:
Qt Code:
To copy to clipboard, switch view to plain text mode
The model is returning a QVariant double and the default delegate is formatting that with QLocale::toString() using the 'g' format. I would suggest you tackle this with a QStyledItemDelegate attached to the view or just to the double column(s). It need not do much except implement displayText() for double columns.
b1 (16th November 2011)
Thank you poporacer and ChrisW67,
I will try both methods tonight when I get home from work.
Thanks again, B1.
b1 (16th November 2011)
Thank you all for the replies. ChrisW67 gave me the solution needed. I had already reimplemented a spinbox for editing so all I had to do was reimplement the drawText function for the displayed text.
Qt Code:
void SpinBoxDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { double value = index.model()->data(index, Qt::EditRole).toDouble(); QString str; painter->drawText(option.rect, str.setNum(value, 'f', 1) ); }To copy to clipboard, switch view to plain text mode
My problem now is when the row is selected the background goes dark blue, as expected, however the text remains black rather than going white, making it impossible to read. Any suggestions to help solve this would be appreciated.
B1.
Last edited by b1; 16th November 2011 at 21:01. Reason: Correct typo in code sample
If you want custom painting then you have to do the work to honour selections and the like and adjust the colours accordingly. You use the data in the QStyleOptionViewItem to achieve that, e.g:
That's why I suggested just overriding QStyledItemDelegate::displayText() to change just the text that the default delegate painting then renders.Qt Code:
// something like this painter->save(); painter->fillRect(option.rect, option.palette.highlight()); painter->setPen(...); // use option.palette.highlightedText() painter->drawText(...); } else { painter->fillRect(option.rect, option.palette.base()); painter->setPen(...); // use option.palette.text() painter->drawText(...); } painter->restore();To copy to clipboard, switch view to plain text mode
Thanks ChrisW67,
I thought I would try the overriding idea and I have failed to get it to work.
Qt Code:
{ { return locale.toString(value.toDouble(), 'f', 1); } else { return QStyledItemDelegate::displayText( value, locale); } };To copy to clipboard, switch view to plain text mode
This doesn't compile with this error message:
I didn't think an object was needed in this case...error: cannot call member function 'virtual QString QStyledItemDelegate::displayText(const QVariant&, const QLocale&) const' without object
All the examples I have found seem to do it this way so any suggestions on where I have gone wrong would be appreciated?
Thanks, B1.
Is your SpinBoxDelegate class derived from QStyledItemDelegate? If it is not, then you can't call a member function for a class that is unrelated to yours. That's what the compiler is trying to tell you: since your class is not derived from QStyledItemDelegate, "this" is not a QStyledItemDelegate, so in order to call one of the QStyledItemDelegate methods, you need an instance of that kind of object.
The solution is simple:
Qt Code:
class SpinBoxDelegate : public QStyledItemDelegate { // ... };To copy to clipboard, switch view to plain text mode
Then your displayText() method will compile and work as you expect.
b1 (18th November 2011)
Thank you d_stranz,
You are correct, it was simple and it worked. I couldn't see the wood for the trees!!!
Thank you to all who answered,
B1.
Bookmarks