+ Reply to Thread
Results 1 to 7 of 7

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

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Send and receive a signal between 2 Qt applications.

    I am running ubuntu linux 10.10.

    Well, my program runs only on one instance, so what I need is to open my application's project file while the application is already running. By clicking this project file, the new program instance realize that there is already one running and it shows a message that the program is already open. What I want is to open the project file with the already running process. So, I want
    1) If the program is already running and
    2) there are arguments (a project file has been opened)
    to open the project file with the already running process.

    So, the new instance may send the URL of the project file to the already running process and the already running process open it. I have not idea how to do this. Maybe System-Wide Signal-Slot ?

    Sorry if this is not a Newbie question. Fell free to move it to the Programming talk.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    If you use QtSingleApplication, you can use isRunning and sendMessage methods of that class. isRunning will tell you if another copy is running in memory, whilst sendMessage will allow you to talk to that instance. The already running instance will receive the signal messageReceived.

    docs: http://doc.qt.nokia.com/solutions/4/...plication.html

    You can download QtSingleApplication from the GIT repository: http://qt.gitorious.org/qt-solutions

  3. The following user says thank you to squidge for this useful post:

    hakermania (5th February 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Hey thanks, I'm not using QtSingleApplication!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,939
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

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

    You might want to look into Inter Process Communication (IPC)

    A popular choice is a TCP socket.
    Or shared memory.
    Depending on the OS, there's also DBus

  6. The following user says thank you to tbscope for this useful post:

    hakermania (5th February 2011)

  7. #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 

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

    hakermania (5th February 2011)

  9. #6
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Thank you all for the interest. I am interested in DBUS (because there is QDbus lib), but I'll see other solutions as well! Thank you


    Added after 1 12 minutes:


    I am tryin' got figure it out with QDBus. I was able to send a DBus message(I was able to see the "HELLO" string with dbus-monitor) with this code:
    Qt Code:
    1. QDBusMessage msg = QDBusMessage::createSignal("/", "open.album", "message");
    2. msg << "HELLO";
    3. QDBusConnection::sessionBus().send(msg);
    To copy to clipboard, switch view to plain text mode 
    Where open.album.xml is
    Qt Code:
    1. <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
    2. "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
    3. <node>
    4. <interface name="open.album">
    5. <signal name="message">
    6. <arg name="album" type="s" direction="out"/>
    7. </signal>
    8. <signal name="action">
    9. <arg name="album" type="s" direction="out"/>
    10. </signal>
    11. </interface>
    12. </node>
    To copy to clipboard, switch view to plain text mode 
    Ok, all right till now, But I've no Idea how will I tell the other application to listen to the specific interface for a specific string and do things with this string!
    Any help?
    Last edited by hakermania; 5th February 2011 at 19:15.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  10. #7
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Ok, it is simple.
    Qt Code:
    1. //this set it ready to receive a message to a specific chanell, once received, album_slot() will run
    2. QDBusConnection::sessionBus().connect(QString(),QString(), "open.album", "MY_message", this, SLOT(album_slot(QString)));
    3. //these 3 lines create a signal and send it to DBus
    4. QDBusMessage msg = QDBusMessage::createSignal("/", "open.album", "MY_message");
    5. msg="text"
    6. QDBusConnection::sessionBus().send(msg);
    7. ...
    8. ...
    9. void album_slot(const QString &text){
    10. if(text=="text") etc
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by hakermania; 5th February 2011 at 21:40.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

+ Reply to Thread

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, 17:33
  2. receive signal from a thread of an external program
    By bigbill in forum Qt Programming
    Replies: 5
    Last Post: 1st December 2010, 23:41
  3. not able to receive SIGNAL
    By thefriedc in forum Newbie
    Replies: 4
    Last Post: 28th September 2010, 09:34
  4. How to send SMS from a desktop qt Applications?
    By newtowindows in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2009, 20:05
  5. How to send and receive Email ??
    By wincry in forum Newbie
    Replies: 4
    Last Post: 18th October 2009, 18: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