I have put together this minimal application to demonstrate a problem with custom URL's.

Run this application and just press enter on the default text in the line edit.
Enter another piece of text into the line edit and press enter.
Now click the link in the QTextBrowser.
Now add another line of text via the line edit.
All text added after clicking the custom URL is underlined.
Could this be a bug or am I doing something wrong?

The complete bug test project
http://www.winpe.com/prog/bugtest.tar.gz

The header File
Qt Code:
  1. #ifndef MAINWINDOWIMPL_H
  2. #define MAINWINDOWIMPL_H
  3. //
  4. #include "ui_mainwindow.h"
  5. //
  6. class MainWindowImpl : public QMainWindow, public Ui::MainWindow
  7. {
  8. Q_OBJECT
  9. public:
  10. MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
  11.  
  12. public slots:
  13. void playVMi(const QUrl &url);
  14.  
  15. private slots:
  16. void returnPressed();
  17.  
  18. private:
  19. QString detectLinks(QString dataIn, QString urlType);
  20. void display(QString dataIn);
  21.  
  22. };
  23. #endif
To copy to clipboard, switch view to plain text mode 

The cpp file
Qt Code:
  1. #include <QtGui>
  2. #include <QDesktopServices>
  3.  
  4. #include "mainwindowimpl.h"
  5. //
  6. MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
  7. : QMainWindow(parent, f)
  8. {
  9. setupUi(this);
  10.  
  11. connect(leInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
  12.  
  13. QDesktopServices::setUrlHandler("vmi", this, "playVMi");
  14. }
  15.  
  16. void MainWindowImpl::returnPressed()
  17. {
  18. if(leInput->text() != "")
  19. display(leInput->text());
  20. leInput->setText("");
  21. }
  22.  
  23. // we registerd vm:// with the URL catcher and it sends us here when a url of this type is clicked
  24. void MainWindowImpl::playVMi(const QUrl &url)
  25. {
  26. leInput->setFocus();
  27. // Normally there would be code here to act upon the URL in some way
  28. }
  29.  
  30. void MainWindowImpl::display(QString dataIn)
  31. {
  32. dataIn = detectLinks(dataIn, "http://");
  33. dataIn = detectLinks(dataIn, "mailto:");
  34. dataIn = detectLinks(dataIn, "ftp://");
  35. dataIn = detectLinks(dataIn, "vmi://");
  36. tbDisplay->append(dataIn);
  37. tbDisplay->verticalScrollBar()->setValue(tbDisplay->verticalScrollBar()->maximum());
  38. }
  39.  
  40. // Detect URL's
  41. QString MainWindowImpl::detectLinks(QString dataIn, QString urlType)
  42. {
  43. QString url;
  44. QString user;
  45. dt = QDateTime::currentDateTime();
  46. int sp[100], ep[100], y, count1, count2;
  47. count1 = -1;
  48. count2 = -1;
  49. y = 0;
  50. do // First find the link start points
  51. {
  52. count1++;
  53. sp[count1] = dataIn.indexOf(urlType, y, Qt::CaseSensitive);
  54. y = sp[count1] + 1;
  55. }
  56. while(sp[count1] > -1);
  57. count1--;
  58. if(sp[0] > -1) // if there are any start points.....
  59. {
  60. do // find the link end points
  61. {
  62. count2++;
  63. ep[count2] = dataIn.indexOf(" ", sp[count2], Qt::CaseSensitive);
  64. if(ep[count2] == -1) // if the link is the end of the text then the space will not be found....
  65. ep[count2] = dataIn.size(); // ....so set the end point to the end of the text
  66. }
  67. while(count2 <= count1);
  68. }
  69. for(int x = count1; x >= 0; x--)
  70. {
  71. url = "\">" + dataIn.mid(sp[x], ep[x] - sp[x]) + "</a>";
  72. dataIn.insert(ep[x], url);
  73. dataIn.insert(sp[x], QString("<a href=\""));
  74. }
  75. return(dataIn);
  76. }
To copy to clipboard, switch view to plain text mode