QFileInfo::absoluteFilePath() not working? - c++

I am using Qt 4.7.4. I have a relative file path that I am storing as a QString and I want to later convert it into an absolute file path. However, when I create a QFileInfo object from that QString and call absoluteFilePath(), the path is still not absolute. For example:
QString fn = "..\..\..\..\..\..\App\exampledata\doll\everything-F.wrl";
QFileInfo fi(fn);
QString fn2 = fi.absoluteFilePath();
And now fn2 contains "C:/../../../App/exampledata/doll/everything-F.wrl", while I want it to contain "C:/App/exampledata/doll/everything-F.wrl". I could manually remove the useless dots, but that's tedious.

That's not the function you should be calling if you want a canonical path. You should be using QFileInfo::canonicalFilePath instead.

Related

How to identify whether an item is a file or a folder in QT

I wanted to specify from a path, whether the item is a file or a folder. How can I implement such function which takes a path (QString based input) and tells whether it is a file or folder?
You can use the method QFileInfo::isFile() or QFileInfo::isDir() to accomplish this.
Example:
QFileInfo fi("/your/path/string");
if (fi.exists() && fi.isFile())
{
//Do stuff...
}

Why can't I open another process in Qt framework with this function?

In the Qt framework we're supposed to be able to open another .exe using QProcess. The following doesn't work when I click a button and the callback is called:
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess(this);
QString wordPath = "C:/Program Files/Internet Explorer/iexplore.exe";
process->start(wordPath);
}
However if I change process->start(wordPath) to:
process->start(wordPath, QStringList());
Which is an overload of the same function, it works. The second parameter is supposed to be the arguments passed to the new process you want to start. The only way I can make the single-argument version work it seems is if something is in my PATH variable, because both "explorer.exe" and "msconfig" work. What is the story behind this only working with the second QStringList(), which is just an empty list?
In another SO question I saw a user specifically add an empty string, like this:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
I would love to know what the reason is behind this.
Your path "C:/Program Files/Internet Explorer/iexplore.exe" contains spaces, so when you use the first version the program is interpreted as being "C:/Program" with arguments "Files/Internet" and "Explorer/iexplore.exe".
The second version treats all of the first argument as the program and the QStringList as the arguments.
From the Qt documentation:
Arguments containing spaces must be quoted to be correctly supplied to the new process.
Try
QString wordPath = "\"C:/Program Files/Internet Explorer/iexplore.exe\"";

How to save a QPixmap as a picture in a folder with C++ and Qt?

I'm trying to code a C++ function to save a selected picture to my program directory, using a Qt GUI.
So far, the "save" function I'm using on my QPixmap object won't save anything, and I can't figure out why.
Here is the code :
qImage = new QPixmap(path);
QPixmap qImage2 = qImage->scaled(this->width(),this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
qImage2.toImage();
qImage2.save(QDir::currentPath());
qDebug()<<QDir::currentPath();
Can anyone help me ? :)
QDir::currentPath() returns the current working directory. Obviously, the filename itself is not specified. Simply append the needed filename, for example: QDir::currentPath() + "/123.png"
QPixmap::toImage() is a const method returning a QImage converted from a QPixmap. It literally does nothing useful in your code, remove it or use QImage instead.
QDir::currentPath() returns the current working directory which is not necessarily an application executable directory. Use QCoreApplication::applicationDirPath() instead if you need an executable directory.
Also, as pointed out by Violet Giraffe, there could be write permission issues.

QFiledialog returns the incorrect directory

A snippet of what i'm using looks like this
QDir lastDir;
QFileDialog dial(this);
dial.getOpenFileName(this,
tr("Open File"),
QString("/home"),
tr("Raw Images (*.nef *.NEF *.dng *.DNG)"));
lastDir = dial.directory();
qDebug() << lastDir;
The output, is completely wrong, no matter which directory I end up in. However, the incorrect directory is always the same.
AFAICT i'm doing nothing wrong here. What is going on here? Cheers
getOpenFileName() is a static function which immediately opens a "file picker" dialog and returns, once the user is finished with the dialog, "an existing file selected by the user". You use it like this (note the use of :: and the class name QFileDialog instead of the object name):
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"),
QString("/home"),
tr("Raw Images (*.nef *.NEF *.dng *.DNG)"));
directory() is non-static and returns "the directory currently being displayed in the dialog". This function is meant to be called while the dialog is still open, it's intended for use cases which are not covered by the static calls.
What is happening here is you have instantiated an object, called a static function on it (which won't affect its state), and then called directory() which will just reflect the original state of the object, which is probably the working directory. Instead, you need to store the return value of the getOpenFileName() call in a variable, as shown above.
If you want to ask the user to just choose a directory, you could consider using getExistingDirectory() instead. Alternatively, if you want to extract the directory from the filename, the QDir class has some functions useful for this.

How to get the file/resource path for a QIcon

So let's say I do something like this:
QIcon myIcon(":/resources/icon.ico");
How can I later determine the path for that icon, e.g.
QString path = myIcon.getPath();
The problem is, there is no getPath() member, and I can't find anything similar, but surely there must be a way!
I guess I could inherit the QIcon class and add this functionality, but it's probably built in and I'm overlooking it?
The filename isn't stored in QIcon, it's only used for constructing the image.