Always empty metadata - c++

I'm writing audio player using QtMutimedia, and need got audio file metadata.
I'm using this code, but always returns empty strings on every file. How fix this?
Qt 5.15.2 MinGw
QStringList meta = player->availableMetaData();
qDebug() << "File meta: ";
foreach(QString data, meta){
qDebug() << data;
}

Related

QT Creator - Parsing through CSV file

OK, so I am using QT Creator for C++ and I am making a function that allows me to parse through the CSV file that I have named getExcelFile. Everything else is working fine but my code will not enter my while loop for some reason which is driving me crazy. Some suggestions would be helpful! Thanks.
void Widget::getExcelFile(){
//Name of the Qfile object.
//filename is the directory of the file once it has been selected in prompt
QFile thefile(filename);
//If the file isn't successfully open
if (thefile.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "File opened successfully";
//Converts text file to stream
QTextStream in(&thefile);
fileContent=in.readAll();
QString line;
while (!in.atEnd()){
//line = textStream.readLine();//reads line from file
//Will not enter this loop for some odd reason.
qDebug() << "This text does not print out";
}
}
qDebug() << "This prints out successfully";
ui->textEdit->setPlainText(fileContent);
}
You did in.readAll(), after that call in.atEnd() will return true. Either remove in.readlAll() or while loop, why do you need both?

QDataStream unable to serialize data

