Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TableView how to format cells of column
QtWS25 Last Chance

TableView how to format cells of column

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 8.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mearb
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbnoimi
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        "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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbnoimi
          wrote on last edited by
          #4

          [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!

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mearb
              wrote on last edited by
              #6

              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 ?

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                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?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mearb
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    [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...

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mearb
                      wrote on last edited by
                      #10

                      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) ?

                      1 Reply Last reply
                      0
                      • raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #11

                        [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 ;)

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved