2013-01-07 69 views
0

我有一個編碼的YouTube網址(直接向視頻),例如:QUrl和YouTube(QT 4.8)

http%253A%252F%252Fr7---sn-xjpm-q0nl.c.youtube.com%252Fvideoplayback%253Fgcr%253Dde%2526ratebypass%253Dyes%2526newshard%253Dyes%2526source%253Dyoutube%2526fexp%253D920704%25252C912806%25252C928001%25252C922403%25252C922405%25252C929901%25252C913605%25252C929104%25252C929109%25252C913546%25252C913556%25252C908493%25252C908496%25252C920201%25252C913302%25252C919009%25252C911116%25252C901451%25252C902556%2526ipbits%253D8%2526key%253Dyt1%2526id%253De4b675c403014739%2526mv%253Dm%2526cp%253DU0hUTFVTVV9LUENONF9NTVlFOlVHOGtlTmJ2WWpt%2526mt%253D1357567034%2526itag%253D46%2526ms%253Dau%2526expire%253D1357587466%2526sparams%253Dcp%25252Cgcr%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526ip%253D46.59.194.67%2526upn%253DVaZiTmBJt0Y%2526sver%253D3%26quality%3Dhd1080%26itag%3D46%26fallback_host%3Dtc.v19.cache5.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26sig%3D6C3258197CB246FA9531E056083053B1B7EAA9C2.758C7B46E77C4B5CF5C7B0C5CBEDCB5F675559D7 

這上面的URL是在爲QString變量 「STR」,如何可以將其設置爲QUrl?

QUrl url = QUrl::fromPercentEncoding(str.toUtf8()); 

不起作用! An

qDebug() << url.toString(); 

給出(上面)編碼的URL返回而不是人類可顯示!

回答

0

你的網址被編碼了兩次。 %25%的編碼版本。這就是爲什麼你也必須撥打fromPercentEncoding兩次。

http%253A%252F%252F在第一輪後變爲http%3A%2F%2F,第二輪後變爲http://

例子:

#include <QApplication> 
#include <QDebug> 
#include <QUrl> 

int main(int argc, char *argv[]) { 

    QApplication app(argc, argv); 
    QString str="http%253A%252F%252Fr7---sn-xjpm-q0nl.c.youtube.com%252Fvideoplayback%253Fgcr%253Dde%2526ratebypass%253Dyes%2526newshard%253Dyes%2526source%253Dyoutube%2526fexp%253D920704%25252C912806%25252C928001%25252C922403%25252C922405%25252C929901%25252C913605%25252C929104%25252C929109%25252C913546%25252C913556%25252C908493%25252C908496%25252C920201%25252C913302%25252C919009%25252C911116%25252C901451%25252C902556%2526ipbits%253D8%2526key%253Dyt1%2526id%253De4b675c403014739%2526mv%253Dm%2526cp%253DU0hUTFVTVV9LUENONF9NTVlFOlVHOGtlTmJ2WWpt%2526mt%253D1357567034%2526itag%253D46%2526ms%253Dau%2526expire%253D1357587466%2526sparams%253Dcp%25252Cgcr%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526ip%253D46.59.194.67%2526upn%253DVaZiTmBJt0Y%2526sver%253D3%26quality%3Dhd1080%26itag%3D46%26fallback_host%3Dtc.v19.cache5.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26sig%3D6C3258197CB246FA9531E056083053B1B7EAA9C2.758C7B46E77C4B5CF5C7B0C5CBEDCB5F675559D7"; 
    qDebug() << QUrl::fromPercentEncoding(str.toUtf8()); 
    qDebug() << QUrl::fromPercentEncoding(QUrl::fromPercentEncoding(str.toUtf8()).toUtf8()); 
    return app.exec(); 
} 
+0

就是這樣,謝謝! – user1955248