Paths with ../ does not work on win XP and Qt - qt

I have an application with executable that resides in somwhere/appDir/bin. I want it to open file that is in somwhere/appDir/data. It works fine on Win7, Win8 and Vista, but fails on XP. My code is:
QString dirPath = QCoreApplication::applicationDirPath();
QFile* src;
src = new QFile(dirPath + "/../data/file.txt");
qDebug()<<src->exists();
App crashes when execution gets to src->exists(). When there are no .. in the path everything works fine. Why? Is there any other way to get to the parent directory other than stripping directory name from path string?

You can just remove last directory name from dirPath.
QStringList dirPathSplitted = dirPath.split("/");
dirPathSplitted.removeLast();
QString dirPath = dirPathSplitted.join("/");
src = new QFile(dirPath + "/data/file.txt");
I don't know if this is the best solution, but it might work. If it's not working only on WinXP it might be a bug. You might consider reporting it. But if above works on XP, it should work on others too.

Related

How to set default directory of QFileDialog on Windows WinRT

I want to open a QFileDialog with a specific default directory to select a file. Argument "url" of QFileDialog::getOpenFileUrl seems to be made for that, but I can't make it work. Here is what I've done :
QUrl url("file:///D:/");
QUrl path = QFileDialog::getOpenFileUrl(0, "Open File", url, tr("Database (*.db)"));
It does not work, default directory is not set.
I'm running Windows 10 and I compile with msvc2015 for WinRT platform.
Edit : Seems to be a bug, I filled a ticket : https://bugreports.qt.io/browse/QTBUG-57464
I think working directly with path string is more conventional when it comes to local files because QUrl is designed for dealing with all sorts of URLs. So I suggest using getOpenFileName instead.
QString defaultPath ="D:/";
QString ret = QFileDialog::getOpenFileName(0, "Open File", defaultPath, tr("Database (*.db)"));
But if you need to use QUrl somehow, you can use it like this.
QUrl url= QUrl::fromLocalFile("D:/");
QUrl path = QFileDialog::getOpenFileUrl(0, "Open File", url, tr("Database (*.db)"));
This is currently not possible, probably due to a bug int Qt WinRT port.
See : https://bugreports.qt.io/browse/QTBUG-57464
Once the bug fixed, t.m.'s answer should work

Not able to initialize tess-two (Could not initialize Tesseract API error)

I wrote a sample Android app. I am getting 'Tesseract(native): Could not initialize Tesseract API with language=eng!' error.
I did include
compile 'com.rmtheis:tess-two:5.4.0'
in the gradle file
Also copied all 'data files' 3.04.00 version to 'tessdata' directory.
I debugged Java portion of 'init' code it seems to be working fine, it's failing inside 'nativeside'.
Any suggestions what could be going wrong with my code. Here are few lines of code I am using to init
final String lang = "eng";
TessBaseAPI baseApi = new TessBaseAPI();
File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String externalDirPath = externalDir.getAbsolutePath() + "/";
flag = baseApi.init(externalDirPath, lang);
The problem was not with tess-two, it was with my app, I deployed it on Marshmallow, it requires different way to get WRITE_EXTERNAL_STORAGE permission. I was not aware of it, now I fixed that issue my app is working fine.

Loading video using QMediaPlayer and UNC paths

I'm trying to load a video from a network using UNC paths thanks to Qt 5.5 QMediaPlayer.
The code snippet is the following one:
projectDirectory = QFileDialog::getExistingDirectory (this,
tr ("Choose project folder (sensor + video data"),
QDir::homePath(), QFileDialog::ShowDirsOnly);
QDir dir(projectDirectory);
QStringList test = dir.entryList();
qDebug () << projectDirectory << "contains:" << endl << test;
mediaPlayer.setMedia(QUrl::fromLocalFile(projectDirectory+"/video.mov"));
The code snippet works for a local file but doesn't work when the path begins with //.
Example output:
"//m4800/Partage/111" contains:
(".", "..", "HandBrake.txt", "sensors.csv", "video.mov")
DirectShowPlayerService::doSetUrlSource: Unresolved error code 80004005
Note that I am able to read the sensors.csv text file and that video.mov has the same permissions.
Instead of
mediaPlayer.setMedia(QUrl::fromLocalFile(projectDirectory+"/video.mov"));
remove ::fromLocalFile and try
mediaPlayer.setMedia(QUrl(projectDirectory+"/video.mov"));
This seems to solve the problem. In the codebase I am working on, we have added a check for "//" at the start of the raw path before creating the URL to check it is a UNC path and still use the fromLocalFile if it is not.
The DirectShow library does not appear to correctly support UNC paths.
You either have to copy the file to a local temp folder or load the file into a QByteArray and stream from there.
Neither is a great solution and Microsoft depreciated DirectShow in favour of Media Foundation (which has limited playback support at this time).

