[SOLVED] QStyleOptionViewItem draw text ?



  • 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


  • Moderators

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



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



  • 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


  • Moderators

    what exactly do you mean?



  • 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)!


  • Moderators

    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?



  • [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 ?


  • Moderators

    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?



  • 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 :)


  • Moderators

    thats why i asked ;)



  • 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::?????);



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


  • Moderators

    try QPalette::Background



  • 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



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



  • Thanks Andre,

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


Log in to reply
 

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