Results 1 to 19 of 19

Thread: HTML and QStandardItem ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

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
  •  
Qt is a trademark of The Qt Company.