Qt-application only works correctly if started from the command line

I made a pretty simple application with Qt Creator on Ubuntu 12.04. The application reads an xml-file and shows a couple of images. But when I try to start the application by double clicking the icon on a different machine (running Lubuntu), the images are not shown, and the xml-file is not read. The application does work properly when it is started from the command line by typing ./App.
Why does it behave like this and how do I fix it?
edit: The method that reads the xml:
QDomDocument doc("document");
QString path = "datastorage.xml"; // xml is in same directory as the executable
QFile xmlFile(path);
if (!xmlFile.open(QIODevice::ReadOnly))
throw QString("Error with XML: Could not open file " + path);
if (!doc.setContent(&xmlFile)) {
xmlFile.close();
throw QString("Error with XML: Could not set QDomDocument content from " + path);
}
xmlFile.close();
QDomElement root = doc.documentElement();
return root;
Simply you are using relative paths to read files and those paths are always relative to "working directory". If you're launching your app from console, and all required files are within app directory then everything works. When launching from desktop working directory may be different. Just prepend QCoreApplication::applicationDirPath() to all paths you're using.

QSettings - where is the location of the ini file?

I'm using QSettings to store some data as ini file in Windows.
I want to see the ini file, but I don't know what is the location of the ini file.
This is my code:
QSettings *set = new QSettings(QSettings::IniFormat, QSettings::UserScope, "bbb", "aaa");
set->setValue("size", size());
set->setValue("pos", pos());
Where do I have to look? Or may be I miss the code which write it to the file?
When does the QSettings write its values?
To print out the exact location of your settings file use method fileName method of QSettings class.
QSettings settings("folderName", "fileName");
qDebug() << settings.fileName();
Console output looks then like:
/home/user/.config/folderName/fileName.conf
I think you'll find everything you're looking for here : http://doc.qt.io/archives/qt-4.7/qsettings.html
It's plateform specific, see under :
Platform-Specific Notes
Locations Where Application Settings Are Stored
You can store Settings in files as well :
QSettings settings("/home/petra/misc/myapp.ini",
QSettings::IniFormat);
QSettings save location changes to the QSettings.Scope enum. QSettings save to the Local scope by default. On Linux, I found my local settings in:
~/.config/CompanyName/ApplicationName.conf
If you create a QSettings without giving any specific path, the ini file will be located in the application path.
QSettings Settings("myapp.ini", QSettings::IniFormat);
Settings.setValue("Test", "data");
//...
qDebug() << QApplication::applicationDirPath();
Be careful though : the application path might change : for instance, if you are developping your app with Qt Creator, in debug mode, the application path is in the /debug subfolder.
If you are running it in release mode, the application path is in the /release subfolder.
And when your application is deployed, by default, the application path is in the same folder as the executable (at least for Windows).
Check out the QStandardPaths class, it links to multiple standard paths including configuration on all supported platforms. https://doc.qt.io/qt-5/qstandardpaths.html
QT >= 5.5:
QString path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QT < 5.5:
QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
There are paths for config files in shared config directories, application data directories, and more.
In linux you can use this snippet or insert this lines into your main code for find location of your file with python.
from PyQt5.QtCore import QSettings
settings = QSettings("Organization Name", "App name")
print(QSettings.fileName(settings))
It should return an output like this.
/$HOME/.config/Organization Name/App name.conf
Source
in windows path is like below:
C:\Users\user_name\AppData\Roaming\bbb
On Mac OSX, I found the file under at ~/Library/Preferences
The QSettings class provides persistent platform-independent application settings.
Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in XML preferences files on Mac OS X. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files
http://doc.qt.io/archives/qt-4.7/qsettings.html
On Windows without providing an ini filename, you'll find the data in the registry.
Using this code snippet:
int red = color.red();
int green = color.green();
int blue = color.blue();
QSettings settings("Joe", "SettingsDemo");
qDebug() << settings.fileName();
settings.beginGroup("ButtonColor");
settings.setValue("button1r", red);
settings.setValue("button1g", green);
settings.setValue("button1b", blue);
settings.endGroup();
After running this code, you'll see the output:
"\\HKEY_CURRENT_USER\\Software\\Joe\\SettingsDemo"
Now, opening the regedit tool and following the path list you got: 1

Resources