PDA

View Full Version : How to use QMimeData::setHtml?



Vadi
25th January 2011, 00:11
Trying to set my html text as an html mimetype into the clipboard. Text like this works:


QMimeData *md = new QMimeData;
md->setText(*text);
QApplication::clipboard()->setMimeData(md, QClipboard::Clipboard);

html like this:

QMimeData *md = new QMimeData;
md->setHtml(*text);
QApplication::clipboard()->setMimeData(md, QClipboard::Clipboard);

or this:

QMimeData *md = new QMimeData;
md->setData(QLatin1String("text/html"), text->toUtf8());
QApplication::clipboard()->setMimeData(md, QClipboard::Clipboard);

does not work. Any ideas?

wysota
25th January 2011, 00:43
What does "does not work" mean in this situation? What is the content of "text"? By the way, there is no point in using pointers to QString.

Vadi
25th January 2011, 01:08
What doesn't work is that the global clipboard does not receive any new text - pasting into a field doesn't do anything either.

Sample content of text is:


<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'><html><head><style><!-- *{ font-family: 'Courier New', 'Monospace', 'Courier';} *{ white-space: pre-wrap; } *{color:rgb(255,255,255);} *{background-color:rgb(0,0,0);} --></style><meta http-equiv='content-type' content='text/html; charset=utf-8'></head><body><br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> </span><span style="color: rgb(0,0,128); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal">******************************************<br /><br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> </span><span style="color: rgb(0,179,0); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal">Achaea, Dreams of Divine Lands<br /><br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> </span><span style="color: rgb(0,128,128); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal">"Your fate and fame shall be<br /></span><span style="color: rgb(0,128,128); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> an echo and a light unto eternity."<br /><br /></span><span style="color: rgb(0,128,128); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> </span><span style="color: rgb(0,0,128); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal">******************************************<br /><br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> Achaea's IP address is 69.65.42.66<br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> For general questions e-mail support@achaea.com.<br /></span><span style="color: rgb(192,192,192); background: rgb(0,0,0); font-weight: normal; font-style: normal; font-decoration: normal"> 212 adventurers are currently in the<br /></body></html>


Thanks for the quick answer :)

wysota
25th January 2011, 01:56
Would you try something simpler? Like:

int main(int argc, char **argv){
QApplication app(argc, argv);
QClipboard *clip = QApplication::clipboard();
QMimeData *data = new QMimeData;
data->setHtml("<font color='red'>TEST</font>");
clip->setMimeData(data);
return app.exec();
}
And then try pasting it somewhere where html is accepted (like QTextEdit). Don't stop the application before you paste the clipboard contents.

Vadi
25th January 2011, 16:52
Thanks. Seems it's certain text fields of Chrome do not accept it... QTextEdid did, Pidgins input field did (albeit stripped the html... while I have seen it render some before).

So now I'm wondering what is the proper format of the HTML is it wanting...

Added after 29 minutes:

This worked:


QMimeData *md = new QMimeData;
md->setText(*text);
md->setHtml(*text);
QApplication::clipboard()->setMimeData(md, QClipboard::Clipboard);

Thanks for the help.