Results 1 to 7 of 7

Thread: Send and receive a signal between 2 Qt applications.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Send and receive a signal between 2 Qt applications.

    I am using pyQt but maybe this can help you to find a solution also in C++.
    I am implementing my SingleInstance Applications with the help of QSharedMemory, QLocalSocket and QLocalServer

    Qt Code:
    1. #import std
    2. import os, sys
    3.  
    4. # import stuff for ipc
    5. import getpass, pickle
    6.  
    7. # Import Qt modules
    8. from PyQt4.QtGui import QApplication
    9. from PyQt4.QtCore import QSharedMemory, QIODevice, SIGNAL
    10. from PyQt4.QtNetwork import QLocalServer, QLocalSocket
    11.  
    12. class SingletonApp(QApplication):
    13.  
    14. timeout = 1000
    15.  
    16. def __init__(self, argv, application_id=None):
    17. QApplication.__init__(self, argv)
    18.  
    19. self.socket_filename = unicode(os.path.expanduser("~/.ipc_%s"
    20. % self.generate_ipc_id()) )
    21. self.shared_mem = QSharedMemory()
    22. self.shared_mem.setKey(self.socket_filename)
    23.  
    24. if self.shared_mem.attach():
    25. self.is_running = True
    26. return
    27.  
    28. self.is_running = False
    29. if not self.shared_mem.create(1):
    30. print >>sys.stderr, "Unable to create single instance"
    31. return
    32. # start local server
    33. self.server = QLocalServer(self)
    34. # connect signal for incoming connections
    35. self.connect(self.server, SIGNAL("newConnection()"), self.receive_message)
    36. # if socket file exists, delete it
    37. if os.path.exists(self.socket_filename):
    38. os.remove(self.socket_filename)
    39. # listen
    40. self.server.listen(self.socket_filename)
    41.  
    42. def __del__(self):
    43. self.shared_mem.detach()
    44. if not self.is_running:
    45. if os.path.exists(self.socket_filename):
    46. os.remove(self.socket_filename)
    47.  
    48.  
    49. def generate_ipc_id(self, channel=None):
    50. if channel is None:
    51. channel = os.path.basename(sys.argv[0])
    52. return "%s_%s" % (channel, getpass.getuser())
    53.  
    54. def send_message(self, message):
    55. if not self.is_running:
    56. raise Exception("Client cannot connect to IPC server. Not running.")
    57. socket = QLocalSocket(self)
    58. socket.connectToServer(self.socket_filename, QIODevice.WriteOnly)
    59. if not socket.waitForConnected(self.timeout):
    60. raise Exception(str(socket.errorString()))
    61. socket.write(pickle.dumps(message))
    62. if not socket.waitForBytesWritten(self.timeout):
    63. raise Exception(str(socket.errorString()))
    64. socket.disconnectFromServer()
    65.  
    66. def receive_message(self):
    67. socket = self.server.nextPendingConnection()
    68. if not socket.waitForReadyRead(self.timeout):
    69. print >>sys.stderr, socket.errorString()
    70. return
    71. byte_array = socket.readAll()
    72. self.handle_new_message(pickle.loads(str(byte_array)))
    73.  
    74. def handle_new_message(self, message):
    75. print "Received:", message
    76.  
    77. # Create a class for our main window
    78. class Main(QtGui.QMainWindow):
    79. def __init__(self):
    80. QtGui.QMainWindow.__init__(self)
    81. # This is always the same
    82. self.ui=Ui_MainWindow()
    83. self.ui.setupUi(self)
    84.  
    85. if __name__ == "__main__":
    86. app = SingletonApp(sys.argv)
    87. if app.is_running:
    88. # send arguments to running instance
    89. app.send_message(sys.argv)
    90. else:
    91. MyApp = Main()
    92. MyApp.show()
    93. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to jfjf for this useful post:

    hakermania (5th February 2011)

Similar Threads

  1. How to signal when a Qwidget receive focus
    By franco.amato in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2016, 16:33
  2. receive signal from a thread of an external program
    By bigbill in forum Qt Programming
    Replies: 5
    Last Post: 1st December 2010, 22:41
  3. not able to receive SIGNAL
    By thefriedc in forum Newbie
    Replies: 4
    Last Post: 28th September 2010, 08:34
  4. How to send SMS from a desktop qt Applications?
    By newtowindows in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2009, 19:05
  5. How to send and receive Email ??
    By wincry in forum Newbie
    Replies: 4
    Last Post: 18th October 2009, 17:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.