I'm trying to add a custom protocol to QtWebView called "local://". I followed an example for python I found here:
http://www.diotavelli.net/PyQtWiki/U...ith%20QtWebKit
and converted it to Java. It kind of works, but there is some sort of character encoding issues I can't figure out. Here is the example Java code I created....
LocalNetworkReply:
public class LocalNetworkReply extends QNetworkReply {
private String content;
private int offset = 0;
public LocalNetworkReply
(Operation op, QNetworkRequest request,
QIODevice outgoingData
){ super();
this.content = "<html><head><title>Test</title></head><body>This is a test.</body></html>";
this.offset = 0;
this.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "text/html; charset=UTF-8");
this.setHeader(QNetworkRequest.KnownHeaders.ContentLengthHeader, (this.content.length()));
flags
[1] = QIODevice.
OpenModeFlag.
Unbuffered;
QTimer.
singleShot(0,
this,
"readyRead()");
QTimer.
singleShot(0,
this,
"finished()");
this.open(flags);
this.setUrl(request.url());
}
@Override
public boolean isSequential() {
return true;
}
@Override
public long bytesAvailable() {
return this.content.getBytes().length - this.offset;
}
@Override
public void abort() {
System.out.println("Abort called");
}
@Override
protected int readData(byte[] data) {
try {
data = this.content.getBytes("UTF-8");
System.out.println(new String(data,"UTF-8"));
System.out.println(data.length);
System.out.println(this.content.getBytes().length);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data.length;
}
}
public class LocalNetworkReply extends QNetworkReply {
private String content;
private int offset = 0;
public LocalNetworkReply(Operation op, QNetworkRequest request, QIODevice outgoingData){
super();
this.content = "<html><head><title>Test</title></head><body>This is a test.</body></html>";
this.offset = 0;
this.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "text/html; charset=UTF-8");
this.setHeader(QNetworkRequest.KnownHeaders.ContentLengthHeader, (this.content.length()));
QIODevice.OpenModeFlag[] flags = new QIODevice.OpenModeFlag[2];
flags[0] = QIODevice.OpenModeFlag.ReadOnly;
flags[1] = QIODevice.OpenModeFlag.Unbuffered;
QTimer.singleShot(0, this, "readyRead()");
QTimer.singleShot(0, this, "finished()");
this.open(flags);
this.setUrl(request.url());
}
@Override
public boolean isSequential() {
return true;
}
@Override
public long bytesAvailable() {
return this.content.getBytes().length - this.offset;
}
@Override
public void abort() {
System.out.println("Abort called");
}
@Override
protected int readData(byte[] data) {
try {
data = this.content.getBytes("UTF-8");
System.out.println(new String(data,"UTF-8"));
System.out.println(data.length);
System.out.println(this.content.getBytes().length);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data.length;
}
}
To copy to clipboard, switch view to plain text mode
LocalNetworkAccessManager:
public class LocalNetworkAccessManager extends QNetworkAccessManager {
public LocalNetworkAccessManager(){
super();
}
public LocalNetworkAccessManager
(QObject qObject
){ super(qObject);
}
@Override
protected QNetworkReply createRequest(Operation op,
QNetworkRequest request,
QIODevice outgoingData
) {
if(request.url().scheme().equalsIgnoreCase("local") && op.compareTo(QNetworkAccessManager.Operation.GetOperation) == 0){
LocalNetworkReply lnr = new LocalNetworkReply(op, request, outgoingData);
return lnr;
}else{
return super.createRequest(op, request, outgoingData);
}
}
}
public class LocalNetworkAccessManager extends QNetworkAccessManager {
public LocalNetworkAccessManager(){
super();
}
public LocalNetworkAccessManager(QObject qObject){
super(qObject);
}
@Override
protected QNetworkReply createRequest(Operation op,
QNetworkRequest request, QIODevice outgoingData) {
if(request.url().scheme().equalsIgnoreCase("local") && op.compareTo(QNetworkAccessManager.Operation.GetOperation) == 0){
LocalNetworkReply lnr = new LocalNetworkReply(op, request, outgoingData);
return lnr;
}else{
return super.createRequest(op, request, outgoingData);
}
}
}
To copy to clipboard, switch view to plain text mode
Application Code:
.....
browser = new QWebView();
browser.page().setNetworkAccessManager(new LocalNetworkAccessManager());
.....
browser.
load(new QUrl("local://index.html"));
.....
.....
browser = new QWebView();
browser.page().setNetworkAccessManager(new LocalNetworkAccessManager());
.....
browser.load(new QUrl("local://index.html"));
.....
To copy to clipboard, switch view to plain text mode
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
To copy to clipboard, switch view to plain text mode
When I run the application, I get a bunch of garbage characters like this:
6Pj� ��� 0�����+ l� ��벼%��ڨ�p��%���k�
Could this be a bug in Qt?
Bookmarks