Using QT or MFC? (Transfer)

Source: Internet
Author: User
Tags qt designer

I have used QT and MFC to develop software. I 'd like to share with you the differences I have experienced when using them.

I am not a professional writer. This article may seem less organized than professional magazines and websites. However, I want to share my experience with you in my own language. English is better than my native language. Therefore, some words may be odd and incorrect. Please send me a letter and I can correct them.

This article does not want to pretend to be objective and fair. I just want to express my experience. 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.

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 OSX, UNIX, and embedded systems like sharp zaurus. QT is fully object-oriented. Document/view modelmfc programming requires the document/view mode and template. If you do not use the template, 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. (This separate design mode of data and views is also a good one and should not be a reason for denying MFC.) Qt does not force any design mode. 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. (It is true that MFC is used to encapsulate the window API. It is easier to use MFC than winowsapi, but it is true that some functions call time and order. If it is not used for a period of time, it is indeed possible

As a result, the problem occurs)

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 also 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. Any parameter that can be passed using signal. 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. (This mechanism seems simple and clear)

 

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 more serious when software is translated internationally, because many countries require longer words and sentences to express the same meaning, and must re-modify their software versions for each language.

In QT, everything can be manually typed out, because it is very simple: in order to get a Utton, you can use some buttons = new Pushbutton ("buttonname", myparentname ); if you want to call the execution of a broken code after pressing a button, you can write 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. Modifiable

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 placed in a separate file

While 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. (QT is really good and powerful in terms of Interface)

 

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. Each category 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.

(Msdn is easy to use, comprehensive, and relevant background knowledge and examples can be found. There are also a variety of routines available for reference on the Internet. The help document of QT alone is definitely not enough to solve all the problems. On the Internet, I only went to a QT Chinese forum and asked a few questions. Some of them provided solutions, and some did not answer them, you have to try it on your own)

 

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. When using external DLL for development

This is a serious problem, 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, or modify the code. You only need to 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. (The cstring option can be a unicode version.) In contrast, 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 strong and big 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 taken into consideration.

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. (QT does a good job .)

 

Resources Problems

When using MFC, some development processes depend on "Resources". In many cases, developers must use them. This will lead to the following consequences: with Visual Studio, it is difficult for you to use other tools to complete 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. (QT designer has a good design interface)

 

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. Normally, you need to check whether the correct mfc42.dll version is available. If not, upgrade it. However, upgrading mfc42.dll will change the behavior of many software. This makes me feel uncomfortable. What if the user crashes after installing my software?

Qt does not have this risk, because QT does not have the concept of "Upgrading the entire system. (If it is not a version of QT, there will still be problems)

 

Speed

MFC is designed for Windows, while QT is cross-platform. Therefore, the running speed and response time of the program written by MFC are better than those of QT.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.