Qt Mkdir with a environment variable - qt

QDir *temp = new QDir("%USERPROFILE%");
bool ok = temp->mkdir("abc");
and it does not work.

For portable Qt code, use static method QProcessEnvironment::systemEnvironment():
QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
QDir *temp = new QDir(env.value("USERPROFILE")); // returns empty string for unset variable
bool ok = temp->mkdir("abc");
Alternative, as suggested in a comment of the actual question, you can also use
#include <cstdio>
QString envValue(QString::fromLocal8bit(::getenv("USERPROFILE"))); // standard, yet deprecated in Windows...
I'd prefer using the first alternative, letting Qt take care of character encoding stuff. If you want to use non-Qt functions, I think in Windows you should use getenv_s or _wgetenv_s to do this "properly".

Related

Problems in converting to UTF-8 in Qt

I try to show a persian string in Qt:
QMessageBox msg;
QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();
but it shows the following error :
/home/msi/Desktop/VoMail
Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40:
error: no matching function for call to 'QString::fromUtf8(QString&)'
I want to use a string variable, and not a string directly.
How can I convert a QString variable to Utf8?
As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:
QMessageBox msg;
QString str = QString::fromUtf8("یا حسین");
msg.setText(str);
msg.exec();
its not good idea write like that
using this must be better
QString str(tr("ya hossein");
and use linguist and add persian translation file to your project http://qt-project.org/doc/qt-4.8/linguist-translators.html
and if you dont want use this, you must be sure your IDE or code editor (like qtcreator) use utf8 for saving files and just use
QString str("یا حسین");
it must be ok, i tested that so many times

C++/CLI How to translate this code ?

I dont know a lot about C++, but I have to make work some C++ code with .NET. I try with DLLImport but I failed. So I try with C++/CLI to make kind of a wrapper.
But I'm not sure to understand everything...
This is the basic C++ H file with the function I want to export (MyFunction)
extern "C"
{
__declspec(dllexport) IplImage* MyFunction(IplImage *src, std::string* name, OneEnumerationType myEnum, bool myBool, float myFloat);
}
This is the Wrapper h code.
#include "MyFunction.h"; // the file containing the h code
#include <string>
namespace MyWrapper{
public ref class MyWrapperClass {
public:
MyWrapper(){};
IplImage^ GetMyFunction(IplImage *src, std::string^ name, OneEnumerationType myEnum, bool myBool, float myFloat);
}
This is the Wrapper cpp code.
#include "MyWrapperCode.h";
namespace MyWrapper{
IplImage^ MyWrapperClass::GetMyFunction(IplImage* src, std:string^ name, OneEnumerationType myEnum, bool myBool, float myFloat){
MyFunction(src, name, myEnum, myBool, myFloat);
}
}
These are my questions :
1) When I'm compiling, the error is "'^ : cannot use this indirection on type IplImage' and same message for type "std::string".
I have followed this logical :
ClasseNative clNat2 = *clNat; --> ClasseManagee clMan2 = *clMan;
ClasseNative &clNat3 = clNat2; --> ClasseManagee %clMan3 = clMan2;
ClasseNative *clNat4 = &clNat2; --> ClasseManagee ^clMan4 = %clMan2;
I have seen, that It was better to use System::String. I try this way but the initial function is using std::string... BTW, why is it better to change ?
2) How do I get the MyFunction IplImage result ? Thru a private member and a get I suppose but I dont know how to initialize it...
3) Tricky question. Is it possible for me to put the CLI obtains IplImage structure (from the OpenCV library) (the result of my function) inside a IplImage .NET structure, when I ll called my wrapper ? Dont know if the question is understandable...
Thanks a lot for your help.
Turning around for 3 days on this problem...
Your wrapper class needs to create a new std::string based on the content of a System::String^ parameter then pass to your native function. Otherwise you need to rewrite the function to take something else as the string input, for example a LPWSTR or LPCSTR.
You can write a ref class to have properties for all data that an IplImage would have, then pass that to your wrapper class. Your wrapper class then create an IplImage object based on the data of the ref class and pass to the native function. Reverse the data copying direction for the return value.
1) just by adding ^ you cannot change a native object to become managed, you have to create wrappers or transfer the data for example:
std::string nativeString = "my string";
String^ managedString = gcnew String(nativeString.c_str());
//now you can return it as
2) create a managed wrapper or use primitive datatype to transfer the data
3) note sure if this will help but look at Emgu.CV
try reading abit more about C++\CLI here are a few nice tutorials:
Quick C++/CLI - Learn C++/CLI in less than 10 minutes
C++/CLI for the C# programmer

