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. [SOLVED] QStyleOptionViewItem draw text ?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QStyleOptionViewItem draw text ?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 9.5k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    Hi,

    I am working on a tableView that uses a QStyledItemDelegate. Inside the itemDelegate I am trying to render a QLineEdit with someText but somehow the text is not displayed. eg

    @if (condition)
    {
    QStyleOptionViewItem item(option);
    item.rect = option.rect;
    item.text = "Hello World";
    item.palette = QPalette(Qt::Red); // just for test

    QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, &item, painter);
    QApplication::style()->drawPrimitive(QStyle::PE_FrameLineEdit, &item, painter);
    }@

    But the text is not rendered at all but if I use qDebug() << item.text; it gives the right output.

    How to render the text using QStyleOptionViewItem ?

    Thanks

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

      i think the problem is that you just don't draw the text ^^

      Look at QLineEdit::paintEvent() implementation (<Qt5-src>\qtbase\src\widgets\widgets\qlineedit.cpp).
      In there you will see that at the beginning the frame/panel is drawn and then then alot of code follows just for text rendering...

      You can all the code you need from there

      or just draw the text with QPainter::drawText() into the rect (using QStyle::subElementRect(QStyle::SE_LineEditContents))

      maybe the most wasiest way: hold a QLineEdit widget as member in your delegate and render it:

      @
      m_RenderLineEdit.setGeometry(option.rect);
      m_RenderLineEdit.render(painter);
      @

      --- 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
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I think you're passing the wrong type of style option... Note that you'll also need to render the contents of the line edit, not just the frame.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          Thanks for a quick reply !!!!

          I changed style option to QStyleOpionFrameV2 (I just checked the source of qlineedit.cpp )

          @QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, &item, painter);

          QApplication::style()->drawPrimitive(QStyle::PE_FrameLineEdit, &item, painter);

          painter->drawText(QApplication::style()->subElementRect(QStyle::SE_LineEditContents, &item),text);@

          Now the text is rendered but there is a little offset on the y() value , anyhow i'll adjust that. But what about the palette how to change that ?

          Thanks

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

            what exactly do you mean?

            --- 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
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              Ok i'll explain in detail,

              The background color(yellow/red) of the lineEdit changes based on the validator and the current text. The tableview cell should render the same palette/effect of the lineEdit.

              The lineEdit is initialized/editable when a particular cell is clicked. I need to set the same effect for a particular cell. For the tableView each cell contains an object of a class that stores the property like text/palette etc.

              Eg I need to render something like

              !http://img42.imageshack.us/img42/3738/v3c4.png(lineEdit)!

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

                ok, but why do you want to draw a line edit instead of using a real qlinedit widget?
                pro: works our of the box like a qlineedit should
                con: may cause performance

                Or why do you want to draw a QLineEdit widget at all and not just simply the text with a background color?

                --- 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
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  [quote author="raven-worx" date="1371554769"]ok, but why do you want to draw a line edit instead of using a real qlinedit widget?
                  pro: works our of the box like a qlineedit should
                  con: may cause performance
                  [/quote]

                  I can't use setInexWidget() performance drops heavily !!!

                  bq. Or why do you want to use a QLineEdit widget at all?

                  The lineEdit is initialized in the createEditor(). what else you recommend ?

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

                    just draw the text with a simple background is enough for you?

                    How do you exactly change the background color of the line edit when the QValidator fails? By Stylesheet?

                    --- 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
                    • S Offline
                      S Offline
                      Sam
                      wrote on last edited by
                      #10

                      yes the background color for the lineEdit changes through styleSheets.
                      Yes easiest way is to change the background color of the cell.

                      Thanks :)

                      PS : I was thinking too complex :)

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

                        thats why i asked ;)

                        --- 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
                        • S Offline
                          S Offline
                          Sam
                          wrote on last edited by
                          #12

                          One more question I would like to ask , is there anyway we can get the background-color of the lineEdit which is set through stylesheets like in the above image the yellow color is set through stylesheet.

                          Anything like
                          lineEdit->palette().color(QPalette::?????);

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

                            No. It might not even be a color. It might be a brush. Or an image, or...

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

                              try QPalette::Background

                              --- 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
                              • S Offline
                                S Offline
                                Sam
                                wrote on last edited by
                                #15

                                and if we are setting the background-color through stylesheets then also its not possible to retrieve the value ?

                                PS : QPalette::Background return gray color

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  [quote author="Sam" date="1371730152"]and if we are setting the background-color through stylesheets then also its not possible to retrieve the value ?[/quote]
                                  AFAIK: indeed not. The style sheets are not exposed through the public API other then setting them on a widget or the application.

                                  I'd love to be proven wrong on this one though!

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    Sam
                                    wrote on last edited by
                                    #17

                                    Thanks Andre,

                                    I think then I have to check/set the color for the painter based on properties specified for the lineEdit.

                                    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