How to set the directory separator character to match the operating system? - qt

I am writing a qt application, with the goal of it being portable to the 3 major operating systems.
I am using QFileDialog to select a folder, and then adding it to a QListWidget. However the folder name is being returned as E:/media even though I am on Windows. I would want it to return E:\media
I could use a simple string replace, but then on Linux/Mac it would look weird to have \home\user\Documents
My code if it helps:
void LibrariesForm::on_addButton_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Folder"), "/", QFileDialog::ShowDirsOnly);
if (dir.isNull() == true)
{
return;
}
ui->librariesList->addItem(new QListWidgetItem(dir, ui->librariesList, 0));
}

I guess you are looking for QDir::toNativeSeparators().

If you use the string just internally, you don't need to convert slashes to backslashes. Qt's classes work with linux-style pathes, too. If you want a "pretty printed" string, take Jérôme's answer. :)

Related

QSettings INI file: value containing semicolon

I'm trying to read and edit a Desktop Entry .desktop file using Qt QSettings. The problem is that these files contain keys with multiple values separated by semicolon ;. I tried reading these as QStringList but no luck. I only get the first value. For example:
Keywords=disc;cdrom;dvd;burn;audio;video;
Categories=GTK;GNOME;AudioVideo;Audio;Video;DiscBurning;
MimeType=application/x-cd-image;application/x-cdrdao-toc;application/x-cue;application/x-toc;audio/x-scpls;audio/x-ms-asx;audio/x-mp3-playlist;audio/x-mpegurl;application/x-brasero;x-content/audio-cdda;x-content/video-dvd;x-content/video-vcd;x-content/video-svcd;x-content/image-picturecd;
Getting the values with:
settings.value("Desktop Entry/MimeType").toStringList();
settings.value("Desktop Entry/MimeType").toString();
returns only the first value (in my example: disc, GTK or application/x-cd-image).
How to I return the full value from those keys? And how do I write it back using QSettings?
Update (first attempt was completely useless)
Variant 1
QMap<QString, QString> settings;
QFile inFile("<input filename.ini>");
if(inFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inFile);
while (!in.atEnd())
{
QString line = in.readLine();
QStringList linelist = line.split("=");
settings[linelist[0]] = linelist[1];
}
}
Variant 2
use QSettings::registerFormat().
This is probably the only "clean" way to do it with QSettings. The advantage is that you can register it with the .desktop extension. You'll have to write a pair of ReadFunc() and WriteFunc() functions.
I think you can't do it. QSettings has certain interpretation of .ini file format, which is very close to Windows interpretation, and is not meant for generic parsing. Semicolon starts a comment, and apparently QSettings allows comment after value until end of line, and AFAIK there's no way around it.
You need to find a different library to handle .desktop files, or implement one yourself.

lupdate is not getting tr() strings from QObject objects

I've been trying to make a simple text editor using QT Creator. This text editor has a translatable UI that supports 3 languages: English, Spanish, and Portuguese.
My problem is that every time I run lupdate, string literals in my code that are tagged as translatable (i.e. enclosed in tr() ) are parsed and can be edited and translated using QLinguist. However, strings that are part of QObjects (E.g. ButtonText for QFileDialog are not parsed. Am I missing some procedure in order for lupdate to parse these strings?
void PeterTextEditor::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Document"), QDir::currentPath(),
tr("Text documents (*.txt)"), 0, QFileDialog::DontUseNativeDialog);
if (fileName.isNull())
return;
if ( m_fileName.isNull() && !isWindowModified() )
{
loadFile(fileName);
return;
}
else
{
PeterTextEditor * openFile = new PeterTextEditor( fileName );
openFile->show();
openFile->setAttribute(Qt::WA_DeleteOnClose);
}
}
In above example tr("Open Document") would be parsed, but QFileDialog has QButton and QLabel objects with strings for button text and label text.These are not parsed. I would like these to be parsed so I could add translations using QLinguist.
All compiled translation files (*.qm) should be in the /translations directory and you would load them as shown in QFileDialog localization.
Unfortunately, the Qt libraries don't come with all translations for all languages, and, not all the translations supplied are complete.
For the version that I have (Qt 5.2.1), there is a Spanish translation(qt_es.qm) of the libraries but there are many strings that have not been translated yet, and there is no translation file for Portuguese(qt_pt.qm).
The translation of Qt to other languages is an ongoing project so I suggest you search the web and/or other forums to see if anyone has got updated files you can use.
If you cannot find any, and your Spanish translation is missing a couple of strings you need, you can file the source extraction files(*.ts) in the /Src/qttranslations/translations directory. Unfortunately, you probably will find only one for Spanish.
If you're willing to start a Portuguese translation, you can extract all the necessary strings you want by running lupdate on the /Src/qtbase/qtbase.pro file.

BizTalk SourceFileName becomes dehydrated

I'm trying to use a custom filename since i need to create two files (a backupfile) so i followed the following tutorial to create filename Here
now when i test this with DELCUS%MessageID%.txt everything works fine but when i change it to DELCUS%SourceFileName%.txt the interface becomes permanently dehydrated.
the only thing i do for the filename is this
fileName = "ContExt" + System.DateTime.Now.ToString();
Message_send_Belspeed_BeautDay_ContExt(FILE.ReceivedFileName) =
fileName;
is there any reason why the use of SourceFileName would cause this to dehydrate?
Found the issue.
after a while the interface did crash and the filename looks like
DELCUS2012 10:50:40.txt
having : in a filename is not good.
This is just a standard windows file naming limitation, you can't name your file using any of these characters "\ / : * ? " < > |". So obviously your instances are going to get stuck!!

QDir::exists returns true for invalid directory

I am having problem with checking the existence of the directory.
i take path from user input (e.g QLineEdit) and check for directory exists or not and if user specify the path "K:\" (k drive does not exists at all) my code becomes like this
QDir tmp("K:\\");
if(tmp.exists())
return true;
else
return false;
Ideally it should return false, as the Driver Letter "K" is not mapped, but unfortunately it returns true all the time, does anyone have any idea why is it like that? or
what is the correct method to check the existence of the directory?
Two suggestions:
Try tmp.makeAbsolute()
If that doesn't work, try substituting QDir tmp ("K:/"); (Unix forward slash instead of DOs/Windows backslash).
More beautiful way to check for drives is QDir::drives() method. Also you need to use platform independent directory seperator for QDir::exists().

qstring white space

QString fe = "C:\\Program Files\\Autodesk\\Maya2008\\bin\\imconvert.exe ";
This line gives a problem because of space between Program and Files. How is it possible to decode it so that it is treated as one complete string
Brgds,
kNish
You can use a QFileInfo object to store it. This object accepts all path even if they have spaces. In addition you have some functions to do all checks you need before using it.
Use QUrl::toPercentEncoding static method
In windows you would do:
QString fe = "\"C:\\Program Files\\Autodesk\\Maya2008\\bin\\imconvert.exe\"";

Resources