Qt internal process communication mechanism

xiaoxiao2021-03-05  31

Qt As a cross-platform C GUI system, QT can provide powerful functions for constructing a graphical user interface. Since QT has been released by Trolltech, the system has become the main system used by many successful graphical user applications in the world. More importantly, the desktop environmental system KDE of the Linux operating system is also based on QT construction. Currently, QT has provided support for the MS / Windows, UNIX / X11 and embedded platforms, has been widely used.

In the QT system, it has not only constructed a perfect system structure, but also in order to meet the needs of the user's interface application, it has also created many new system mechanisms, where the internal process communication mechanism unique to QT is particularly worth mentioning. . This paper analyzes three mechanisms commonly used between QT-based applications: QCOP protocol, Signal-Slot mechanism and FIFO mechanism. The respective methods of use are given, and their use occasions are pointed out.

1. QCOP protocol QCOP is a communication protocol within QT, which is used in communication between or different processes between different customers in the same address space. Currently, this mechanism is only available in embedded versions of QT.

To achieve this communication mechanism, QT includes QcopChannel classes inherited by QObject classes, which provide static functions such as send (), isregistered (), which can be used in the case of off-object. In order to receive communication data in the Channel, the user needs to construct a subclass of QCOPChannel and provide an overload function of the Receive () function, or associate with the received signal using the connect () function.

It is worth mentioning that in the QT system, only the QCOP protocol mechanism and classes for receiving messages, and how to send messages, no corresponding classes are provided for users.

In the QT-based desktop system QTOPIA (QPE), the corresponding send class is provided: QCOpenvelope. Users can use the Channel to send messages to other processes through this class. This class encapsulates the process of sending QCOP messages through QCOPChannel, and users can easily implement communication processes between processes only need to call relevant functions in this class. On the one hand, the transmission of QCOP messages uses the QCOpenvelope class. On the other hand, the received message is associated with a QcopChannel.

When sending a message, the following protocol mechanism will be utilized:

Qcopenvelope E (ChannelName, MessageName);

For messages that need to carry parameters, you must use the << () "operator to add parameters to envelope.

E << parameter1 << parameter2 << ...;

For messages without parameters, you only need to use:

Qcopenvelope E (ChannelName, MessageName);

In QTOPIA, all the Channels name begins with "QPE /", while MessageName is an identifier of a function.

When receiving a message, it is usually only necessary to use the QPE / Application / {AppNAME} pipe in advance in the application. Of course, the pipeline can also be defined as needed, and it is associated with a SLOT function:

MyChannel = New Qcopchannel ("QPE / FOOBAR", this)

