PDA

View Full Version : QWebKit shows raw text



prof.ebral
7th March 2010, 09:35
I am having a strange problem with QWebKit. When I send and receive HTML data through my network, WebKit is displaying it as raw text. I admit my current network recoding is premature, but I have successfully connected to the server with an older client software and that software shows the HTML as it should.

When I setHtml from the client itself, the HTML looks as it should. The problem currently occurs only with the data being sent across the network.

FYI: I am using WebKit as part of the chat so that it is a rich text environment.

I wanted to try setContent instead of setHtml, but I am not sure how to set the mimetype. Needless to say, I am using setContent wrong.

Any pointers?

ad5xj
7th March 2010, 17:18
Your problem may be in inappropriate usage. Try using QWebView to view the pages instead.

prof.ebral
7th March 2010, 17:31
I have tried using WebView.setHtml also, that is what I tried first. No matter how I try it, the text that is sent across the network seems to loose it's HTML code and becomes raw text. Or, Plain text for that matter .. the HTML code is gone and all I see is the text.

ad5xj
11th March 2010, 15:43
I have used the QWebView widget often and it works fine. Perhaps an example would be helpful.

Here is a QWebView widget that loads an animated gif from the web and shows it in a parent object (in my case a tab widget).



void DXCentral::createStereo()
{
// Create Web Content View //
webStereo = new QWebView(tabTen);
webStereo->setObjectName("webStereo");
webStereo->setGeometry(3,3,260,260);
connect(webStereo, SIGNAL(loadFinished(bool)), this, SLOT(slotShowStereo()));

webStereo->load(QUrl("http://stereo-ssc.nascom.nasa.gov/beacon/euvi_195_rotated.gif"));
}

void DXCentral::slotShowStereo()
{
webStereo->show();
}


As you can see, the object is created on a tabWidget, the URL is set and when the QWebView has it loaded - the .show() is called to make it visible. Try to substitute your URL and parent and see if this works for you.

prof.ebral
11th March 2010, 17:59
I am not using a URL in this example. I am sending a string of text that contains HTML code.

JD2000
11th March 2010, 18:22
Try the setHtml function to load your HTML in that case.

prof.ebral
11th March 2010, 18:53
I have tried using WebView.setHtml also, that is what I tried first. No matter how I try it, the text that is sent across the network seems to loose it's HTML code and becomes raw text. Or, Plain text for that matter .. the HTML code is gone and all I see is the text.


Try the setHtml function to load your HTML in that case.

I have, mentioned that.

ad5xj
12th March 2010, 15:53
Then I think should use the QUrl class and retrieve unencoded text with the fromFile() method.

prof.ebral
13th March 2010, 02:43
That's not how it works. I have no plans of Person A sending data to Person B and Person B's client putting that data into a file so it can be read. That sounds insecure.

Honestly it sounds more like it's either a bug in Qt, or I am doing it wrong. If I connect with an older client the HTML string being sent works, it's only with the newer clients.

ad5xj
17th March 2010, 18:28
As I re-read your posts it strikes me that I overlooked that you wanted to use Rich Text - NOT HTML. I think you mentioned that the use of HTML was to display Rich Text in your chat over the network.

Is this the case? Are you having trouble with displaying transferred rich text at the receiving end of the chat exchange? Or are you mistakenly using HTML to get a richer text result?

wysota
17th March 2010, 19:23
Can we see some code?

prof.ebral
22nd March 2010, 09:54
As I re-read your posts it strikes me that I overlooked that you wanted to use Rich Text - NOT HTML. I think you mentioned that the use of HTML was to display Rich Text in your chat over the network.

Is this the case? Are you having trouble with displaying transferred rich text at the receiving end of the chat exchange? Or are you mistakenly using HTML to get a richer text result?
not RTF.


Can we see some code?
Yeah.. after I catch some Zs. I just recoded a horribly implemented Server <-> Client script, and while a bit buggy on my end, I have a solid foundation created for a pure Python, stackable, socket based Server <-> Client network.

prof.ebral
24th March 2010, 04:32
So, I am not sure how much code you really need to see here.


def buildHead(self):
with open(path['chat_styles']+'chat.css', 'r') as f:
css = f.read()
self.head = '<style type="text/css">\n'
self.head += css+'\n'
self.head += '</style>'

def Post(self, msg):
tabIndex = self.parent.center2.chatView.currentIndex()
tabText = str(self.parent.center2.chatView.tabText(tabIndex) )
chatTabs = self.parent.center2.chatTabs
chatTabs['Main Room'].chatBuffer.append(msg)
chatTabs['Main Room'].scrollView(self.head, chatTabs[tabText].chatBuffer)


I build a head that will be the users defined CSS. In this test model I just send the chat to the 'Main Room' tab. The chat messages are placed inside a list called chatBuffer and then the list is put together.



def scrollView(self, message, chatBuffer):
while len(chatBuffer) > 10: chatBuffer.pop(0) # Currently shows only last 10 messages
message += ''.join(chatBuffer)
self.viewFrame.setHtml(message)
self.viewFrame.scroll(0, 1000)


This allows me the user to create a chat buffer, so 1,000's of lines of chat are not soaking of their memory. Then I set the HTML in the view frame .. and I get raw text. Do you need to see more code?

edit: Just a quick not. Oddly the setHtml works when the data comes from the client itself, but when the data is transmitted over the network it does not. I may still have an error on my end.

wysota
24th March 2010, 08:20
Dump the contents of the message variable to the console before you set it as html on self.viewFrame to see if the html code is complete.

prof.ebral
24th March 2010, 10:15
It is complete.

EDIT: I am almost done with the network's changes. I'll drop this issue and come back to it later. Thanks for all the help so far.

wysota
24th March 2010, 11:55
It is complete.
Did you verify it with some validator service? Maybe there is something that you simply can't see that causes the problems.

prof.ebral
24th March 2010, 12:56
I will finish the network and create a new thread. The new client is having it's own problems. It just seems odd that the string would print to the console as HTML, but when I use that string to set the HTML I get raw text. I don't even get the HTML code in the raw text, just HTML stripped raw text.

prof.ebral
6th February 2011, 10:12
I know this thread is nearly a year old, but I wanted to post my answer to my own problem in hopes that it helps someone else in the future. So please don't bombard me with thread necromancy.

As it turns out the problem is in the setHtml method and the fact it is occurring within a threaded method. The setHtml method cannot operate outside of the Main GUI thread, so in order for me to post the correct HTML I need to use signals and slots. All is working much better now and I hope to use WebKit as a strong platform for my rich text chat box. :o