force call to QString(const char *) constructor in Qt 4.7

I am trying to compile a library written in Qt 4.6. On my current Linux machine I have only Qt 4.7 installed. The following code part:
/*file try.h*/
void fileOpen(QString s = NULL) ;
/*file try.cpp*/
void MainWindow::fileOpen(QString s) {
QString filename ;
if(s.isNull()) filename = QFileDialog::getOpenFileName(
this,
"Choose a file",
".",
"Source file (*.)");
else filename = s ;
}
compiles with the following error (I used cmake but the corresponding line code is the one listed above):
In member function ‘virtual int MainWindow::qt_metacall(QMetaObject::Call, int,
void**)’:
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
error: conversion from ‘long int’ to ‘QString’ is ambiguous
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
note: candidates are:
/usr/include/QtCore/qstring.h:426:43: note: QString::QString(const char*)
/usr/include/QtCore/qstring.h:105:14: note: QString::QString(const QChar*)
So I am guessing the problem is that in qt. 4.7. there are two QString constructors that can take a pointer as an argument (as listed in the compilation error), whereas in qt 4.6. there is only one QString constructor that can take a pointer as an argument. How can I force QString to call the constructor with const char * as an argument?
Thank a lot for your help in advance,
madalina
void fileOpen(QString s = NULL);
You are trying to construct a QString object with 0. It seems you are confusing the null of pointers with a null QString. A null QString is one which is created with the constructor QString(). Given how your function is implemented (referring to s.isNull()), you should change the function declaration to
void fileOpen(QString s = QString());

Force compiler not to use .rodata section

Is there any way to force gcc to put
char* str = "Hello";
not in the .rodata without change this statement in
char str[] = "Hello!";
?
Ok, so better way to do this is modify the statement to char str[]. Thanks to all.
Why? Trying to change string literals leads to undefined behavior. It's evil. Consider this program:
"hello"[0] = 'y'; // Welcome to Undefined Behavior Land. Enjoy yor stay!
std::cout << "hello" << std::endl; // Could very will print "yello" now!
static char strbuf[] = "Hello";
char *str = strbuf;
How about using strdup if your platform has it, or implementing it yourself if it doesn't?
char *str = strdup("hello world");
This will allocate memory (at runtime) and copy the string literal into an appropriately sized chunk of memory which you can quite legitimately write to and modify later.
Don't forget to free() after use though.
You might be able to force GCC to put somethings in specific sections of your choosing using the __attribute__ ((section ("my_section"))) attribute, but you'll still have to modify the original source to do that so you'd be much better off doing it a "normal" way.

QDir and QDirIterator ignore files with non-ASCII filenames

The following code somehow fails to notice any files with non-ASCII characters in their names (Cyrillic characters, specifically):
for (int path = 1; path < argc; path++) {
QFileInfo fi(argv[path]);
if (fi.isDir()) {
QDir dir(argv[path], "", QDir::LocaleAware, QDir::AllEntries);
qDebug() << dir.entryList();
QDirIterator it(QString(argv[path]), QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
qDebug() << it.fileInfo().absoluteFilePath();
/* Processing; irrelevant in the context of the question */
}
}
}
What exactly am I doing wrong here? How should I handle QDir and QDirIterator to make them aware of Cyrillic filenames?
The system locale is en_US.UTF-8.
Update: On Windows, everything works correctly.
Get cmd line parameters out of QApplication itself.
So
QApplication app(argc, argv);
QStringList args = app.arguments();
for(...)
Qt will handle encoding properly. But that will only fix problems with unicode on cmd line. Not sure if that is your main problem though.
EDIT:
fromLocal8Bit() probably doesn't work because it wasn't local encoding, but utf8. So fromUtf8() would work on linux and osx (but it won't work on windows). On *nuxes it depends on some environment variables (LS_LANG or something). I guess Qt takes everything into account and converts it properly. You can look at the constructor code for QApplication if you want to know exactly what they do.
Which part is failing? Reading the initial directory specified argv[path] or the iterator? If it's the former, you should convert byte strings to QString for file processing using QFile::decodeName. The default char* => QString conversion uses Latin-1, which is not what you want for file names.
Don't use argv[path] just like that when constructing the QStrings. This will treat the string as a latin1 string (which doesn't care about cyrillic characters). Try using
const QString dirName = QString::fromLocal8Bit( argv[path] );
at the top of your loop and then use dirName everywhere instead of argv[path].

Resources