Qt inter-process communication

Source: Internet
Author: User
Tags sendmsg

The traditional inter-process communication mode (shared memory...) can still be used in QT ..)
· In the desktop environment, a more convenient and Object-Oriented communication mode is developed based on the traditional inter-process communication mode.
-
KDE environment: dcop
-
GNOME Environment: bonobo
· Mechanism: the Linux IPC communication mechanism of the freedesktop open-source project. Both KDE and gnome environments support
· QT embedded defines a lightweight inter-process communication mechanism qcop.

Qcop:
· Qcop is implemented using the qcopchannel class.
· Qcopchannel inherited from the qobject class
-
Provides the static function send () to send messages and data to be transmitted.
-
The static function isregistered () is used to query whether a channel has been registered.
-
When receiving messages and data from a channel, we need to construct a sub-class of qcopchannel and rewrite the receive () function, or provide a slot and use the connect () function to send the receive () signal connection
· Using qcop can easily combine the QT signal and slot mechanism, making it very convenient to use
· However, for communications between non-QT programs and QT programs, only other IPC methods can be used
QcopExample:
Send messages and data in a process:

Qbytearray data;
Qdatastream out (& Data, qiodevice: writeonly );
Out <celnum;
Qcopchannel: Send ("/system/temperature", "convertceltofah (INT)", data );

Receive messages and data in another process:

Qcopchannel * channel = new qcopchannel ("/system/temperature", this );

