clive
27th March 2007, 14:27
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
#ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H
//
#include "ui_mainwindow.h"
//
class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
public slots:
void playVMi(const QUrl &url);
private slots:
void returnPressed();
private:
QString detectLinks(QString dataIn, QString urlType);
void display(QString dataIn);
};
#endif
The cpp file
#include <QtGui>
#include <QDesktopServices>
#include "mainwindowimpl.h"
//
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
connect(leInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
QDesktopServices::setUrlHandler("vmi", this, "playVMi");
}
void MainWindowImpl::returnPressed()
{
if(leInput->text() != "")
display(leInput->text());
leInput->setText("");
}
// we registerd vm:// with the URL catcher and it sends us here when a url of this type is clicked
void MainWindowImpl::playVMi(const QUrl &url)
{
leInput->setFocus();
// Normally there would be code here to act upon the URL in some way
}
void MainWindowImpl::display(QString dataIn)
{
dataIn = detectLinks(dataIn, "http://");
dataIn = detectLinks(dataIn, "mailto:");
dataIn = detectLinks(dataIn, "ftp://");
dataIn = detectLinks(dataIn, "vmi://");
tbDisplay->append(dataIn);
tbDisplay->verticalScrollBar()->setValue(tbDisplay->verticalScrollBar()->maximum());
}
// Detect URL's
QString MainWindowImpl::detectLinks(QString dataIn, QString urlType)
{
QString url;
QString user;
QDateTime dt;
dt = QDateTime::currentDateTime();
int sp[100], ep[100], y, count1, count2;
count1 = -1;
count2 = -1;
y = 0;
do // First find the link start points
{
count1++;
sp[count1] = dataIn.indexOf(urlType, y, Qt::CaseSensitive);
y = sp[count1] + 1;
}
while(sp[count1] > -1);
count1--;
if(sp[0] > -1) // if there are any start points.....
{
do // find the link end points
{
count2++;
ep[count2] = dataIn.indexOf(" ", sp[count2], Qt::CaseSensitive);
if(ep[count2] == -1) // if the link is the end of the text then the space will not be found....
ep[count2] = dataIn.size(); // ....so set the end point to the end of the text
}
while(count2 <= count1);
}
for(int x = count1; x >= 0; x--)
{
url = "\">" + dataIn.mid(sp[x], ep[x] - sp[x]) + "</a>";
dataIn.insert(ep[x], url);
dataIn.insert(sp[x], QString("<a href=\""));
}
return(dataIn);
}
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
#ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H
//
#include "ui_mainwindow.h"
//
class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
public slots:
void playVMi(const QUrl &url);
private slots:
void returnPressed();
private:
QString detectLinks(QString dataIn, QString urlType);
void display(QString dataIn);
};
#endif
The cpp file
#include <QtGui>
#include <QDesktopServices>
#include "mainwindowimpl.h"
//
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
connect(leInput, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
QDesktopServices::setUrlHandler("vmi", this, "playVMi");
}
void MainWindowImpl::returnPressed()
{
if(leInput->text() != "")
display(leInput->text());
leInput->setText("");
}
// we registerd vm:// with the URL catcher and it sends us here when a url of this type is clicked
void MainWindowImpl::playVMi(const QUrl &url)
{
leInput->setFocus();
// Normally there would be code here to act upon the URL in some way
}
void MainWindowImpl::display(QString dataIn)
{
dataIn = detectLinks(dataIn, "http://");
dataIn = detectLinks(dataIn, "mailto:");
dataIn = detectLinks(dataIn, "ftp://");
dataIn = detectLinks(dataIn, "vmi://");
tbDisplay->append(dataIn);
tbDisplay->verticalScrollBar()->setValue(tbDisplay->verticalScrollBar()->maximum());
}
// Detect URL's
QString MainWindowImpl::detectLinks(QString dataIn, QString urlType)
{
QString url;
QString user;
QDateTime dt;
dt = QDateTime::currentDateTime();
int sp[100], ep[100], y, count1, count2;
count1 = -1;
count2 = -1;
y = 0;
do // First find the link start points
{
count1++;
sp[count1] = dataIn.indexOf(urlType, y, Qt::CaseSensitive);
y = sp[count1] + 1;
}
while(sp[count1] > -1);
count1--;
if(sp[0] > -1) // if there are any start points.....
{
do // find the link end points
{
count2++;
ep[count2] = dataIn.indexOf(" ", sp[count2], Qt::CaseSensitive);
if(ep[count2] == -1) // if the link is the end of the text then the space will not be found....
ep[count2] = dataIn.size(); // ....so set the end point to the end of the text
}
while(count2 <= count1);
}
for(int x = count1; x >= 0; x--)
{
url = "\">" + dataIn.mid(sp[x], ep[x] - sp[x]) + "</a>";
dataIn.insert(ep[x], url);
dataIn.insert(sp[x], QString("<a href=\""));
}
return(dataIn);
}