Re: [PyQt] QString, QTextEdit and Encoding problem
On 28.06.11 08:37:06, Yaşar Arabacı wrote: Thanks for the info. Do you know any decent example, tutorial, documents etc. about qt network stack? There should be examples coming with PyQt, including a small chat-app. In addition look at http://doc.qt.nokia.com/4.7/qtnetwork.html and the check out the corresponding PyQt api docs for the classes mentioned there. Andreas ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] QString, QTextEdit and Encoding problem
Thanks. Appereantly package for my distro didn't include them so I downloaded original source files. 2011/6/28 Andreas Pakulat ap...@gmx.de On 28.06.11 08:37:06, Yaşar Arabacı wrote: Thanks for the info. Do you know any decent example, tutorial, documents etc. about qt network stack? There should be examples coming with PyQt, including a small chat-app. In addition look at http://doc.qt.nokia.com/4.7/qtnetwork.html and the check out the corresponding PyQt api docs for the classes mentioned there. Andreas ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] QString, QTextEdit and Encoding problem
Hi, I am havin an encoding problem. If you have read my earlier post, I was doing a simple chat application. Here is how it goes. === Client Class === def on_lineEdit_returnPressed(self): text= self.ui.lineEdit.text() self.ui.lineEdit.selectAll() self.ui.lineEdit.cut() self.ui.textEdit.paste() self.netconnector.sendMessage(text) === netconnector === def sendMessage(self,msg): if self.isConnected: self.socket.send(msg.toUtf8().trimmed()) === Server Side: === === Server Class === self.connect(self.netconnector,QtCore.SIGNAL(dataRecieved(QString)),self.messageRecieved) === server side netconnector: === def messageRecieved(self,data): self.ui.textEdit.append(data) What happens is that, when I type non-ascii characters into client side line edit, it becomes distorted at server side, like şğü -- ÅÄü Any idea how to fix this? You can find whole code as attachment. #!/usr/bin/python # -*- coding: utf-8 -*- # chat_server import sys from PyQt4 import QtCore,QtGui from chat_ui import Ui_MainWindow import socket class Client(QtGui.QMainWindow): def __init__(self,parent=None): super(Client,self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.textEdit.append(Trying to connect to server) self.netconnector = NetConnectWorker(self) self.netconnector.attemptConnect() self.connect(self.netconnector,QtCore.SIGNAL(Connected()),self.connected) #self.connect(self.netconnector,QtCore.SIGNAL(dataRecieved(QString)),self.messageRecieved) self.connect(self.netconnector,QtCore.SIGNAL(socketError()),self.socketError) self.ui.lineEdit def on_lineEdit_returnPressed(self): text= self.ui.lineEdit.text() self.ui.lineEdit.selectAll() self.ui.lineEdit.cut() self.ui.textEdit.paste() self.netconnector.sendMessage(text) def connected(self): self.ui.textEdit.append(Connected) #def messageRecieved(self,data): #self.ui.textEdit.append(data) # def socketError(self): self.ui.textEdit.append(Couldn't connect lol :S) class NetConnectWorker(QtCore.QThread): def __init__(self,parent=None): super(NetConnectWorker,self).__init__(parent) self.exiting = False self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.host = 'localhost' self.port = 5 self.size = 5 self.isConnected=False def __del__(self): self.exiting = True self.wait() def attemptConnect(self): self.start() def sendMessage(self,msg): if self.isConnected: self.socket.send(msg.toUtf8().trimmed()) def run(self): try: self.socket.connect((self.host,self.port)) self.isConnected = True except socket.error: self.emit(QtCore.SIGNAL(socketError())) return self.emit(QtCore.SIGNAL(Connected())) if __name__ == __main__: app = QtGui.QApplication(sys.argv) sv=Client() sv.show() sys.exit(app.exec_())#!/usr/bin/python # -*- coding: utf-8 -*- # chat_server import sys from PyQt4 import QtCore,QtGui from chat_ui import Ui_MainWindow import socket class Server(QtGui.QMainWindow): def __init__(self,parent=None): super(Server,self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.textEdit.append(Waiting for inbound connections) self.netconnector = NetConnectWorker(self) self.netconnector.attemptConnect() self.connect(self.netconnector,QtCore.SIGNAL(Connected()),self.connected) self.connect(self.netconnector,QtCore.SIGNAL(dataRecieved(QString)),self.messageRecieved) self.connect(self.netconnector,QtCore.SIGNAL(socketError(QString)),self.socketError) def connected(self): self.ui.textEdit.append(Connected) def messageRecieved(self,data): self.ui.textEdit.append(data) def socketError(self,msg): print msg self.ui.textEdit.append(Encountered a socket error:\n %s % msg) class NetConnectWorker(QtCore.QThread): def __init__(self,parent=None): super(NetConnectWorker,self).__init__(parent) self.exiting = False self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.host = '' self.port = 5 self.backlog = 5 self.address = self.client = def __del__(self): self.exiting = True self.wait() if self.client: self.client.close() def attemptConnect(self): self.start() def run(self): try: self.socket.bind((self.host,self.port)) except
Re: [PyQt] QString, QTextEdit and Encoding problem
On 28.06.11 06:26:24, Yaşar Arabacı wrote: Hi, I am havin an encoding problem. If you have read my earlier post, I was doing a simple chat application. Here is how it goes. You're not converting your data correctly. On the sending side you do it properly, decoding to utf-8 and then sending the bytes through the network. On the receiving side you're using Python's socket API, which gives you a byte-stream and then create a QString from it implicitly. That uses whatever your systems default-encoding is to decode the bytes into a string. The error is in NetConnectorWorker.run, but its not that easy to fix since you currently do not send the amount of bytes you cannot know on the receiving side wether all data has been received or not and hence you cannot do a correct decoding. I suggest to switch to use Qt's network stack, that'll make these things a bit easier since you can use QDataStream on the network socket which handles sending QString's just fine. If thats not possible, you'll have to add the length of the message into the byte-stream or always read up to the first \n and decode the content then and make sure the sender always sends at least 1 \n. Andreas ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] QString, QTextEdit and Encoding problem
Thanks for the info. Do you know any decent example, tutorial, documents etc. about qt network stack? 2011/6/28 Andreas Pakulat ap...@gmx.de On 28.06.11 06:26:24, Yaşar Arabacı wrote: Hi, I am havin an encoding problem. If you have read my earlier post, I was doing a simple chat application. Here is how it goes. You're not converting your data correctly. On the sending side you do it properly, decoding to utf-8 and then sending the bytes through the network. On the receiving side you're using Python's socket API, which gives you a byte-stream and then create a QString from it implicitly. That uses whatever your systems default-encoding is to decode the bytes into a string. The error is in NetConnectorWorker.run, but its not that easy to fix since you currently do not send the amount of bytes you cannot know on the receiving side wether all data has been received or not and hence you cannot do a correct decoding. I suggest to switch to use Qt's network stack, that'll make these things a bit easier since you can use QDataStream on the network socket which handles sending QString's just fine. If thats not possible, you'll have to add the length of the message into the byte-stream or always read up to the first \n and decode the content then and make sure the sender always sends at least 1 \n. Andreas ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt