TableView how to format cells of column



  • How to apply specific format to cells of specific column in TableView control ?

    For example, I have column which is filled with values of type double and they are displayed with default format (with arbitrary number of decimal places), I want them formatted with 2 decimal places instead. I cannot put them as QStrings because sort will not work correctly.

    Also how to right-align that column ?



  • I've same issue and I couldn't find any snippet to help me out.


  • Moderators

    "subclass QSyteledItemDelegate":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#subclassing-qstyleditemdelegate and override it's paint method. In there paint the double value like this:
    @
    painter->drawText( pos, QString::number( value, 'g', 2 ); //you may want to choose 'f' instead
    @
    and "set the delegate":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#setItemDelegate on your tableview.



  • [quote author="raven-worx" date="1367224358"]"subclass QSyteledItemDelegate":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#subclassing-qstyleditemdelegate and override it's paint method. In there paint the double value like this:
    @
    painter->drawText( pos, QString::number( value, 'g', 2 ); //you may want to choose 'f' instead
    @
    and "set the delegate":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#setItemDelegate on your tableview.[/quote]

    This solution deprecated and not became available in Qt5!


  • Moderators

    [quote author="mbnoimi" date="1367226491"]This solution deprecated and not became available in Qt5![/quote]
    and what exactly makes you believe that flam?!



  • I am aware of paint replacement or creating item delegate solution but it seems it is just too much if you consider Qt as RAD tool. Think about it: what about styles and other TableView settings that have to be met like focus rectangle and such. You have to redo big deal of it, just to specify conversion from QVariant cell data to QString for rendering.

    If you compare it to .net's DataGridView, there you have event which collects real data (QVariant equivalent, this data is required to allow correct sorting and filtering behavior), then you have event that allows conversion to rendering type (QVariant -> QString equivalent, this allow you to do formatting display text from step 1. data and avoid replacing rendering), and finally there is event that allow rendering replacement (QString to picture on screen). So, if there is no similar thing in Qt that should really be considered. Also rendering process in DataGridView is separated in several phases so you can do as little as required by using original rendering as much as possible. That would be very nice to see in Qt also.

    How about right alignment, is that possible without render replacement ?


  • Moderators

    ok ... it should be also enough to do the following and let Qt do the rest (focus rect, selection background, etc.):
    @
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);
    opt.text = QString::number( value, 'g', 2 );
    QStyledItemDelegate::paint(opt, index);
    }
    @

    How do you add your data to the listview exactly? Do you use a custom model?



  • Now, this starts to be useful !

    At this point is it "healthy" to manipulate other opt values also, so I could customize TableView's behavior even more ?

    All we need here is some universal format configurable ItemDelegate so we should not create specific class every time we need different format.

    I really appreciate your help, thank you.

    P.S. To answer your question, I don't really implement this in a concrete program, right now, but evaluating options that I have if I choose QTableView.


  • Moderators

    [quote author="Mearb" date="1367266399"]At this point is it "healthy" to manipulate other opt values also, so I could customize TableView's behavior even more ?[/quote]

    What do you have in mind for example? Visually there are also alot of capabilities using stylesheets (in CSS-like syntax).

    [quote author="Mearb" date="1367266399"]
    All we need here is some universal format configurable ItemDelegate so we should not create specific class every time we need different format.[/quote]

    If you choose to subclass you could add a custom property to specifiy the precision if the value is a double value. You get a QVariant passed ... so it could be any type of value, whcih makes it unverisally usable. So you could check in the paint method if it is a double-variant (QVariant::type()) and set the text the configured way...



  • What do you have in mind for example?

    Column specific colors, data specific colors (red negative values and such things). As I know, stylesheets can't be applied on per column base, can they ?

    Could I change such things in QStyleOptionViewItemV4 object or in QPainter object before call to base class QStyledItemDelegate::paint (is it "healthy" to do that) ?


  • Moderators

    [quote author="Mearb" date="1367272312"]
    Column specific colors, data specific colors (red negative values and such things). As I know, stylesheets can't be applied on per column base, can they ?
    [/quote]
    No that's thats too specific for stylesheets. But take a look at the "members":http://qt-project.org/doc/qt-4.8/qstyleoptionviewitemv4-members.html of the styleoption class. The backgroundBrush, index and state members are the most useful for that case.
    [quote author="Mearb" date="1367272312"]
    Could I change such things in QStyleOptionViewItemV4 object or in QPainter object before call to base class QStyledItemDelegate::paint (is it "healthy" to do that) ?
    [/quote]
    yes it is ... that's one purpose they are for ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.