I am trying to follow the tutorial here and serialize Qt objects. Here is my code:
QFile file("/Users/kaustav/Desktop/boo.dat");
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Cannot open file for writing: "
<< qPrintable(file.errorString()) << endl; //no error message gets printed
return 0;
}
QDataStream out(&file); // we will serialize the data into the file
out.setVersion(QDataStream::Qt_5_3); //adding this makes no difference
out << QString("the answer is"); // serialize a string
out << (qint32)42;
When I run this program, the file gets created in my desktop all right, but its size is 0 kB, it is blank. Naturally, when I then try this:
QFile file("/Users/kaustav/Desktop/boo.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file
in.setVersion(QDataStream::Qt_5_3);
QString str;
qint32 w;
in >> str >> w;
I get a blank string in str. What am I doing wrong? If of any help, I am using Qt Creator 3.1.1 based on Qt 5.2.1.
Check if there are any errors returned when calling open and ensure you close the file with file.close() when you're finished with it.
As you're using Qt 5, you should really use QSaveFile instead, when saving the data.

Can't extract exe or dll files with quazip (Qt)

I am building a qt framework to download and install application updates (like sparkle for obj-c). The download works, the downloaded zip file is valid and i can extract the contents manually but when I let my framework unzip the contents via quazip the files (dll and exe) contains this and only this string: "MZ" and a special char which is wrong encoded (some kind of square on windows and "ê" on mac), so exactly 3 bytes. When I include a text file (or xml) in the zip file, it will be unzipped correctly, manually and with quazip, so I assume that the library was compiled correctly. Where is my error?
I think this can be part of the solution http://en.wikipedia.org/wiki/DOS_MZ_executable?
Here is my method to install the update:
QuaZip archiveWrapper(filename); // The downloaded zip file
if (archiveWrapper.open(QuaZip::mdUnzip)) {
QuaZipFile archive(&archiveWrapper);
qDebug() << "Extracting files" << archiveWrapper.getFileNameList();
for (bool more = archiveWrapper.goToFirstFile(); more; more = archiveWrapper.goToNextFile()) {
QString filePath = archiveWrapper.getCurrentFileName();
QString destinationPath = QDir::cleanPath(QDir::currentPath() + QDir::separator() + filePath);
QString destinationBackup = destinationPath + "_backup";
qDebug() << "Extract" << filePath << "to" << destinationPath;
QuaZipFile zip(archive.getZipName(), filePath);
zip.open(QIODevice::ReadOnly);
QByteArray data = zip.readAll();
zip.close();
QFile oldFile(destinationPath);
if (oldFile.exists()) {
qDebug() << "Rename" << destinationPath << "to" << destinationBackup;
if (!oldFile.rename(destinationBackup)) {
qWarning("Could not rename %s to %s!", destinationPath.toUtf8().constData(), destinationBackup.toUtf8().constData());
}
}
QFile destination(destinationPath);
destination.open(QIODevice::WriteOnly);
destination.write(data.data());
destination.close();
if (oldFile.exists()) {
qDebug() << "Deleting backup of" << destinationPath;
if (!oldFile.remove()) {
qWarning("Could not delete %s!", destinationPath.toUtf8().constData());
}
}
}
if (archive.getZipError() == UNZ_OK) {
qDebug() << "All files extracted successfully";
qDebug() << "Restarting application...";
archiveWrapper.close();
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
} else {
qWarning("Error while extracting files (Error %d)", archive.getZipError());
archiveWrapper.close();
}
} else {
qWarning("Could not open archive to extract contents");
}
Edit:
I found out that data (QByteArray) has the expected size, so I think the problem is that QFile does not write the contents of QByteArray into the exe/dll files the way it should be?
Edit 2:
I've found one error, the file size to write:
destination.write(data.data(), data.size());
instead of
destination.write(data.data());
but still, the exe does not have an icon or is executable (but with the correct file size). For a short time a dos window opens and closes. There is a antivirus software running but there is no alert (and because this is a corporate notebook i am not able to shut it down and the update framework should also be running whether there is a antivirus software running or not).
Edit 3:
Although I thought writing exe files is complicated, it was a mixture of stupid bugs I "implemented" for testing purposes. So the simple
QFile destination(destinationPath);
destination.open(QIODevice::WriteOnly);
destination.write(data.data(), data.size())
is sufficient.

File retrieving in MongoDB using C++ Drivers

I am doing one project to make a database application in which i need to store some files in the database and later i need to retrieve those files back to the local system. I am using C++ drivers of the mongoDB..Storing the files on the server is working perfectly and when i am trying to retrieve it back only text files are received as it is but when i am doing it with images, .pdf files or other files format the file is corrupt.Can anyone tell me how i can save the files on local system without any corruption. Thanks
Code:
std::fstream out;
const char* gridfilename="Penguins.jpg";
const char* filename="Temp.jpg";
out.open(filename, ios::out);
DBClientConnection c;
c.connect("localhost");
cout << "connected ok" <<endl;
GridFS gfs = GridFS(c, "Test", "DB");
GridFile gf = gfs.findFile(gridfilename);
if (true != gf.exists()) {
cerr << "There is no file like " << gridfilename << endl;
}
gf.write(out);
out.close();

Qt how to get application to get md5 checksum of itself

Working on a Qt application. I'm trying to get the exe file to return an md5 checksum of itself while it's running. How can I do this?
I tried this:
QFile theFile("file.exe");
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
thisFile = theFile.readAll();
}
else
{
qDebug() << "Can't open";
}
qDebug() << QString("%1").arg(thisFile.length());
fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex().toUpper());
qDebug() << fileMd5;
This does not return the correct value, however.
Update:
I got it working with other files. The problem seems to be that I am unable to read the exe while it's running.
Final update:
This is the solution:
QFile theFile(QCoreApplication::applicationFilePath());
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
thisFile = theFile.readAll();
}
else
{
qDebug() << "Can't open file.";
}
QString fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex());
qDebug() << fileMd5;
You forgot to call open on theFile.
if (!theFile.open(QIODevice::ReadOnly))
// Handle error here
Also, you should be using QCoreApplication::applicationFilePath() to get the path to the executable.
You have to create an independent application (let's call it myApp) which check the MD5sum and compare it with your PHP script and ask for an update if needed or load directly the application.
Like so : myApp=> need update ? (update) : (TheRealApp)
Ok, looks like it just wasn't finding the file. I tried an absolute path instead of a relative and it worked. I'll have to figure out what's going wrong, but it looks like it can read itself while running.