Posted on 25-10-2013
Filed Under (Technology) by bigbang

QtService is a library not maintained by Qt right now, but a lot of engineers find that it is useful when you want to write a Windows application with Windows Service.

First, git clone the code from

git://gitorious.org/qt-solutions/qt-solutions.git

After checkout, there is a folder qtservice which is the target in this post. Enter the folder and we will compile the library for later usage.

Second, compile the code into a library under Windows. Use Qt Command Prompt, jump to the source folder and use following commands to compile it:

configure.bat

qmake

nmake.exe

The nmake.exe is the Visual Studio compiler, you can find it under Visual Studio installation path.

If the command prompt provides you with an error code:

NMAKE : fatal error U1077: 'cl' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin
\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.

The reason is that you don’t use Qt Command Prompt.

After Compiled the code successfully, you can copy qtservice.h and generated library files such as QtSolutions_Service-head.dll, QtSolutions_Service-head.lib to your project folder. You link them in your pro file, then you can write QtService code. Just read examples under doc folder, and you will find the way.

If you use QtService in your application which name is example.exe for example, you can use following command to check the usage:

example.exe -help

and you can use following command to run the application as normal command line tool:

example.exe -exec
(0) Comments    Read More   
Posted on 02-10-2013
Filed Under (Technology) by bigbang

I tried to write a simple http server using Qt as a desktop tool. But when I tried to handle Chinese characters in UTF-8 encoding, my Firefox will always get mess characters.

The reason must be the difference between QString encoding and http encoding. I have encoded the http content using UTF-8, and I also should do something for QString.

Finally, I find the steps to let this simple http server handle Chinese characters.

First, in main function of your program, using following code to set global QString handler:

QTextCodec *codec = QTextCodec::codecForLocale();
QTextCodec::setCodecForTr(codec);

Second, when you get http request, process the request and generate the feedback of the http request, you can send the content by following code using UTF-8:

//generate html content according to http request
//which is result
...
QString content = tr("HTTP/1.0 200 Ok\r\n"
                                "Content-Type: text/html; charset=\"utf-8\"\r\n"
                                "\r\n");
content = content + result;

QTcpSocket* socket = (QTcpSocket*)sender();
QTextStream os(socket);
os.setCodec("UTF-8");
os.setAutoDetectUnicode(true);
os << content;

Then you will get the right output in you web browser.

(0) Comments    Read More