Qt (1) Qt console application to determine the path control
Published on| May 20th, 2009 | No Comment.
Categories:Qt |
Description: Qt is set in the environment, until a recent article, I said.
Finally, programming. Ever, "Hello World" based on the color of the sample sources have been tried. Qt is the first place, so a GUI library, on the same track, all, GUI executable was created. I think this is standard.
Qt has been developed, you may want to check and how to use the library easily. Then, GUI, not in the console, when you suddenly get the urge to check.
So, do what you can create a Qt console application?
This time, Qt creates a console application, Qt path and how to manage files in Windows-specific file name (long pass, short pass) for control, and touch.
Articles: qmake with VC + + 2008 Express to create a project file
Related Sites: http://doc.trolltech.com/4.5/qmake-variable-reference.html # config
Sample source code:
Immediately, try to create a console application
Sample.cpp to compile the above, I would like to create a Console application.
1 2 3 4 5 6 7 8 9 | #include <iostream> using namespace std; int main(int /* argc */, char* /*argv[]*/ ) { std::cout << "Test"<< std::endl; return 0; } |
MAKEFILE only one option when you create a.
1 2 3 4 5 6 7 | C:\temp> qmake -project C:\temp> qmake temp.pro C:\temp> nmake : : -- compile... : C:\temp> |
If you create a Console application, the second line of the file MAKE "qmake" to give you the option.
1 | C:\temp> qmake temp.pro "CONFIG+=console" |
"CONFIG + = console" I'll just add.
Then, like "nmake" and run from the command line, please start the executable.
Sure, "Test" would be displayed.
Also, VC + + as well if you create a project file, "CONFIG + = console" I'll just add.
1 | C:\temp> qmake -tp vc temp.pro "CONFIG+=console" |
If you specify this way, you can create a console application.
The other console in this way, "dll" (DLL), "static" (library) and can be created.
For more information, http://doc.trolltech.com/4.5/qmake-variable-reference.html # config please see the. CONFIG is the option to set the parameters.
You are ready.
Determine the path to Qt Management
Continues, Qt paths in management, what is?
In Qt, QDir, QFileInfo class, and most of the control path.
Let's examine the source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include "QFileInfo" #include "QTextStream" int main(int argc , char* argv[] ) { QTextStream out(stdout); int ni; // print of input parameters. for ( ni = 0; ni < argc; ni++ ){ // app.argc() == argc out << "argv[" << ni << "]:" << argv[ni] << endl; } if(argc>1){ // secound parameter is check path. QFileInfo fInfo(QString::fromLocal8Bit(argv[1])); out << "finfo absoluteFilePath() = " << fInfo.absoluteFilePath() << endl; out << "finfo absolutePath() = " << fInfo.absolutePath() << endl; out << "finfo baseName() = " << fInfo.baseName() << endl; out << "finfo canonicalFilePath() = " << fInfo.canonicalFilePath() << endl; out << "finfo canonicalPath() = " << fInfo.canonicalPath() << endl; out << "finfo completeBaseName() = " << fInfo.completeBaseName() << endl; out << "finfo completeSuffix() = " << fInfo.completeSuffix() << endl; } } |
IO streams, STL iostream from Qt for the QTextStream Please note the change.
QFileInfo each method is as follows.
For more information, please see the class documentation.
- absoluteFilePath: full path name (including file name) to output
- absolutePath: full path name (not including the file name) to output
- baseName: Name the output file name without extension
- canonicalFilePath: full path name (including file name) to output (output and not the actual file does not exist)
- canonicalPath: full path name (not including the file name) to output (output and not the actual file does not exist)
- completeBaseName: Outputs the name of the file name without extension
- BaseName the difference between "." Behavior is different when there are multiple file names. - baseName, the first "." obtained from the left, completeBaseName was the last "." to get the left.
- completeSuffix: Outputs the name of the file name extension
- Document Class:
- QFileInfo: http://doc.trolltech.com/4.5/qfileinfo.html
- QTextStream: http://doc.trolltech.com/4.5/qtextstream.html
1 2 3 4 5 6 7 8 9 10 | c:\temp> temp.exe "c:\temp\temp.txt" argv[0]:temp.exe argv[1]:c:\temp\temp.txt finfo absoluteFilePath() = C:/temp/temp.txt finfo absolutePath() = C:/temp finfo baseName() = temp finfo canonicalFilePath() = C:/temp/temp.txt finfo canonicalPath() = C:/temp finfo completeBaseName() = temp finfo completeSuffix() = txt |
Thus, Windows and different, "/" is the path of management. The drive is controlled in all capital letters.
In short file (path), how will be managed?
I'll look into it. Results are as follows.
1 2 3 4 5 6 7 8 9 10 | c:\temp> temp.exe "C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe" argv[0]:temp.exe argv[1]:C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe finfo absoluteFilePath() = C:/PROGRA~1/MICROS~1.0/VC/BIN/cl.exe finfo absolutePath() = C:/PROGRA~1/MICROS~1.0/VC/BIN finfo baseName() = cl finfo canonicalFilePath() = C:/PROGRA~1/MICROS~1.0/VC/BIN/cl.exe finfo canonicalPath() = C:/PROGRA~1/MICROS~1.0/VC/BIN finfo completeBaseName() = cl finfo completeSuffix() = exe |
Short file (path), are managed with this exact name. , Long file (path)?
Well, we'll look into it. Results are as follows.
1 2 3 4 5 6 7 8 9 10 | c:\temp> temp.exe "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe" argv[0]:temp.exe argv[1]:C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe finfo absoluteFilePath() = C:/Program Files/Microsoft Visual Studio 9.0/VC/bin/cl.exe finfo absolutePath() = C:/Program Files/Microsoft Visual Studio 9.0/VC/bin finfo baseName() = cl finfo canonicalFilePath() = C:/Program Files/Microsoft Visual Studio 9.0/VC/bin/cl.exe finfo canonicalPath() = C:/Program Files/Microsoft Visual Studio 9.0/VC/bin finfo completeBaseName() = cl finfo completeSuffix() = exe |
Long file (path), are managed with this exact name. In short file (path) with long file name (path) of each file name, you will be recognized as the same?
Let's check the source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include "QFileInfo" #include "QTextStream" int main(int argc , char* argv[] ) { QTextStream out(stdout); int ni; // print of input parameters. for ( ni = 0; ni < argc; ni++ ){ // app.argc() == argc out << "argv[" << ni << "]:" << argv[ni] << endl; } if(argc>2){ // secound parameter is check path. QFileInfo fInfo1(QString::fromLocal8Bit(argv[1])); QFileInfo fInfo2(QString::fromLocal8Bit(argv[2])); out << "finfo check = " << (fInfo1 == fInfo2) << endl; } } |
1 2 3 4 5 | c:\temp> temp.exe "C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe" "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe"
argv[0]:temp.exe
argv[1]:C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe
argv[2]:C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe
finfo check = 0 |
Does not match. QDir like let's check.
The sample source of the QDir QFileInfo try to implement the change.
The result is the same.
Qt Thus, short passes, long passes will be treated as different.
If this application if it is necessary to check the path, not only for its Apps.
For example, the following applications, if only to manage the long pass at all times, you can avoid this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include "QFileInfo" #include "QTextStream" #ifdef Q_OS_WIN #include <windows.h> QString getLongPathName(const QString& spath) { ushort uspath[MAX_PATH]; if (spath.isEmpty()){ return QString(); } QString spath_win32 = QDir::toNativeSeparators(spath); if(GetLongPathName(spath_win32.utf16(), uspath, MAX_PATH)==0){ return spath; } else { return QDir::fromNativeSeparators(QString::fromUtf16 (uspath)); } } #else QString getLongPathName(const QString& spath) { return spath; } #endif int main(int argc , char* argv[] ) { QTextStream out(stdout); int ni; // print of input parameters. for ( ni = 0; ni < argc; ni++ ){ // app.argc() == argc out << "argv[" << ni << "]:" << argv[ni] << endl; } if(argc>2){ // secound parameter is check path. QFileInfo fInfo1(getLongPathName(QString::fromLocal8Bit(argv[1]))); QFileInfo fInfo2(getLongPathName(QString::fromLocal8Bit(argv[2]))); out << "finfo check = " << (fInfo1 == fInfo2) << endl; } } |
Let us verify the program. Results are as follows.
1 2 3 4 5 | c:\temp> temp.exe "C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe" "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe"
argv[0]:temp.exe
argv[1]:C:\PROGRA~1\MICROS~1.0\VC\BIN\cl.exe
argv[2]:C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe
finfo check = 1 |
Qt in this way, Windows-specific problems are also more than a little.
(Many are not so much.
At any rate, Qt is a cross-platform.
)
This time, I wrote about an example of directory and file name. In addition, Qt libraries for a bit, just touched the touch. Qt library, so you can have a taste you can use.
Once, I think a good test.
This time, I wrote about an example of directory and file name. In addition, Qt libraries for a bit, just touched the touch. Qt library, so you can have a taste you can use.
Once, I think a good test.
You might also like:
Trackback URL
After Admin approves this comment, it will be shown.
Comments
Leave a Reply
