newbie slot/signal question

28 views
Skip to first unread message

pbutler

unread,
Apr 2, 2012, 2:10:01 PM4/2/12
to lqt-bindings
I'm trying to understand why I can add a slot to a QTcpSocket when I
create the socket using QTcpSocket.new() but not when I get one
returned from a listening server socket.

__addmethod() seems to work correctly in the first case, however it
doesn't add the method in the second case.

What is the correct way to do this?



require "qtcore"
require "qtnetwork"

sock1 = QTcpSocket.new()

sock1:__addmethod('gotsome()', function() print('got some!') end)

table.foreachi(sock1:__methods(), print)
1 destroyed(QObject*) Protected Signal
2 destroyed() Protected Signal
3 deleteLater() Public Slot
4 _q_reregisterTimers(void*) Private Slot
5 readyRead() Protected Signal
6 bytesWritten(qint64) Protected Signal
7 aboutToClose() Protected Signal
8 readChannelFinished() Protected Signal
9 hostFound() Protected Signal
10 connected() Protected Signal
11 disconnected() Protected Signal
12 stateChanged(QAbstractSocket::SocketState) Protected Signal
13 error(QAbstractSocket::SocketError) Protected Signal
14 proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)
Protected Signal
15 connectionClosed() Protected Signal
16 delayedCloseFinished() Protected Signal
17 connectToHostImplementation(QString,quint16,OpenMode) Protected
Slot
18 connectToHostImplementation(QString,quint16) Protected Slot
19 disconnectFromHostImplementation() Protected Slot
20 _q_connectToNextAddress() Private Slot
21 _q_startConnecting(QHostInfo) Private Slot
22 _q_abortConnectionAttempt() Private Slot
23 _q_testConnection() Private Slot
24 _q_forceDisconnect() Private Slot
25 gotsome() Public Slot




addr = QHostAddress('192.168.145.151')
port = 44412

server = QTcpServer()

server:listen(addr, port)

server:waitForNewConnection(-1)

sock2 = server:nextPendingConnection()

sock2:__addmethod('gotsome()', function() print('got some!') end)


table.foreachi(sock2:__methods(), print)
1 destroyed(QObject*) Protected Signal
2 destroyed() Protected Signal
3 deleteLater() Public Slot
4 _q_reregisterTimers(void*) Private Slot
5 readyRead() Protected Signal
6 bytesWritten(qint64) Protected Signal
7 aboutToClose() Protected Signal
8 readChannelFinished() Protected Signal
9 hostFound() Protected Signal
10 connected() Protected Signal
11 disconnected() Protected Signal
12 stateChanged(QAbstractSocket::SocketState) Protected Signal
13 error(QAbstractSocket::SocketError) Protected Signal
14 proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)
Protected Signal
15 connectionClosed() Protected Signal
16 delayedCloseFinished() Protected Signal
17 connectToHostImplementation(QString,quint16,OpenMode) Protected
Slot
18 connectToHostImplementation(QString,quint16) Protected Slot
19 disconnectFromHostImplementation() Protected Slot
20 _q_connectToNextAddress() Private Slot
21 _q_startConnecting(QHostInfo) Private Slot
22 _q_abortConnectionAttempt() Private Slot
23 _q_testConnection() Private Slot
24 _q_forceDisconnect() Private Slot

Michal Kottman

unread,
Apr 2, 2012, 2:20:17 PM4/2/12
to lqt-bi...@googlegroups.com
On 2 April 2012 20:10, pbutler <butle...@gmail.com> wrote:
I'm trying to understand why I can add a slot to a QTcpSocket when I
create the socket using QTcpSocket.new() but not when I get one
returned from a listening server socket.

__addmethod() seems to work correctly in the first case, however it
doesn't add the method in the second case.

What is the correct way to do this?

Unfortunately this is due to the design on Lqt - in order to support custom signals/slots, every QObject derived class in Lqt has a "shell" class, which handles virtual overloads and also custom signals/slots. This is relatively simple, because the creation of objects is under Lqt control, and therefore instead of  QTcpSocket it creates lqt_shell_QTcpSocket.

The problem is with objects returned from other functions. Since they are already constructed as "pure" QTcpSocket, the object does not handle the special Lqt signals/slots and neither the virtual overloads. I do not know a portable way to change a class of an object at runtime, maybe except hacking with the virtual function table of the object and pointing it at the shell class, but this is neither portable nor supported.
Reply all
Reply to author
Forward
0 new messages