Qt Meta-Object System (Meta-Object) (iii), why Qt signals and slots for the Moc

Why would Moc for Qt signals and slots?

  Templates are built in C ++ mechanism that allows the compiler to generate code dynamically, depending on the type of parameters passed. Therefore, for people to create a framework of templates is very interesting, we use advanced templates in many parts of Qt. However, there are some limitations: You can use templates to easily express certain content, but some content can not be expressed with a template. A generic container class vector represented easily, even if the pointer type partial specialization required, and a description of XML-based graphical user interface provided function given a string can not be used to represent the template. And in a gray zone between them. You can use templates to modify some things, of course, at the expense of code size, readability, portability, availability, scalability, robustness, and the final design aesthetic. Templates and C preprocessor can be extended to achieve incredible intelligence and incredible things. But just because these things can be done, it does not necessarily mean they are the right design choices. Unfortunately, the code is not to be published in a book, but the use of a real compiler to compile on the operating system of the real world.
Here are some reasons to use Qt's moc:

The importance of grammar

  Grammar is not just candy: the syntax used to express our algorithms can significantly affect the readability and maintainability of the code. The syntax for Qt signals and slots proved very successful in practice. Syntax intuitive, easy to use and easy to read. Qt study found that people who help them to understand the syntax and use signals and slots - even though it has a high level of abstraction and generality. This helps the programmer from the outset know how to design, even without considering design patterns.

Good use code generator

  Qt's moc (moc) provides a simple method of compiling transcend language capabilities. It may be achieved by generating a standard compiled by the C ++ compiler any additional C ++ code. moc reads C ++ source file. If it finds one or more macros Q_OBJECT comprising a class declaration, it will generate another C ++ source file that contains the class meta object code. Generated by moc C ++ source files must be compiled and linked with the realization of the class (or it may be #included class source file). Usually, moc than manually calling, but called automatically by the build system, so programmers do not need extra work.
  moc is not the only use of the code generator Qt. Another prominent example is the UIC (UI compiler). It accepts XML user interface description information stored in, and create a c ++ code to set the interfaces. In addition Qt, code generators are also common. To rpc and idl example, it makes the program or object can communicate through the process or machine boundaries. Or a variety of scanner and parser generators, wherein lex and yacc are best-known. They syntax specification as input and generating code for the state machine. Alternatively code generator is hacked compilers, proprietary programming language or a graphical tool, which comprises a one-way dialog or wizard, wizard dialog box or the design time rather than compiled code is blurred. Our customers will not be locked in a proprietary C ++ compiler or specific integrated development environment, but to enable them to use whatever tools they prefer. We encourage them to add to their build system in our tools, rather than forcing programmers to code to the generated source code repository: clearer, more secure, more in line with the spirit of UNIX.

GUI is dynamic

  C ++ is a standardized, powerful and well-designed universal language. It is the only language used in such a wide range of software projects, covering a variety of applications from the entire operating system, database servers, high-end graphics applications to a normal desktop application. One key to the success of the C ++ language is its scalable design, focusing on maximum performance and minimum memory consumption, while still maintaining compatibility with ANSI C.
  Despite these advantages, there are some drawbacks. For C ++, when referring to component-based programming graphical user interface, the static object model clearly not conducive to the dynamic messaging method for Objective C. High-end database server or operating system is not necessarily a good thing right design choices GUI front end. Use moc, we have this disadvantage into advantage, and increased flexibility to respond to a safe and efficient graphical user interface programming challenge required.
  Our approach goes far beyond anything that you can do using the template. For example, we can have object properties. We can also overload signals and slots, our signal will be added to the zero byte size class instance, which means that we can add new signals without breaking binary compatibility.
  Another benefit is that we can explore objects at run time signals and slots. We can use the type-safe call by name to establish a connection, without the need to know the exact type of the object being connected. Template-based solutions are not possible. This runtime introspection opens up new possibilities, such as generation and GUI connection from Qt Designer's XML UI files.

Call performance is not everything

  Qt signals and slots implementation is not based solution templates so fast. Although the use of a common template to send a signal to achieve cost about four ordinary function call overhead, but the amount of work required is equivalent to 10 Qt function calls. This is not surprising, because Qt mechanisms include queuing calls between a common grouper, introspection, different threads, as well as the final script function. It does not depend too much inline expansion, and code, and provides unparalleled safety runtime. Qt iterators are safe, and those faster system based on the template is not. Even in the process of transmitting signals to a plurality of receivers, the receivers can also be removed safely without causing a crash. Without this security, your application will eventually collapse, memory read or write error appears difficult to debug.
  However, template-based solutions can not improve the performance of applications using signals and slots do? Although Qt really adds to the cost of the signal by calling a small amount of overhead tank, but the cost of the entire call only a fraction of the cost of the slot. Qt signals and slots of systems commonly used benchmark recess. As long as perform some useful operations (for example, some simple string manipulation) in the slot, you can call overhead is negligible. Qt system is optimized, any operations that require operator new or delete (e.g., string manipulation or insert / delete some contents from the container in the template) is much more expensive than the signals.
  NARRATOR: If you close the performance of mission-critical inner loop has a signal and groove connection, and you use this connection as a bottleneck, consider using the standard listener-interface mode, rather than signals and slots. In this case, you may only need 1: 1 connection. For example, if you have downloaded from a network data object, then use a signal to indicate the requested data has arrived, this is a very clever design. However, if you need to send each byte individually to the consumer, use the listener interface, rather than signals and slots.

no limit

  Because we have moc signals and slots support, so we can add other useful things to it, which is a template can not. These include conversion function generated by tr (), and advanced with the property system and the type information of the extended runtime introspection. Property system itself is a huge advantage: If you do not have a powerful and introspective property system, like Qt Designer so powerful and universal user interface design tool will be very difficult to write (if not impossible). But it does not end there. We also offer a dynamic qobject_cast () mechanism that does not rely on RTTI system, so it is not limited. We use it to safely query interface components dynamically loaded. Another application fields are dynamic element object. For example, we can use ActiveX components, and around it at runtime to create a meta-object. Or we can be exported as an ActiveX component assembly Qt by Qt's meta-object export. You can not use the template to do two things.
  Moc use the c ++ nature provides flexibility Objective-C or Java run-time environment for us, while maintaining the c ++ unique performance and scalability advantages. It makes it flexible and comfortable Qt become the tools we have today.

Guess you like

Origin blog.csdn.net/a844651990/article/details/93022953