Results 1 to 2 of 2

Thread: Delegate hyperlink

  1. #1
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Delegate hyperlink

    Hi

    I am using below code to draw an html. I need to display text in the item as link, once clicked on a text (not on the item it self) it perform some action. I was using setIndexWidget and QLabel but performance is pretty bad.

    An here the question how to make a text clickable ( not the item) to behave like an html link.

    Possible ? thanks for help

    Qt Code:
    1. QSize HyperLinkDelegate::sizeHint(const QStyleOptionViewItem & option ,
    2. const QModelIndex & index) const
    3. {
    4. QStyleOptionViewItemV4 optionV4 = option;
    5. initStyleOption(&optionV4, index);
    6.  
    7. doc.setHtml(optionV4.text);
    8. doc.setTextWidth(optionV4.rect.width());
    9. return QSize(doc.idealWidth(), doc.size().height());
    10. }
    11.  
    12. void HyperLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    13. const QModelIndex &index) const
    14. {
    15. QStyleOptionViewItemV4 optionV4 = option;
    16. initStyleOption(&optionV4, index);
    17.  
    18. QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();
    19.  
    20. doc.setHtml(optionV4.text);
    21.  
    22. // Painting item without text
    23. optionV4.text = QString();
    24. style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);
    25.  
    26. QAbstractTextDocumentLayout::PaintContext ctx;
    27.  
    28. // Highlighting text if item is selected
    29. if (optionV4.state & QStyle::State_Selected)
    30. ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
    31.  
    32. QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
    33. painter->save();
    34. painter->translate(textRect.topLeft());
    35. painter->setClipRect(textRect.translated(-textRect.topLeft()));
    36. doc.documentLayout()->draw(painter, ctx);
    37. painter->restore();
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

  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: Delegate hyperlink

    There are no magic two lines of code you can call and have the job done. You have to calculate the geometry of the link and provide event handlers for the mouse that will 1) change the cursor when it is over the link and 2) execute the action when the link is clicked.
    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.


Similar Threads

  1. Looking for a hyperlink control
    By scarleton in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2010, 04:46
  2. QTextEdit for Hyperlink
    By blueteeth in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2008, 09:25
  3. How to create a HyperLink in Qt 4.1.5?
    By vishal.chauhan in forum Qt Programming
    Replies: 7
    Last Post: 18th May 2007, 06:36
  4. How to create a HyperLink in Qt?
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 27th March 2007, 14:03
  5. creating a hyperlink
    By filmfreak in forum Qt Tools
    Replies: 1
    Last Post: 22nd February 2006, 00:49

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.