Results 1 to 19 of 19

Thread: HTML and QStandardItem ?

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default HTML and QStandardItem ?

    I have a QStandardItemModel and some QStandardItems.
    I use item.setText(QString("......")) to set the text of the item that I wont.
    The problem I have is that I can't set text with html tags. I mean, something like item.setText(QString("<b>" + string + "</b>")).

    Is that possible? Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HTML and QStandardItem ?

    Not directly. Search the forum for "rich text" and "delegate" for possible solutions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    I should write a delegate for the QTreeView, right? It's the only one with paintEvent. The QStandardItem doesn't have it.

    I have searched some things, but without much luck. Maybe this could help me? http://doc.trolltech.com/qq/qq24-delegates.html (it's c++ & qt4) Can you give me some more examples? Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HTML and QStandardItem ?

    If you open the search box of the forum and enter "rich text delegate", you'll get some meaningful answers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Ok, I got the basic. I inherit from QItemDelegate (I want to be able to use icons and so on).
    I have one more question. What should I implement? drawDisplay? paint? something more?

    edit: What should I inherit from? QItemDelegate or QStyledItemDelegate ? (http://www.riverbankcomputing.co.uk/...mdelegate.html)

    I only want to be able to use colored text and icons like this one > < in a QStandardItem in a QTreeView.
    Something like:

    qtv = QTreeView()
    d = MyCustomDelegate()
    qtv.setDelegate(d)

    string = "<b>bold text</b> with image <img src=..........>"
    a = QStandardItem()
    a.setText(string)
    Last edited by alexandernst; 30th July 2009 at 02:39.

  6. #6
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HTML and QStandardItem ?

    Learning delegates was on my to-do list so here is a simple implementation.
    Qt Code:
    1. class Delegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. protected:
    6. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    7. QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    8. };
    9.  
    10.  
    11. void Delegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
    12. {
    13. QStyleOptionViewItemV4 options = option;
    14. initStyleOption(&options, index);
    15.  
    16. painter->save();
    17.  
    18. doc.setHtml(options.text);
    19.  
    20. /* Call this to get the focus rect and selection background. */
    21. options.text = "";
    22. options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
    23.  
    24. /* Draw using our rich text document. */
    25. painter->translate(options.rect.left(), options.rect.top());
    26. QRect clip(0, 0, options.rect.width(), options.rect.height());
    27. doc.drawContents(painter, clip);
    28.  
    29. painter->restore();
    30. }
    31.  
    32. QSize Delegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    33. {
    34. QStyleOptionViewItemV4 options = option;
    35. initStyleOption(&options, index);
    36.  
    37. doc.setHtml(options.text);
    38. doc.setTextWidth(options.rect.width());
    39. return QSize(doc.idealWidth(), doc.size().height());
    40. }
    41.  
    42.  
    43. /* Demonstration. */
    44. MainWindow::MainWindow(QWidget *parent)
    45. : QMainWindow(parent)
    46. {
    47. model->setData(model->index(0, 0), "<b>Test</b><br />i<img src=\"img.png\" />ng123", Qt::DisplayRole);
    48. model->setData(model->index(1, 0), "<b>Test</b>ing234", Qt::DisplayRole);
    49.  
    50. QListView * lv = new QListView;
    51. Delegate * delegate = new Delegate;
    52. lv->setModel(model);
    53. lv->setItemDelegate(delegate);
    54.  
    55. setCentralWidget(lv);
    56. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Really cool!!! I translated this to python:

    Qt Code:
    1. class itemDelegate(QStyledItemDelegate):
    2.  
    3. def paint(self, painter, option, index):
    4. options = QStyleOptionViewItemV4()
    5. options.__init__(option)
    6. self.initStyleOption(options, index)
    7.  
    8. painter.save()
    9.  
    10. doc = QTextDocument()
    11. doc.setHtml(options.text)
    12.  
    13. #get focus rect and selection background
    14. options.text = ""
    15. options.widget.style().drawControl(QStyle.CE_ItemViewItem, options, painter)
    16.  
    17. #draw using our rich text document
    18. painter.translate(options.rect.left(), options.rect.top())
    19. rect = QRectF()
    20. rect.__init__(0, 0, options.rect.width(), options.rect.height())
    21. doc.drawContents(painter, rect)
    22.  
    23. painter.restore()
    24.  
    25. def sizeHint(self, option, index):
    26. options = QStyleOptionViewItemV4()
    27. options.__init__(option)
    28. self.initStyleOption(options, index)
    29.  
    30. doc = QTextDocument()
    31. doc.setHtml(options.text)
    32. doc.setTextWidth(options.rect.width())
    33. size = QSize()
    34. size.__init__(doc.idealWidth(), doc.size().height())
    35. return size
    To copy to clipboard, switch view to plain text mode 

    But I have one little problem. I dont get the same colors when selected/mouse_over when I'm using my custom delegate and whem I'm using the "default" one. See those 2 pictures if you cant understandme.

    Mouse over an item:


    Item selected:


    Do you see the diference between the colors? Why is that happening? Maybe I translated something wrong? Something related with the "options" maybe?


    EDIT:

    Ok, I have been looking at this and I think that this is a pallete problem. Maybe I should manually set the backgroundBrush ? Something like this:

    Qt Code:
    1. if option.state & QStyle.MouseOver
    2. if option.state & QStyle.Selected
    3. options.backgroundBrush = DARK-BLUE
    4. else
    5. options.backgroundBrush = LIGHT-BLUE
    To copy to clipboard, switch view to plain text mode 

    The problem is that the background color is not the same on every style/SO, so, I cant just force it to use "blue" (the default in qt4 on kde).

    Maybe I'm on the wrong way :/ ?
    Last edited by alexandernst; 31st July 2009 at 04:00.

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    offtop: I wold create QTextDocument in the heap.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Nobody? .

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HTML and QStandardItem ?

    Will it change anything if you get rid of this line?
    python Code:
    1. self.initStyleOption(options, index)
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    It wont draw the text, but it will draw the background (with the messed colors).
    So, commenting that line is making it worse.

  12. #12
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Ok, I know where the problem is! It's here:

    Qt Code:
    1. QStyleOptionViewItemV4 options = option;
    To copy to clipboard, switch view to plain text mode 

    And I translated it this way:

    Qt Code:
    1. options = QStyleOptionViewItemV4()
    2. options.__init__(option)
    To copy to clipboard, switch view to plain text mode 

    So, here is the problem! It's just that I'm doing it wrong. I still don't know how exactly has to be done. I know that the problem is here because if I change the second line of the translated code to this "options = option" then I wont get the text and when I click on the item I wont get the dark-blue color. I'll keep thinking...

  13. #13
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Ok, I got here:

    Qt Code:
    1. options = QStyleOptionViewItemV4(option)
    2. options.state &= ~QStyle.State_Selected
    3. options.state &= ~QStyle.State_MouseOver
    To copy to clipboard, switch view to plain text mode 

    This will make it *almost* work. When mouse is over an item, colors show perfectly, but when I click an item, the left small half of the item (look at pictures in my last post) will be filled with a dark-blue and if the mouse is over the item (once selected) the right big half will be filled with the same color as when mouse is over the item. If the mouse is not over the item once selected, the left half will be filled with the same color as if the mouse is over the item, while the right halft will be white.

    This is the stylesheet of the qtreeview:

    Qt Code:
    1. QTreeView::item:hover {
    2. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
    3. }
    4.  
    5.  
    6. QTreeView::item:selected:active{
    7. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
    8. }
    9.  
    10. QTreeView::item:selected:!active {
    11. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
    12. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Could be this the problem:

    Qt Code:
    1. doc.setHtml(options.text)
    To copy to clipboard, switch view to plain text mode 

    This will create a QTextDocument, but that element doesnt support StyleSheet, so, it gets colored with the default QStyle.palette colors.

  15. #15
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HTML and QStandardItem ?

    It turns out that I left out the last (supposedly optional) argument to drawControl. The line should be:
    Qt Code:
    1. options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
    To copy to clipboard, switch view to plain text mode 
    which fixes everything on my computer.

  16. #16
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    ROFL! xD
    This was sooooo annoying :/ I'm really glad that we finally made it =P.
    That code was dryving me insane uff...

    One more thing, could you tell me if style sheet works for you? I'm trying to draw the items with custom background his way:

    Qt Code:
    1. QTreeView::item:hover {
    2. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
    3. }
    4.  
    5. QTreeView::item:selected:active{
    6. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
    7. }
    8.  
    9. QTreeView::item:selected:!active {
    10. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
    11. }
    To copy to clipboard, switch view to plain text mode 

    But I get the default blue/dark-blue when selected or mouse-over.

  17. #17
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HTML and QStandardItem ?

    I get the following, which is not the default on my machine.
    Attached Images Attached Images

  18. #18
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Yeah, I get that too! But I have my own stylesheet and it's not like that, so... What's the problem? Why is not rendering my stylesheet?

  19. #19
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: HTML and QStandardItem ?

    Ok, nm... I'm stupid. It was actually showing the stylesheet, but I had the stylesheet almost exactly the same colors as the default (+I'm a little bit daltonic) ...

    Really thanks I can finally rip xD

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.