Connect (Channel, signal (received (const qstring &, constqbytearray &), this, slot (handlemsg (const qstring &, const qbytearray &)));

The following is an example:

// Copserv. h
Run as a server-qws
# Ifndef conversion_screen_h
# Define conversion_screen_h

# Include <qwidget>

Class copserv: Public qwidget
{

Q_object

Public:

Copserv ();

~ Copserv (){};

Private slots:

Void launchfah ();

Void launchcel ();

PRIVATE:

Void createscreen ();

Void launchapp (const char * path );
};

# Endif // conversion_screen_h

// Copserv. cpp Program subject
# Include <qpushbutton>
# Include <qslider>
# Include <qlabel>
# Include <qdial>
# Include <qlcdnumber>
# Include <qvboxlayout>
# Include <qhboxlayout>
# Include <qgridlayout>
# Include <qsettings>
# Include <qapplication>
# Include <qcoreapplication>
# Include <unistd. h>
# Include <sys/types. h>

# Include "copserv. H"

Copserv: copserv (): qwidget ()
{

Createscreen ();
}

Void copserv: createscreen ()
{

Qpushbutton * celbtn = new qpushbutton ("cel ");

Qpushbutton * fahbtn = new qpushbutton ("Fah ");

Qpushbutton * quitbtn = new qpushbutton ("quit ");

Qgridlayout * mainlayout = new qgridlayout;

Mainlayout-> addwidget (celbtn, 0, 0 );

Mainlayout-> addwidget (fahbtn, 0, 1 );

Mainlayout-> addwidget (quitbtn, 1, 0 );

Setlayout (mainlayout );

Connect (celbtn, signal (clicked (), this, slot (launchcel ()));

Connect (fahbtn, signal (clicked (), this, slot (launchfah ()));

Connect (quitbtn, signal (clicked (), qapp, slot (quit ()));

Setgeometry (0, 50,240,320 );

Setwindowtitle ("server window ");
}

Void copserv: launchapp (const char * path)
// Start the subroutine
{

If (Path = NULL)

{

Qdebug ("launch path is null! /N ");

}


Pid_t pid = fork ();
// Create a sub-process

If (pid = 0)

{

Qdebug ("New Process forked. PID is: % d/N", getpid ());

Int result = execl (path, path, 0 );

If (result <0)

{

Qdebug ("failed to launch application! /N ");

}

_ Exit (-1 );

}
}

Void copserv: launchcel ()
{

Launchapp ("./CEL ");
}
Void copserv: launchfah ()
{

Launchapp ("./Fah ");
}

// Main. cpp
# Include <qapplication>
# Include "copserv. H"

Int main (INT argc, char * argv [])
{

Qapplication app (argc, argv, qapplication: guiserver );

Copserv screen;
//
Screen. setgeometry (0, 0,240,320 );

Screen. Show ();

Return app.exe C ();
}

// Cel. h
// Header file of subroutine 1
# Ifndef cel_h
# Define cel_h

# Include <qwidget>

Class qslider;
Class qhboxlayout;

Class cel: Public qwidget
{

Q_object

Public:

Cel ();

~ Cel (){};

Private slots:

Void handlemsg (const qstring & message, const qbytearray & data );

Void sendmsg (INT celnum );
// Slot

PRIVATE:

Void fahtocel (INT fahnum );

Void createscreen ();

Void createcel ();

Void listenchannel ();

Qslider * slider;

Qhboxlayout * cellayout;
};

// Cel. cpp
# Endif // cel_h
# Include <qpushbutton>
# Include <qslider>
# Include <qlabel>
# Include <qlcdnumber>
# Include <qvboxlayout>
# Include <qhboxlayout>
# Include <qapplication>
# Include <qcopchannel>
# Include <qdatastream>
# Include <qbytearray>

# Include "cel. H"

Cel: cel (): qwidget ()
{

Createscreen ();

Listenchannel ();
}

Void cel: createscreen ()
{

Qpushbutton * quitbtn = new qpushbutton ("quit ");

Createcel ();

Qvboxlayout * mainlayout = new qvboxlayout;

Mainlayout-> addwidget (quitbtn );

Mainlayout-> addlayout (cellayout );

Setlayout (mainlayout );

Slider-> setfocus ();

Connect (quitbtn, signal (clicked (), qapp, slot (quit ()));

Setwindowtitle ("Celsius ");
}

Void cel: createcel ()
{

Slider = new qslider (QT: vertical );

Slider-> setrange (0,100 );

Slider-> setvalue (0 );

Slider-> settickposition (qslider: ticksleft );

Qlabel * cellabel = new qlabel ("0 ");

Cellayout = new qhboxlayout;

Cellayout-> addwidget (cellabel, 0, QT: alignright );

Cellayout-> addwidget (slider, 0, QT: alignleft );

Cellayout-> setspacing (10 );

Connect (slider, signal (valuechanged (INT), cellabel, slot (setnum (INT )));

Connect (slider, signal (valuechanged (INT), this, slot (sendmsg (INT )));
}

Void cel: fahtocel (INT fahnum)
{

Int celnum = (fahnum-32) * 5/9;

Slider-> setvalue (celnum );
}

Void cel: listenchannel ()
{

Qcopchannel * channel = new qcopchannel ("/system/temperature", this); // register a channel

Connect (Channel, signal (received (const qstring &, const qbytearray &)),

This, slot (handlemsg (const qstring &, const qbytearray &)));
}

Void cel: handlemsg (const qstring & message, const qbytearray & Data) // process received information
{

Qdatastream in (data );

If (Message = "convertfahtocel (INT )")

{

Int fahnum;

In> fahnum;

Fahtocel (fahnum );

}
}

Void cel: sendmsg (INT celnum)
// Send information
{

Qbytearray data;

Qdatastream out (& Data, qiodevice: writeonly );

Out <celnum;

Qcopchannel: Send ("/system/temperature", "convertceltofah (INT)", data );
}
# Include <qapplication>

# Include "cel. H"

Int main (INT argc, char * argv [])
{

Qapplication app (argc, argv/*, qapplication: guiclient */);

Cel screen;

Screen. setgeometry (0, 25,100,250 );

Screen. Show ();

Return app.exe C ();
}

 

// Header file of Fah. h subroutine 2
# Ifndef fah_h
# Define fah_h

# Include <qwidget>

Class qdial;
Class qvboxlayout;

Class Fah: Public qwidget
{

Q_object

Public:

Fah ();

~ Fah (){};

Private slots:

Void handlemsg (const qstring & message, const qbytearray & data );

Void sendmsg (INT fahnum );
// Custom slot

PRIVATE:

Void celtofah (INT celnum );

Void createscreen ();

Void createfah ();

Void listenchannel ();

Qdial * dial;

Qvboxlayout * fahlayout;
};

# Endif // fah_h
# Include <qpushbutton>
# Include <qdial>
# Include <qlcdnumber>
# Include <qvboxlayout>
# Include <qapplication>
# Include <qcopchannel>
# Include <qdatastream>
# Include <qbytearray>

# Include "Fah. H"

Fah: Fah (): qwidget ()
{

Createscreen ();

Listenchannel ();
}

Void Fah: createscreen ()
{

Qpushbutton * quitbtn = new qpushbutton ("quit ");

Createfah ();

Qvboxlayout * mainlayout = new qvboxlayout;

Mainlayout-> addwidget (quitbtn );

Mainlayout-> addlayout (fahlayout );

Setlayout (mainlayout );

Dial-> setfocus ();

Connect (quitbtn, signal (clicked (), qapp, slot (quit ()));

Setwindowtitle ("Fahrenheit ");
}

Void Fah: createfah ()
{

Qlcdnumber * lcdnum = new qlcdnumber (3 );

Lcdnum-> setsegmentstyle (qlcdnumber: filled );

Lcdnum-> display (32 );

Dial = new qdial;

Dial-> setrange (32,212 );

Dial-> setvalue (32 );

Dial-> setnotchesvisible (true );

Fahlayout = new qvboxlayout;

Fahlayout-> addwidget (lcdnum, 0, QT: alignbottom | QT: alignhcenter );

Fahlayout-> addwidget (Dial );

Fahlayout-> setspacing (10 );

Connect (DIAL, signal (valuechanged (INT), lcdnum, slot (Display (INT )));

Connect (DIAL, signal (valuechanged (INT), this, slot (sendmsg (INT )));
}

Void Fah: celtofah (INT celnum)
{

Int fahnum = (celnum * 9/5) + 32;

Dial-> setvalue (fahnum );
}

Void Fah: listenchannel ()
{

Qcopchannel * channel = new qcopchannel ("/system/temperature", this );

Connect (Channel, signal (received (const qstring &, const qbytearray &)),

This, slot (handlemsg (const qstring &, const qbytearray &)));
}

Void Fah: handlemsg (const qstring & message, const qbytearray & Data)
{

Qdatastream in (data );

If (Message = "convertceltofah (INT )")

{

Int celnum;

In> celnum;

Celtofah (celnum );

}
}

Void Fah: sendmsg (INT fahnum)
{

Qbytearray data;

Qdatastream out (& Data, qiodevice: writeonly );

Out <fahnum;

Qcopchannel: Send ("/system/temperature", "convertfahtocel (INT)", data );
}

# Include <qapplication>

# Include "Fah. H"

Int main (INT argc, char * argv [])
{

Qapplication app (argc, argv );

Fah screen;

Screen. setgeometry( 130, 25,100,250 );

Screen. Show ();

Return app.exe C ();
}

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.