PDA

View Full Version : Could this be a bug in QTextBrowser?



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);
}

guilugi
27th March 2007, 17:53
Ok..it seems like a bug to me.

First, I thought your detectLinks method had a mistake, creating a link for every new text inserted, but that's not the case.

I added a simple debug message into the lines dedicated to link creation, and it outputs nothing when I insert standard text...

You should post on Task Tracker, I haven't noticed a bug like that, at least for QTextBrowser.

clive
27th March 2007, 19:55
The problem is definitely with the QTextBrowser, for instance if you make the folowing changes to the returnPressed function



void MainWindowImpl::returnPressed()
{
if(leInput->text() != "")
if(leInput->text().left(1) == "a")
tbDisplay->append(leInput->text());
else
display(leInput->text());
leInput->setText("");
}


Now any lines that start with the letter 'a' are just appended to the QTextBrowser and the problem still exists.
If you keep adding text to the QTextBrowser without ever clicking on the custom link then all is fine, but the moment you click the custom link every line of text added to the QTextBrowser from that moment onwards is underlined, any lines added after the link was added but before the link is clicked remain okay.
I have made a bug report with Trolltech but to be honest I would have thought someone else would have come across this by now if it is a bug.

guilugi
28th March 2007, 12:12
Yes, that's definitely strange :)

But maybe this bug appeared in a recent Qt release, through QTextBrowser modifications : that would explain the lack of answers...

Guilugi.

patrik08
28th March 2007, 15:30
You have a lot of way to discovery link....

via currentCharFormatChanged each click emit this signal ....
evry link hare underline style! probe to make a link not underline ... next reload is underline ....
&& via click on lastcursor.currentFrame(); or QTextCharFormat

basicly QTextCharFormat say all.. http://doc.trolltech.com/4.2/qtextcharformat.html

have a look on svn xxx https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/html_editor/
subversion.... view modus like firefox or write modus edit use context menu....




void Edit_html::DoubleClick()
{
Eframe = false;
Eimage = false;
Etable = false;

lastcursor = wtext->textCursor();
xytextpos = lastcursor.position(); /* int */
Eframe = lastcursor.currentFrame(); /* bool */
nowimage = lastcursor.charFormat().toImageFormat(); /* bool */
nowtable = lastcursor.currentTable();
Eimage = nowimage.isValid(); /* bool */
Etable = lastcursor.currentTable(); /* bool */

if (bellavista) {
return; /* on view modus not edit! nothing*/
}

if (Eimage) {
Image_mod_Setting(); /* open image setting */
return;
}
if (lastcursor.currentTable()) { /* open table setting */
TableSetting();
return;
}
}

QTextCursor lastcursor;
QTextImageFormat nowimage;
QTextTable *nowtable;


/*
connect(wtext, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)), this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
*/

void Edit_html::currentCharFormatChanged(const QTextCharFormat &format)
{
FontChanged(format.font());
/*colorChanged(format.foreground().color());*/
MakealignmentChanged(wtext->alignment());
}

void Edit_html::FontChanged(const QFont &f)
{
vol_link->setChecked(f.underline()); /* all link are underline */
fontname->setCurrentIndex(fontname->findText(f.family()));
fontsize->setCurrentIndex(fontsize->findText(QString::number(f.pointSize())));
vol_bold->setChecked(f.bold());
vol_italic->setChecked(f.italic());
vol_underline->setChecked(f.underline());
}

clive
29th March 2007, 16:04
Trolltech have confirmed that this is a bug and it is being dealt with.

Just thought I would mention it here in case anyone else is having a similar problem.