QUrl and YouTube (Qt 4.8) - qt

I have an encoded YouTube url (directly to the video), example:
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
This above url is in a QString variable "str", how can I set it to QUrl?
QUrl url = QUrl::fromPercentEncoding(str.toUtf8());
doesn't work! An
qDebug() << url.toString();
gives the (above) encoded url back and not the human displayable!

Your url is encoded twice. %25 is the encoded version of %. That's why you have to call fromPercentEncoding twice, too.
http%253A%252F%252F becomes http%3A%2F%2F after the first round and http:// after the second one.
Example:
#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();
}

Related

How to show every qDebug with new window?

I make a QT console program.
I want to show every command by new window.
How could I do that ??
main.cpp
I use system is win10 with QT.
#include <QCoreApplication>
#include <QDebug> //在文字視窗輸出文字功能函式
#include <QDir> //搜尋資料夾功能函式
#include <QFileInfo> //顯示檔案資訊
#include <QString> //字串函式
#include <QStringList> //字串陣列函式
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir; //設定資料夾位置
qDebug() << mDir.exists(); //輸出確定是否有mDir資料夾
QString command = "frtomem -p WnaYooAQ ";
foreach(QFileInfo mitm, mDir.entryInfoList(Qfilter)) //列出所有資料夾中篩選的檔案內容
{
qDebug() << command << mitm.fileName();
}
return a.exec();
}
I just can show it by one console window.
can't separete it.

Qt - Creating PDF file

I want to create a PDF-file in Qt console application. But file size of created file is 0B and I can't open it.
The same code in GUI application works. Where is the difference between code in console and gui application? What should I do to make the code working in console application?
Thanks for help in advance!
#include <QCoreApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}
Here it's working.
#include <QApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}
so it looks like you have just to change the QCoreApplication by QApplication.

Setting LocalStorage Location via setOfflineStoragePath

I'm trying to set the LocalStorage location (sqlite db) for my QML application but once i rebuild and run the application i still can't see the subfolder, INI file and the sqlite DB created on the desired location (in a subfolder within the resources folder). Here is what's in my main file.
Appreciate any one could pint what's I'm doing wrong here?
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//engine.setOfflineStoragePath("qrc:/");
auto offlineStoragePath = QUrl::fromLocalFile(engine.offlineStoragePath());
engine.rootContext()->setContextProperty("offlineStoragePath", offlineStoragePath);
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
return app.exec();
}
By looking at the code snippet in your question, everything looks fine.
Anyway, you should verify if the following line actually returns true as you expect:
dir.mkpath(QString(customPath)
If no, the body of the if statement isn't executed in any case, thus setOfflineStoragePath is never invoked.
As a hint, using qrc:/OffLineStorage as a path for your storage doesn't seem to be a good idea. I'm not sure it will work once in the production environment (to be checked, it sounds strange, but it could work).
Try use engine.setOfflineStoragePath before engine.load.
Using qrc:/OffLineStorage as a path for your storage doesn't seem to
be a good idea. I'm not sure it will work once in the production
environment
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.clearComponentCache();
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

Why nothing is display when I use QWebpage

it's very stupid question but I don't understand why nothing is display in my qt console application when I use QWebpage :
This is my basic code :
#include <QCoreApplication>
#include <QtWebKitWidgets>
#include <QDebug>
#include <stdio.h>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/*QWebPage page;
page.settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
page.settings()->setAttribute(QWebSettings::AutoLoadImages, true);
page.settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
page.settings()->setAttribute(QWebSettings::PrintElementBackgrounds, true);
page.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
page.mainFrame()->load(QUrl("http://myflowerpower.parrot.com/#plantdb/3"));
qDebug () << "source html : \n";
qDebug() << page.currentFrame()->toHtml();
QString htmlResult = page.currentFrame()->toHtml();*/
printf("test");
std::cout << "test std::cout\n";
qDebug() << "Debug Message";
qWarning() << "Warning Message";
qCritical() << "Critical Error Message";
bool result = a.exec();
return result;
}
If I uncomment the code, nothing is display, but if I comment QWebpage stuff it's work without problem. Have any idea ?
QCoreApplication can't handle GUI elements.
You need to use QApplication instead to support the GUI and the event framework.
And make sure that you have what is needed in your .pro file.
QT += widgets webkitwidgets
Hope that helps.

Qt is successfully creating a file but not writing to it with QTextStream

Hey I'm trying to mess around with Qt and for some reason the following code will create the desired text file, but never writes anything to it. Am I doing something wrong? I believe I've copied the example in the documentation pretty accurately.
qDebug() << output
works as expected, but even though the file is created, nothing is ever written to it.
#include <QCoreApplication>
#include <QtDebug>
#include <QString>
#include <QDateTime>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filename = "";
filename.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
filename.append(".txt");
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
QString output = "TEST";
qDebug() << output;
out << output;
return a.exec();
}
The data does not get written to disk immediately: It sits in a buffer until it's flushed.
Close the file after you've finished writing.
(In my experience, the file is closed anyway when you quit the program, but it's good practice to do this explicitly)

Resources