Qt UTF8 to ANSI coverting and file saving - qt

Here is test code..
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray fileBuffer("일이");
QFile saveFile("test.txt");
if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Text))
return a.exec();
QTextStream stream(&saveFile);
QTextCodec *codec = QTextCodec::codecForName("EUCKR");
QByteArray encodedString = codec->fromUnicode(fileBuffer);
stream << encodedString;
stream.flush();
saveFile.close();
return a.exec();
}
I thought that test.txt encoded EUC-KR .
But when read by note pad (Window application) encoding type is UTF-8, not ANSI.
What is the problem??
I should text file which stored hex code encoded by EUC KR .

Related

Save and load QWebEngineHistory to a QWebEnginePage

I need to save the history of a QWebEnginePage and load it back. Therefore I want to store the history from page A in some structure and set it to page B.
In the documentation I found the following methods:
// Saves the web engine history history into stream.
QDataStream &operator<<(QDataStream &stream, const QWebEngineHistory &history)
// Loads the web engine history from stream into history.
QDataStream &operator>>(QDataStream &stream, QWebEngineHistory &history)
But honestly I don't know how to work with them.
I tried the following:
QWebEnginePage *m_history;
...
...
void setHistory(QWebEngineHistory *history){
QDataStream data;
data << history; //Hoping that the content of data is persistent after deleting of the QWebEnginePage where the history is coming from
data >> m_history;
}
And later on I want to load it back to the page:
m_history >> m_webEnginePage.history(); // Pseudo-Code
I know that the QWebEngineHistory of a QWebEnginePage is const, but then I'm wondering why are there even those two methods from above? Why is there a function that "loads the web engine history into history"?
The only alternative I can think of is storing my history in a QList, but managing this is not nice and could lead to more problems (because of the whole forward/backward button etc).
Thank you very much for your help.
No object can be saved, what is saved is the information associated with the object so you should not create QWebEngineHistory but save and/or load the information.
In the following example, the information is saved in a file when the application is closed and the startup is loaded.
#include <QtWebEngineWidgets>
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc,argv);
const QString filename = "history.bin";
QWebEngineView view;
view.load(QUrl("https://stackoverflow.com"));
{// load
QFile file(filename);
if(file.open(QFile::ReadOnly)){
qDebug() << "load";
QDataStream ds(&file);
ds >> *(view.page()->history());
}
}
view.resize(640, 480);
view.show();
int ret = app.exec();
{// save
QFile file(filename);
if(file.open(QFile::WriteOnly)){
qDebug() << "save";
QDataStream ds(&file);
ds << *(view.page()->history());
}
}
return ret;
}
In the same way you can save it through QSettings:
#include <QtWebEngineWidgets>
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc,argv);
QWebEngineView view;
view.load(QUrl("https://stackoverflow.com"));
{// load
QSettings settings;
QByteArray ba = settings.value("page/history").toByteArray();
QDataStream ds(&ba, QIODevice::ReadOnly);
ds >> *(view.page()->history());
}
view.resize(640, 480);
view.show();
int ret = app.exec();
{// save
QSettings settings;
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << *(view.page()->history());
settings.setValue("page/history", ba);
}
return ret;
}

QFileDialog created without file extension in linux

I am creating a file in both windows and linux using QFileDialog
fileName = QFileDialog::getSaveFileName(this, tr("Create project"), applicationPath,tr("Files (*.MSC)"));
In windows the file is created as path/to/file.MSC
but in linux file is created as path/to/file
why .MSC is not appending in Linux, whether we need to use other function for this
The following example works fine on Linux. You get the file myfile.MSC with the text "test" written on it.
#include <iostream>
#include <QApplication>
#include <QFileDialog>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString applicationPath = QDir::currentPath() + "/myfile.MSC";
QString fileName = QFileDialog::getSaveFileName(0,
QApplication::tr("Create project"),
applicationPath,
QApplication::tr("Files (*.MSC)"));
if (fileName.isEmpty())
return -1;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
std::cout << "error\n";
return -1;
}
QDataStream out(&file);
out << "test";
}
return a.exec();
}

how to read QByteArray and run as Detached process in qt?

I'm trying to run the hardcoded base64 .exe file in a Detached process.
What I'm trying now is:
void Read(QString file){
QProcess process;
process.startDetached(file);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray exe = QByteArray::fromBase64("TVqQAAMAAAAEAAAA...."); //base64 of .exe file
QString s = exe.data();
qDebug() << s ;
Read(s);
return a.exec();
}
not working, debug shows : "MZ?"

QUrl and YouTube (Qt 4.8)

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();
}

QT QString from QDataStream

I'm working with a buffer and I'm trying to get a string from it, but isnt working...
Example:
*void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
// Ok, I caught v value successfuly
QString s;
in >> s;
// Didnt work :<
}*
The string lenght is stored on 2 first bytes...
Thanks
If the string was not written as a QString, you need to read its length and content separately.
quint8 v;
in >> v;
quint16 length = 0;
in >> length;
// the string is probably utf8 or latin
QByteArray buffer(length, Qt::Uninitialized);
in.readRawData(buffer.data(), length);
QString string(buffer);
You might have to change the endianness of the QDataStream with QDataStream::setByteOrder before reading the 16-bit length.
We should really see the writing code and how you create the QDataStream. I tried with the following sample, and in this case your function works very well:
#include <QCoreApplication>
#include <QDebug>
#include <QDataStream>
#include <QBuffer>
void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
qDebug() << v;
// Ok, I caught v value successfuly
QString s;
in >> s;
qDebug() << s;
// Didnt work :<
}
int main(int argc, char ** argv) {
QCoreApplication a(argc, argv);
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
// write test data into the buffer
QDataStream out(&buffer);
quint8 ival = 42;
QString sval = "Qt";
out << ival;
out << sval;
// read back data
buffer.seek(0);
myFunc(out);
return a.exec();
}
Output when executed:
$ ./App
42
"Qt"

Resources