I have the following class
class GUI( QtGui.QMainWindow ):
'''
classdocs
'''
"""**********************************************************************"""
""" Constructor """
"""**********************************************************************"""
def __init__( self, parent = None ):
self.udpClass = MCUDP.MCUDP()
def insertText( self, string ):
string = time.ctime() + ': ' + string + '\n'
self.messageField.insertPlainText( string )
And I also have MCUDP class created in the GUI class. My question is how can I reach the GUI class insertText function in MCUDP
UPDATED
this is the MCUDP
'''
Created on 09.06.2011
@author: robu
'''
import socket
import time
import MCGui;
class MCUDP( object ):
'''
classdocs
'''
"""**********************************************************************"""
""" UDP: Broadcasting """
"""**********************************************************************"""
def UDPBroadcast( self, ip = "255.255.255.255", UDPport = 15000, message = 'whoisthere', timeout = 10, TCPport = 30000 ):
# ip="255.255.255.255" stands for a broadcast
ip = str( ip )
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP )
s.setsockopt( socket.SOL_SOCKET, socket.SO_BROADCAST, True )
s.settimeout( timeout )
ownIP = socket.gethostbyname( socket.gethostname() )
if message.upper() == 'WHOISTHERE':
message = message + ';' + ownIP + ':' + str( TCPport )
#print "Trying to send '%s' to IP %s, Port %s!" %(message, ip, port)
#self.Eingang.put("Trying to send '%s' to IP %s, Port %s!" %(message, ip, UDPport))
s.sendto( message, ( ip, UDPport ) )
answer = "%s: '%s' broadcasted to %s!" % ( time.asctime(), message, ip )
GUI.insertText( 'test' );
#print answer
s.close()
return answer
You have two objects that need to communicate with each other, which is a fairly standard communication problem. There’s a number of ways that this problem can be solved:
(1) Dependency Injection – Make your MCUDP() class require the MCGUI class at construction time.
You’ll then have it available whenever you need:
If you do this your MCUDP class becomes dependent on an object that implements all the methods of
self.guithat MCUDP uses. In other words, MCUDP is now coupled directly to the MCGUI class. Of course, the MCGUI class is already dependent on MCUDP to some extent.(2) Message passing – In Qt, Signals and slots. The idiomatic Qt route uses messages instead of function calls:
And then you just need to make your MCUDP class a QObject so that it can emit events:
The benefit of this is now MCUDP doesn’t need to know anything about the MCGUI class which will make both testing and future changes easier.