Summary of QT knowledge points (3)

1. When compiling a shared library, it must be marked as exported. In order to use shared libraries on the client side, some platforms may require a special import statement.

Therefore, Qt provides two special macros:

  • Q_DECL_EXPORT: When compiling the shared library, it must be added to the symbol declaration used.
  • Q_DECL_IMPORT: When compiling a client (using the shared library), it must be added to the symbol declaration used.

2. QLibrary loads the shared library at runtime. It should be noted that the function name to be parsed must be exported as a C function. This means that if the library is a C ++ compiler, then the function must be wrapped in a extern "C"block. You must also be used Q_DECL_EXPORTand Q_DECL_IMPORTexplicitly derived from the function library. If you can not access dynamic libraries .hwhen header files, this method is useful.

3. If you use static link library, pro file should always use both LIBSand PRE_TARGETDEPS.

4. QMutex: A thread locks a mutex to gain access to shared resources.

5. QReadWriteLock: It distinguishes "read" and "write" access, allowing simultaneous reading, thereby improving parallelism

6, QSemaphore semaphore, condition variable QWaitCondition is better

7. QMutexLocker, QReadLocker and QWriteLocker are convenience classes, when they are constructed, they will lock the resource; when they are destroyed, they will be automatically unlocked

8. Thread safety: it can be called by multiple threads at the same time

9. Reentrancy: It can be called by multiple threads at the same time, but each caller can only use its own data.

Therefore, a thread-safe function is always reentrant, but a reentrant function is not necessarily thread-safe

10. The fifth parameter of connect:

  • Auto Connection (default): If the signal is emitted in the thread to which the receiver is attached, it is equivalent to Direct Connection. Otherwise, it is equivalent to Queued Connection.
  • Direct Connection: When the signal is transmitted, the slot function is called immediately. The slot function is executed in the thread of the signal transmitter, but not necessarily in the thread of the receiver.
  • Queued Connection: When control returns to the event loop of the recipient's thread, the slot function is called. The slot function is executed in the receiver's thread.
  • Blocking Queued Connection: The calling situation of the slot function is the same as that of the Queued Connection, except that the current thread will be blocked until the slot function returns.
  • Note: Using this type of connection in the same thread will cause a deadlock.
  • Unique Connection: The behavior is the same as Auto Connection, but the connection will only be established when "will not be the same as an existing connection", that is: if the same signal has been connected to the same slot function, then the connection will not be reconnected Is established, and connect() will return false.
     

11. Never use the QThread object (this) as the father of an object created in the thread

12. Although QObject is reentrant, GUI classes, especially QWidget and all its subclasses are not reentrant, they can only be used in the main thread

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/103372580