Connect (MyChannel, Signal (Received &), this, Slot (FooBarMessage (Const QCString &, Const QByteaRray &));

The specific communication process will be described below:

Define pipelines in classes that need to receive messages (such as Window1):

Qcopchannel * Dochannel = New Qcopchannel ("QPE / DO", this);

Connect (Dochannel, Signal (Received (Const Qcstring &, Const QByteaRray &),

This, Slot (Domestic (Const Qcstring &, Const QByteaRray &));

At the same time, you need to define the corresponding message processing function DOMESSAGE in this class.

Void Window1 :: Domestic (Const QCstring & MSG, Const QbyteaRray & Args)

{

QDataStream Stream (Args, IO_READOONLY);

IF (MSG == "Message1 (QString)")

{

Qstring text;

Stream >> Text;

Button-> setText (text);

}

Else IF (msg == "message2 ()")

{

CLOSE ();

}

}

The Message1 (QString) and Message2 (QString) are a message defined by the user, and these messages have been correspondingly processed in this function. In this example, when receiving the Message1 message with parameters, the string parameter stream is displayed on the button button; when the Message2 message is received, the action to turn off the Window1 window will be executed, and of course the user can write corresponding according to the needs of it. Process.

On the other hand, the function function of sending messages in class Class2 is required to send messages in function function in function:

Void class2 :: function ()

{Qcopenvelope e ("QPE / DO", "MESSAGE1 (QString));

E << param;}

Here, Message1 messages are sent, and the parameter param that needs to be carried into the pipe.

With this process, the user can easily implement different objects, different processes, and may transmit parameters in the communication process as needed.

2, the signal-slot mechanism in QT, there is a communication between the objects: signal-slot mechanism, this mechanism is the core mechanism of QT, which is the most important of other GUI tools. feature. In most GUI tools, a callback function is typically defined for each behavior that might trigger, this callback function is a pointer to a function. In QT, the signal-slot mechanism replaces this complicated function pointer and can achieve the same function. Signal-tank machine can carry any type, any number of parameters, and is completely safe and will not cause crash of the system.

All classes inherited by the QObject class, or one of its subclasses, can include signal-groove mechanisms. The signal is usually issued when the object changes their status, which is an object that needs to be done when communicating with other objects. It does not know if there is any other object to receive the signal at the other end. In this sense, this mechanism implements a real information package to ensure that the object can be used as a separate software component. The slot can be used to receive signals, which are usually a member function in the class. A slot does not know if there is a signal to contact yourself. Similarly, objects containing a groove function also knows about the communication mechanism, which can also be used as a separate software component.

The user can contact a lot of signals with a separate groove function as needed, and a signal can also be contacted to many different groove functions as needed. Even a signal can also be directly linked to another signal so that the second signal is immediately issued when the first signal is sent.

In this way, the signal-groove is combined with a powerful programming mechanism.

E.g:

Button = new QAction (TR ("Button"), Qiconset (QPixMap ("Button.png"), 0, 0, this)

Connect (Button, Signal (ActiVated ()), THIS, SLOT (SlotButton ()));

A button is defined in the program and use the connect () function to associate this button button is associated with the slotbutton () function. When the user triggers the button, the corresponding groove function is performed. Of course, the signal here is a pre-defined signal in the QAction class. When the user uses the mechanism, the signal can be defined according to the required signal, and the signal is emitted by the EMIT statement when appropriate. In addition, any parameters can be passed between the signal and the corresponding groove function, such as:

Emit Signal (Parameter);

3, the FIFO mechanism is Of course, in addition to the communication mechanism unique to QT, the communication mechanism commonly used in the general operating system can also be used in communication between different processes within the QT system. Some mechanisms such as message queues, shared memory, semaphore, and pipelines, some mechanisms, such as semaphysics, re-packaged in QT; some mechanisms can directly call system calls for the operating system. Here, the famous pipe is a simple and practical communication mechanism, and the user is in the QT internal mechanism.

Without understanding, you can also use this method to implement communication between object processes. The communication process between the QT internal processes in this mechanism will be described below.

First, you need to create a FIFO. This process is similar to the creation file. You can use the mkfifo command in the system to create, so you can open it with the Open function, at the same time, the general file I / O function (Close, Read, Write) can For FIFO.

In QT-based applications, there are many applications that use a client-server mode that can be transmitted between clients and servers using FIFO. For example, there is a server that is responsible for receiving the news from the underlying program. At the same time, it is related to many clients, the server needs to send the received messages to different clients, and each client also has a request to be issued. The server is sent to the underlying program.

Below is a program example of the server side: (Set up the client process for reading / dev / fifoclient1 and / dev / fifoClient1)

FD = Open ("/ dev / fifoServer", o_nonblock | o_rdonly); file = fdopen (fd, "r");

RET = FGETS (BUF, MAX_LINE, FILE);

IF (BUF [0] == '0')

{

Qfile fd_file ("/ dev / fifoClient1");

QString Temp (BUF);

IF (fd_file.open (IO_WRITEONLY | IO_APPEND)) {

QTextStream T (& fd_file);

t << temp;

FD_FILE.CLOSE ();

}

Else IF (buf [0] == '1')

{

Qfile fd_file ("/ dev / fifoClient2);

QString Temp (BUF);

IF (fd_file.open (IO_WRITEONLY | IO_APPEND)) {

QTextStream T (& fd_file);

t << temp;

FD_FILE.CLOSE ();

}

......

In this program, the server receives information from the underlying (here, it is also transmitted by the FIFO pipe), and then sends information to the pipes of different clients according to the content received, such as the first byte, Implement the correct distribution of information.

The client program example is, for example,: (Suppose the server is to open / DEV / FIFO pipeline)

Qfile Out_file ("/ dev / fifo");

IF (OUT_FILE.OPEN (IO_WRITEONLY | IO_APPEND)) {

QTextStream T (& out_file);

T << Text << "/ n";

When any of the client needs to send a message to the server, you can send it to the public pipe of / dev / fifo.

In this way, the communication process between the different processes or applications can also be realized in the GUI, but when the number of clients is more, this method shows certain limitations, and the overall communication process layout has become too complicated. The pipeline is increasing makes the possibility of error. Therefore, the communication process of the above client and server-side terminals in QT is implemented using FIFO, and it is more suitable for minimal client applications.

Reference

Qt official document: http://doc.trolltech.com/3.2/index.html Qt and Qtopia Sourcecodes: Qcopchannel, QCopenvelope, QAction. Advanced Programming in The Unix Environment, W. Richard Stevens.

About the author's succession: Ph.D., university lecturer, currently engaged in research on wireless network congestion control, and the development of embedded GUI. contact details

xxin76@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-34685.html

New Post(0)