Display QList contents - c++

I want to display the contents of a QList just like how it is displayed in the console with qDebug()
For example:
QList<QNetworkCookie> cookies = mManager->cookieJar()->cookiesForUrl(mUrl);
qDebug() << "COOKIES for" << mUrl.host() << cookies;
Output:
QNetworkCookie("MSession=kr6i819jbvkorherbe76oh23c7; domain=website.com; path=/)"
Is there a function that I can use?

You can create a QDebug object that will store anything streamed into it, inside a string. Here is it:
QString str;
QDebug dStream(&str);
dStream << mUrl.host();
Now you can put str wherever you want. For example a QTextBrowser:
ui->textBrowser->insertPlainText(str);
This should work everywhere that qDebug() works. Because qDebug() itself returns a QDebug object according to this documentation.

Related

How do you delete a sub-string in a string(qstring) in Qt

Is there a function in Qt similar to delete() and copy() in Delphi.
I am reading data from a device connected to my computer via USB and storing it as a QString. Every line that is read is not the same (or is cut short, even while using readyRead). I created a buffer sting to add these "half sting" (eg. string = "This" instead of "This is a string#") to and now I want to copy the string up until the '#' and then delete the string so if new "half stings" get added I can do the same with them. The code below is what I tried
void MainWindow::readSerial()
{
QByteArray serialData = port->readAll();
serialBuffer += serialData;
QByteArray serialString = serialBuffer.
qDebug() << serialString;
ui -> textEdit ->append(serialString);
//serialBuffer.replace(serialString,"");
}
The above code only returns an empty string.
void MainWindow::readSerial()
{
QByteArray serialData = port->readAll();
serialBuffer += serialData;
QString serialString = serialBuffer.mid(serialBuffer.indexOf("$"),serialBuffer.indexOf("\r\n"));
qDebug()<< "index of \r\n" << serialBuffer.indexOf("\r\n");
qDebug() << "SerialString" <<serialString;
ui -> textEdit ->append(serialString);
qDebug() << "SerialBuffer: " << serialBuffer;
serialBuffer.replace(serialString + "\r\n","");
}
the above code works. Thanks all.
regards

Blackberry 10 contact pic to byte array

I am working on migration application. I have to transfer blackberry 10 contacts to android.
I am getting problem in transferring contact pic. I am getting the uri of the pic, create file and try to read the bytes.
ContactPhoto contactPhoto = contact.primaryPhoto();
QString photo = contactPhoto.originalPhoto();
//photo = file:///accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg
if (!photo.isEmpty()){
QFile file(photo);
if (file.open(QIODevice::ReadOnly)) {
qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
}else{
qDebug() <<"file.readAll() ELSE" <<endl;
}
vcardString += "PHOTO;JPEG;ENCODING=BASE64:" + (file.readAll() + "\n");
}
But else part of the below snip of code is executing
if (file.open(QIODevice::ReadOnly)) {
qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
}else{
qDebug() <<"file.readAll() ELSE" <<endl;
}
How I read bytes from below uri
file:///accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg
As I suspected, removing file:// from the url works.
Here's the code I used to test :
bb::pim::contacts::ContactPhoto contactPhoto = contact.primaryPhoto();
QString photo = contactPhoto.originalPhoto();
if (!photo.isEmpty()){
QFile file(photo.remove("file://"));
if (file.open(QIODevice::ReadOnly)) {
qDebug() <<"file.readAll() IF" <<file.readAll() <<endl;
}else{
qDebug() <<"file.readAll() ELSE" <<endl;
}
}
Please note that you need to leave one / in front of the url, so your picture url shared in OP would look like :
/accounts/1000/pimdata/_startup_data/contacts/2/img-tnqpx0.jpg
Also, if your goal is to create a VCard vcf file, you don't need to manually create the VCard file content, don't even need to read the bytes of the photo file, the contactToVCard function will do that for you.
QByteArray vcard = contactService.contactToVCard(contact.id(), bb::pim::contacts::VCardPhotoEncoding::BASE64, -1);
qDebug() << "vcard:" << vcard;

Qt: empty content of opened QFile for a .txt file from the project resources

I tried this in my project in mainwindow.cpp:
QString dir = ":/nodesDir/nodesDir/";
QFile baseFile(dir + "allNodeNames.txt");
qDebug() << baseFile.exists(); // true
qDebug() << baseFile.readAll(); // ""
but it is wrong, the content of the file is
plusOperator
Why does it say, that there would be nothing written in the file? Or What did I miss in my code?`
Thanks for answers!
In order to read the file you need to open it for it we use open () and we indicate the way we want it to open. We must also bear in mind that the files stored in the resources are read only, so they can not be modified.
QString dir = ":/nodesDir/nodesDir/";
QFile baseFile(dir + "allNodeNames.txt");
qDebug() << baseFile.exists(); // true
qDebug()<< baseFile.open(QFile::ReadOnly);
qDebug() << baseFile.readAll(); // ""
Output:
true
true
"plusOperator"

How do I get the value of text inside of the file using Qt?

The data of my file.txt is as below:
Student_ID=0001
Student_Name=joseph
Student_GradeLevel=2
How do I get the value, let say I want to get the Student_ID using Qt.
Thanks.
Take a look at this function, it can be used to find any value you want in your input file, where all lines are in the format you've posted above (key=value). If the key is not found, it returns an empty QString() object.
QString findValueInFile(QString key, QString filename) {
QFile file(filename);
if(file.open(QIODevice::ReadOnly)) {
QTextStream txtStr(&file);
QStringList fileContent = txtStr.readAll().split('\n');
for(auto &&line : fileContent) {
if(line.contains(key)) return line.split(QChar('='))[1];
}
file.close();
}
return QString(); // not found
}
Now you call it somewhere, e.g.:
qDebug() << findValueInFile("Student_ID", "file.txt");
qDebug() << findValueInFile("Student_Name", "file.txt");
This function can be easily modified if you replace your = sign with other delimiter e.g. => or sth else. However for key=value format there is a special QSettings class (mentioned by sebastian) that can allow you to read those values even easier:
QSettings file("file.txt", QSettings::IniFormat);
qDebug() << file.value("Student_Name").toString(); // et voila!
You can probably also use QSettings, as they are able to read ini files.
There are some caveats though regarding backslashes which might be important to you (though they aren't for the example you posted): http://doc.qt.io/qt-4.8/qsettings.html#Format-enum
QSettings iniFile("myfile.txt", QSettings::IniFormat);
// now get the values by their key
auto studentId = iniFile.value("Student_ID").toString().toInt();
I'm more of a PyQt user, so: apologies if I got some C++ specifics wrong...

QSetting trouble

I am working with QSettings function. Once successfully set in the .h file variable
QSettings *settings;
inside the constructor (cpp. file) I set the variable in this way, to obtain a path like Draw/Input/Cells/Width
settings = new QSettings("MySoft", "Star Runner");
settings->beginGroup("Draw");
settings->beginGroup("Input");
settings->beginGroup("Cells");
settings->setValue("width", 80);
settings->endGroup();
settings->endGroup();
settings->endGroup();
The problem is that width value is properly set to 80 only if during the declaration of the organization name is set to "MySoft": if you assign any other value (e.g. "foobar"), doing a test via
qDebug() << settings->value("width", "").toString();
the width key as no value
You should also start and end the groups when reading the value. So you could either try
qDebug() << settings->value("Draw/Input/Cells/width", "").toString();
or
settings->beginGroup("Draw");
settings->beginGroup("Input");
settings->beginGroup("Cells");
qDebug() << settings->value("width", "").toString();
settings->endGroup();
settings->endGroup();
settings->endGroup();