QT development programs are very efficient, especially with QT creator 2.2.1. In the past few days, using QT to develop programs seems to have found the feeling of using C ++ to develop programs. In the past, using c ++ to write programs was like self-abuse, even basic types such as string and time have to be combined to find a class library. The MFC documentation/view also makes me dizzy. The key is to use awkwardly. It may be that WinForms is used to it, besides, it seems that it is outdated, and development tool support is not bad! (Do not scold me for VC fans.) After learning WTL for a while, I feel that it is only suitable for use by experts and is a waste of feelings! QT is no longer suitable for programmers like me who have been using. net for a long time to quickly switch to c ++ programming. At the very least, your own programming ideas will soon be realized! Instead, A must be B, B must be C, and C must be D... pain! Or maybe I haven't found the application scenarios of MFC, WTL, and VC?
Anyway, QT fits my mind: Using C ++ like C # provides a good infrastructure for me to quickly compile small programs. Of course, many concepts of C # And C ++ are different. Now let's summarize the programming experience over the past few days:
1. QProcess class: it is very different from. net's Process, that is, QProcess cannot obtain the current Process, that is, there is no GetCurrentProcess method. The direct result is that if you want to Kill yourself, you can only use winAPI. I think QT may be used for cross-platform consideration.
# Include <windows. h>
Void killMe (){
DWORD dwProcessID =: GetCurrentProcessId (); // gets the ID of the current process
HANDLE hProcess =: OpenProcess (PROCESS_TERMINATE, FALSE, dwProcessID); // return the object HANDLE based on the process ID
: TerminateProcess (hProcess, 0); // end the process based on the object handle
CloseHandle (hProcess );
}
2. Signal and Slot: equivalent to the event and event handler in C #, using connet to connect signals and slots.
Connect (setAction, SIGNAL (triggered (), this, SLOT (Settings ()));
The Event in the QT document is rewritten in the subclass to customize the behavior. This event is a non-peer event, such as a close event to overwrite the form:
Void MainWindow: closeEvent (QCloseEvent *)
{
IProcess: killMe ();
}
3. Be sure to try the QScriptEngine class: it is so easy to embed JavaScript scripts. The advantage of embedding scripts is, of course, to improve Program Flexibility. Another advantage is to improve development efficiency: C ++ Compilation speed is obviously not as fast as C #, so it is different to embed a script, script compilation is not required!
QScriptEngine * engine = new QScriptEngine;
QString basePath = QCoreApplication: applicationDirPath ();
QFile file (basePath + "\ test. js ");
If (! File. open (QIODevice: ReadOnly | QIODevice: Text ))
{
QMessageBox: information (this, "", "open failed ");
Return;
}
QByteArray bytes = file. readAll ();
QString scripts (bytes );
Engine-> evaluate (scripts );
If (engine-> hasUncaughtException ())
{
QMessageBox: information (this, "error", engine-> uncaughtException (). toString ());
}
File. close ();
You only need to modify test. js to easily change the logic of my program. If the script needs to interact with the main program:
QScriptValue mainWin = engine-> newQObject (this );
Engine-> globalObject (). setProperty ("host", mainWin );
You can directly access the host in test. js.
4. to store some settings in the registry, use the QSettings class directly.
5. QThread class: you cannot Sleep, so you have to use APIs again. The following code is not accurate, but does not block interface thread execution.
# Include <windows. h>
Void sleep (int svalue)
{
QTime dieTime = QTime: currentTime (). addSecs (svalue );
While (QTime: currentTime () <dieTime)
{
: Sleep (100 );
QCoreApplication: processEvents (QEventLoop: AllEvents, 100 );
}
}
I would like to sum up that it may take a long time for QT to be compiled!