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:

Qt Code:
  1. public class LocalNetworkReply extends QNetworkReply {
  2.  
  3. private String content;
  4. private int offset = 0;
  5.  
  6. public LocalNetworkReply(Operation op, QNetworkRequest request, QIODevice outgoingData){
  7. super();
  8.  
  9. this.content = "<html><head><title>Test</title></head><body>This is a test.</body></html>";
  10. this.offset = 0;
  11.  
  12. this.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "text/html; charset=UTF-8");
  13. this.setHeader(QNetworkRequest.KnownHeaders.ContentLengthHeader, (this.content.length()));
  14.  
  15.  
  16. QIODevice.OpenModeFlag[] flags = new QIODevice.OpenModeFlag[2];
  17. flags[0] = QIODevice.OpenModeFlag.ReadOnly;
  18. flags[1] = QIODevice.OpenModeFlag.Unbuffered;
  19.  
  20. QTimer.singleShot(0, this, "readyRead()");
  21. QTimer.singleShot(0, this, "finished()");
  22.  
  23. this.open(flags);
  24. this.setUrl(request.url());
  25. }
  26.  
  27. @Override
  28. public boolean isSequential() {
  29. return true;
  30. }
  31.  
  32. @Override
  33. public long bytesAvailable() {
  34. return this.content.getBytes().length - this.offset;
  35. }
  36.  
  37. @Override
  38. public void abort() {
  39. System.out.println("Abort called");
  40. }
  41.  
  42. @Override
  43. protected int readData(byte[] data) {
  44. try {
  45. data = this.content.getBytes("UTF-8");
  46. System.out.println(new String(data,"UTF-8"));
  47. System.out.println(data.length);
  48. System.out.println(this.content.getBytes().length);
  49. } catch (UnsupportedEncodingException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. return data.length;
  54. }
  55. }
To copy to clipboard, switch view to plain text mode 

LocalNetworkAccessManager:

Qt Code:
  1. public class LocalNetworkAccessManager extends QNetworkAccessManager {
  2.  
  3. public LocalNetworkAccessManager(){
  4. super();
  5. }
  6.  
  7. public LocalNetworkAccessManager(QObject qObject){
  8. super(qObject);
  9.  
  10.  
  11. }
  12.  
  13. @Override
  14. protected QNetworkReply createRequest(Operation op,
  15. QNetworkRequest request, QIODevice outgoingData) {
  16.  
  17. if(request.url().scheme().equalsIgnoreCase("local") && op.compareTo(QNetworkAccessManager.Operation.GetOperation) == 0){
  18. LocalNetworkReply lnr = new LocalNetworkReply(op, request, outgoingData);
  19. return lnr;
  20. }else{
  21. return super.createRequest(op, request, outgoingData);
  22. }
  23. }
  24. }
To copy to clipboard, switch view to plain text mode 

Application Code:
Qt Code:
  1. .....
  2. browser = new QWebView();
  3. browser.page().setNetworkAccessManager(new LocalNetworkAccessManager());
  4. .....
  5. browser.load(new QUrl("local://index.html"));
  6. .....
To copy to clipboard, switch view to plain text mode 

index.html
Qt Code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Hello World</title>
  6. </head>
  7. <body>
  8. <h1>Hello World!</h1>
  9. </body>
  10. </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?