I can see it on the Internet and discuss it with you. In this article, the advantages and disadvantages of QT and MFC are not listed one by one.
The fact that I used QT before using MFC may affect my objectivity.
(MFC is highly efficient, but a large number of windows APIs and messaging mechanisms make it hard to understand and easy to use; QT encapsulation is good, easy to use and cross-platform, but inefficient)
From a pragmatic point of view: My boss gave me a software planning instruction and asked me to develop it. Some of them are developed using QT, while others are developed using MFC.
MFC (Microsoft basic class library) is a Class Library specially designed for Windows for developing graphical user interfaces. MFC uses more or less object-oriented methods to encapsulate Win32 APIs. For this reason, these APIs are sometimes a mixture of C ++, C, and C ++.
The qt c ++ graphics library was developed by trolltech around 1994. It can run on Windows, Mac OS X, UNIX, and embedded systems like sharp zaurus. QT is fully object-oriented.
Document/view Model
The document/view mode and template are required for MFC programming. If you do not use them, programming becomes very difficult. In addition, the template has a fixed structure. If the required structure is an undefined structure of the template, programming is difficult. For example, divide a region to display two views in two documents ). Another common problem is that a template creates a view but cannot access it. The document has to do everything, however, this problem often occurs.
Qt does not force any design patterns. If you think it is appropriate, there is no problem using document/view. If you do not use it, there is no problem.
Pseudo object VS real object
In the final analysis, the difference between QT and MFC lies in the Design difference.
The fundamental purpose of MFC is to access packaged windows APIs written in C language. This is not a good object-oriented design model. In many places, you must provide a struct with 15 members in C language, but only one of them is related to what you expect, or you must use old-style parameters to call your function.
There are still a lot of confusing parts of MFC, and function names do not have any continuity. For example, if you create a graphical class, it will not be created until creat () is called. However, for dialogs, you must wait until oninitdialog () to create this object. The strange thing is that the function name used to create the class is oninitupdate (),...... you create a class and call it in their way. Your program crashes.
For example, there is a didit that contains the cedit control. If domodal () is not called, you cannot use getwindowtext (). Otherwise, it will be inexplicably failed. In short, MFC is full of things that the second monk cannot understand, and it is difficult to debug such errors.
On the contrary, QT has a well-designed object-oriented architecture. Therefore, QT maintains excellent consistency in terms of naming, inheritance, and class organization. You only need to provide the parameter of the only method. In different classes, calling methods also have strong consistency. The return value is also logical. Everything has achieved a simple and powerful Harmonious Unity. Once you use one of the classes, other classes are accessible because they are consistent.
In QT, you can use the Edit Control and C ++ to create a class to create your own qlineedit. You can always access any method immediately, whether it is displayed or hidden. There is no confusion here, and everything works in what you think is simple.
Message Loop
MFC is an event-driven architecture. To perform any operation, you must respond to a specific message. Windows sends
There are thousands of messages. Unfortunately, it is very difficult to clearly identify these complex messages, and the documentation on this issue cannot solve these problems well.
The message mechanism of QT is based on signal () transmission and slot () acceptance. This mechanism is the core mechanism for establishing connections between objects. Use signal () to pass any parameters. Its functions are very powerful. You can directly send a large signal to slot (), so you can clearly understand what is going to happen. The number of signals sent by a class is usually very small (4 or 5), and the documentation is also very complete. This makes you feel that everything is under control. The signal/slot mechanism is similar to the listener mechanism in Java. However, this mechanism is more lightweight and has more functions.
Create Interface
MFC cannot create a child window with a dynamically changeable size. You must manually modify the code to change the position of the window (this just explains why the dialog in Windows cannot be changed) this problem is even more serious when software is being translated internationally, because many countries require longer words and sentences to express the same meaning, and the software must be modified for each language version.
In QT, anything can be manually typed out, because it is very simple: to get a button, you can
Button = new Pushbutton ("buttonname", myparentname );
If you want to call the execution of a broken code after pressing a button, you can write it as follows:
Connect (button, signal (clicked (), qapp, slot (action ()));
Qt has a very simple and powerful layout mechanism, so that it is a waste of time if you don't use it.
Qt also provides a graphical user tool, QT designer, to help you build a user interface. You can modify the properties of any controls used. You don't have to place them strictly. You can use layout to perfectly organize them. The Code produced by this tool can be actually read and understood. The generated code is stored in a single file. During programming, You can recreate the user interface multiple times as you wish.
Qt designer allows you to complete many tasks that are not possible in MFC. For example, you can use a pre-filled listview to generate a listview and use different views for tab control on each tab.
Help documentation
When you select a graphic development environment, whether or not the help documentation is comprehensive is an important factor in your selection. The Visual Development Environment's help document msdn (which needs to be paid separately) is very huge, with 10 cdrom discs. He is all-encompassing and extensive. However, it is inevitable that the theme is blurred and the key information is not prominent. The link design is also poor. It is difficult to jump from a class to its parent class or subclass and related classes through the link. If you search for a keyword, whether it is visual C ++, Visual J ++, or Visual Basic, all the information containing these keywords will be returned.
Qt documentation is well designed. You can access doc.tolltech.com.
Qt documentation is complete and detailed covering all aspects of QT, and only 18 m. Every class and method is described in detail, and the examples are full. With links provided by trolltech or QT Assistant tools, you can easily jump from one class or method to another class. The document also contains a beginner's tutorial and examples of typical applications. You can also provide FAQs and email lists for your convenience through the Internet or user groups. If you purchase authorization, you will receive technical support from trolltech within one day.
In fact, excellent help documents of QT greatly reduce the chance of seeking external help. One of tolltech's purposes is to have such excellent QT products and their help documentation, and technical support is redundant.
Unicode
To use MFC, if Unicode is to be displayed, special parameters (and the entry to change Executable File Execution) must be used during link compilation. T must be added before each string, modify Char to tchar. Each string processing function (strcpy (), strdup (), strcat ()......) to be changed to another function name. Even more annoying is that Unicode-supported software cannot work with DLL that does not support Unicode. This is a serious problem when using external DLL for development, but you have no choice.
Using QT, the string is processed using qstring, which is inherent in Unicode and does not need to be changed. Do not add parameters during compilation/link, do not modify the code, just use qstring.
The qsting class is powerful and can be widely used without worrying about Unicode issues. This makes conversion to Unicode very convenient. Qsting provides functions for converting to char * And utf8.
Obviously, the cstring Design of MFC is significantly different from the qstring Design of QT. Cstring is based on char * and provides a few features. It has the advantage that the cstring type can be used directly when the char * type is required. At first glance, this seems to be an advantage. In fact, there are still major defects, especially the possibility of directly modifying char * instead of updating the class. It also encountered a lot of trouble when converting to Unicode.
On the contrary, qstring stores strings in Unicode internally and provides the char * function when needed. In fact, char * is rarely used, because the entire qt api responds to the qstring parameter in text mode. Qstring also comes with many other features, such as automatically sharing the content of qstring. This is a very powerful class and you will like to use it in many places.
Internationalization
Using MFC can be internationalized, but every string needs to be placed in a string table and loadstring (identifiet) is used everywhere in the code ). Then convert these resources to the DLL, translate the strings to the required language, change the graphical interface, and then call the program to use the DLL. The whole process is so cumbersome that it can be described as a trigger. All things should be considered.
When using QT, you only need to place the string in the tr () function. In program development, this is a breeze. You can directly change the string reference in the code. Qt linguist, a tool of QT, can extract all strings to be translated and display them on a friendly interface. This user interface is very suitable for translation. It uses dictionaries to display string content, properly display Unicode, fast mode conflict detection, detects untranslated strings, and detects string modifications. It has complete functions. This software can be used by translators without any programming experience. At the same time, the software is released under the copyright of GPL and can be modified as needed.
The translated documents are stored in XML, which is applicable to the principle of software reuse. Adding a new language version for the software is simply using QT linguist to generate a new file.
Resources Problems
When using MFC, some development processes depend on "Resources". In many cases, developers must use them. This will cause the following consequences:
With Visual Studio, it is difficult for you to use other tools for development.
The resource editor has only limited functions. For example, it is impossible to change all attributes through the dialog editor. Some attributes can be changed, while others cannot. (Note: There are two examples that describe the shortcomings of MFC below, but I feel that these are enough to explain the problem. I will not translate them for the moment)
However, QT does not have the concept of resources, which solves the problems mentioned above. Qt provides a script to compile your code. For interface design, QT designer creates readable code.
Price
Once you purchase Visual Studio, you will get the mfc sdk for free.
QT is a version that complies with the GPL copyright for free on UNIX ). To develop software that does not disclose source code, you must purchase the QT authorization. On a specific platform, each developer buys a permanent authorization and receives one year of technical support. (Translator's note: You can delete the following questions about the purchase price because the price is not fixed. If you have any questions, please go to the official website to check the price)
Release
When releasing an MFC-based software, you must rely on the MFC on your computer. However, this is not safe. It is also mfc42.dll. You can get three different versions based on the same library. Generally, you need to check whether the mfc42.dll version is correct. If not, upgrade it. However, upgrading mfc42.dll will change the behavior of many software. This makes me feel uncomfortable. What should I do if a user crashes after installing my software?
Qt does not have this risk, because QT does not have the concept of "Upgrading the entire system.
It seems that MFC has many shortcomings compared with QT, but it has a huge user base. It is still difficult for Qt to shake the status of MFC in a short time.