Results 1 to 15 of 15

Thread: unable to click the hyperlink in textedit

  1. #1
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default unable to click the hyperlink in textedit

    hi ,
    i want to get a url from the dialog box lik this
    QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);

    after getting the url, i can only insert the url in the textedit, but it is not looking like a hyperlink.

    How to make the link in a clickable format.??????

    i have usd anchorclickd too

  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: unable to click the hyperlink in textedit

    The easiest solution is to wrap your url into html link tag (<a>) and add that to the text edit (provided that it is in rich text mode).

    Alternatively use QTextCursor::insertText() to insert your url with a proper text char format that contains the anchor.
    Last edited by wysota; 6th January 2011 at 11:36.
    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
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: unable to click the hyperlink in textedit

    wrap my url?

    Lets say that i am getting url from the above code
    QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);

    now i cant insert the baseUrl into the <a href .......> tag ???

  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: unable to click the hyperlink in textedit

    Quote Originally Posted by rleojoseph View Post
    now i cant insert the baseUrl into the <a href .......> tag ???
    Why not?
    Qt Code:
    1. QString link = QString("<a href=\"%1\">%1</a>").arg(baseUrl);
    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.


  5. The following user says thank you to wysota for this useful post:

    rleojoseph (7th January 2011)

  6. #5
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: unable to click the hyperlink in textedit

    ya. its working now. But i got a NEW problem now. after inserting the link in the TextEdit, the text which i am typing now is looking like a hyperlink. i have inserted a link lik www.google.com , whatever the text am typing after that it is like Hyperlink. even i tried a lot with Qtextcursor.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: unable to click the hyperlink in textedit

    Hey what's up with you? Please do not start multiple threads about the same topic, and I told you days ago to use HTML...

    And as to your new problem, you probably didn't close the a tag correct. Please show us the code.

  8. #7
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: unable to click the hyperlink in textedit

    Sorry for posting multiple threads


    here is the code

    Qt Code:
    1. bool ok;
    2. QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);
    3. if (ok && !baseUrl.isEmpty())
    4. {
    5.  
    6. QTextCursor cursor = ui->textEdit->textCursor();
    7. //int i=cursor.position();
    8. //qDebug("%d",i);
    9. QString link = QString("<a href=\"%1\">%1</a>").arg(baseUrl);
    10.  
    11. cursor.insertHtml(link);
    To copy to clipboard, switch view to plain text mode 

    Whatever i am typing after inserting url , all the texts are becoming as hyperlink.
    Last edited by Lykurg; 7th January 2011 at 12:46. Reason: missing [code] tags

  9. #8
    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: unable to click the hyperlink in textedit

    Make sure the cursor is positioned after the link is closed. For example insert a space after closing the "a" tag. For more advanced things you'll have to use the text document and text cursor api. Bottom line is you need to make sure the current text char format is not the one associated with the link. There are many one that are likely to make it work.
    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.


  10. The following user says thank you to wysota for this useful post:

    rleojoseph (7th January 2011)

  11. #9
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: link is not added with image.

    Hi, So far i have used QImage, QTextCursor, QTextCharFormat to make the image as a link.
    And i have used the QTextCharFormat::anchorRef also but when i am inserting the image using
    format.setAnchorHref(QString("<a href=\"%1\"> %1 </a> ").arg(file)); , the image is only added not the url.

    can any1 can fix this?


    Added after 1 21 minutes:


    cursor.insertImage will insert only the image, but i couldn't add the link to image.

    Last edited by rleojoseph; 10th January 2011 at 12:15.

  12. #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: link is not added with image.

    setAnchorHref() should only contain the href, not any html tags.

    I didn't test this but try adjusting something like this to your needs:
    Qt Code:
    1. QTextCursor addLink(QTextCursor cursor, const QString &url, QString title = QString()) {
    2. if(title.isEmpty()) title = url;
    3. QTextCharFormat original = cursor.charFormat();
    4. format.setAnchor(true);
    5. format.setAnchorHref(url);
    6. cursor.insertText(title, format);
    7. cursor.setFormat(original);
    8. return cursor;
    9. }
    10.  
    11. QTextEdit *te = ...
    12. addLink(te->textCursor(), "http://www.qtcentre.org", "QtCentre");
    To copy to clipboard, switch view to plain text mode 

    For adding an image instead of text you will probably want to use QTextCursor::insertImage() instead of insertText():
    Qt Code:
    1. format.setAnchor(true);
    2. format.setAnchorHref(url);
    3. format.setName(imageName);
    4. cursor.insertImage(format);
    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.


  13. #11
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default TextBrowser in TextEdit.

    I have did something where we can click the URL in the TextBrowser.
    But in TextEdit i can't do that.
    There is no signal that can be emitted from the TextEdit when a URL is Clicked. But in QTextBrowser we have the option of anchorClicked.
    My Question is how the TextBrowser is implemented in TexEdit, so that we can get a signal when the URL is Clicked in TextEdit.
    In simple , i want to get a signal from textedit when a URL is clicked.

    ???

  14. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: TextBrowser in TextEdit.

    Oh man, still the same problem, so stick with this thread!

    QTextBrowser inherits QTextEdit, not the other way around. If you what to know how the textbrowser can emit a signal like anchorClicked() have a look at the sources and find it out. Qt is open source! I haven't done it, but I guess they are reimplementing mousePressEvent(), check the current cursor at the position the event occur, and look if it is a link. if so, the signal is emitted.

  15. #13
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: TextBrowser in TextEdit.

    No sir. I have already done the clicking the URL in TextBrowser widget. But now am trying to implement it in the TextEdit Widget. ok?
    The problem is the signals available in the TextEdit are
    copyAvailable,currentCharFormatChanged,cursorPosit ionChanged (),redoAvailable,selectionChanged ,textChanged (),undoAvailable.
    So whenever i click the URL in the TextEdit which Signal is emitted?? that is what i was asking.

  16. #14
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: TextBrowser in TextEdit.

    Quote Originally Posted by rleojoseph View Post
    No sir. I have already done the clicking the URL in TextBrowser widget. But now am trying to implement it in the TextEdit Widget. ok?
    The title says: "unable to click the hyperlink in textedit". I mixed that up in the first place too.
    The problem is the signals available in the TextEdit are
    copyAvailable,currentCharFormatChanged,cursorPosit ionChanged (),redoAvailable,selectionChanged ,textChanged (),undoAvailable.
    So whenever i click the URL in the TextEdit which Signal is emitted?? that is what i was asking.
    And the answer has given in my last post: the text edit has no such signal. You have to do it yourself.

  17. #15
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: TextBrowser in TextEdit.

    Hi.. This is wat i finally found out. This might be useful for others. The URL can be inserted into a TextEdit but those links cannot be clicked, if u want click events go for TextBrowser.
    If you want the click event in the TextEdit, Creating Custom Signals and Slots might be the possible Solution.....

Similar Threads

  1. insert hyperlink(URL) inside the TextEdit..
    By rleojoseph in forum Qt Programming
    Replies: 10
    Last Post: 6th January 2011, 07:15
  2. Replies: 0
    Last Post: 2nd January 2010, 08:58
  3. Catch click Hyperlink event
    By summer_of_69 in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2009, 19:43
  4. QTextEdit for Hyperlink
    By blueteeth in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2008, 09:25
  5. How to create a HyperLink in Qt?
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 27th March 2007, 14:03

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.