Results 1 to 2 of 2

Thread: URL in QTextEdit

  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default URL in QTextEdit

    I would like to launch URLs from a QTextEdit widget. The URLs are custom styled. I have made a naive attempt:

    Writing text to the QTextEdit:
    Qt Code:
    1. bool Text::WriteText( const QStringRef& str, bool inurl ) // Text is "public QTextEdit", inurl is true if we are appending an URL
    2. {
    3. if( !str.isEmpty() )
    4. {
    5. if( inurl )
    6. {
    7. QTextCharFormat cf(currentCharFormat());
    8.  
    9. cf.setAnchorHref(str.toString());
    10. mergeCurrentCharFormat(cf);
    11. }
    12.  
    13. insertPlainText(str.toString());
    14. }
    15.  
    16. return true;
    17. }
    To copy to clipboard, switch view to plain text mode 

    mouse click (debugging version for now, launch iceweasel when it will work):
    Qt Code:
    1. void Text::mousePressEvent( QMouseEvent *event )
    2. {
    3. bool bb;
    4.  
    5. if( event->buttons() == Qt::LeftButton )
    6. {
    7. QTextCursor cu(cursorForPosition(event->pos()));
    8. QTextCharFormat cf(cu.charFormat());
    9. QString url(cf.anchorHref());
    10.  
    11. bb = url.isEmpty();
    12.  
    13. QTextEdit::mousePressEvent(event);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Works ... almost. If I click a URL in the text window, the "url" string contains the URL. If I click another URL, I get another URL correctly. But if I click a non-URL text, I get the last URL clicked. Not acceptable. What am I doing wrong?

  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: URL in QTextEdit

    Most probably the text char format is incorrect. Do you get the same problem if you insert the data into the text edit widget with code using your own crafted text char formats?
    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. QTextEdit
    By titoaii in forum Newbie
    Replies: 3
    Last Post: 15th April 2010, 09:29
  2. QTextEdit
    By veda in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2007, 07:30
  3. Replies: 1
    Last Post: 16th February 2007, 07:22
  4. QTextEdit
    By merlvingian in forum Newbie
    Replies: 1
    Last Post: 2nd November 2006, 08:15
  5. 4.1 to 4.1.1, qtextedit
    By jey in forum Qt Programming
    Replies: 5
    Last Post: 9th March 2006, 12:48

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.