item = QtGui.QStandardItem() item.setText(member) member_list_model.appendRow(item) self.member_list.setModel(member_list_model) @QtCore.pyqtSlot() def update_messages(self): msg_list = self.client.recv_msg() if not msg_list: print 'No New Message' return print 'updating messages' for msg in msg_list: msg_from, msg_time, msg_content = msg.split('\t') message = u'{0} ({1}):\n{2}\n'.format(msg_from, msg_time, msg_content) self.message_browser.append(message) def keyPressEvent(self, QKeyEvent): if QKeyEvent.key() == QtCore.Qt.Key_Return: self.send_msg() if __name__ == '__main__': client = ChatClient() client.login(u'张宝华', '123') client.join_room(u'香港记者招待会') app = QtGui.QApplication(sys.argv) chat_window = ChatWindow(client) chat_window.show() sys.exit(app.exec_())
class LoginDialog(QtGui.QDialog): def __init__(self, callback_window): super(LoginDialog, self).__init__() self.callback_window = callback_window self.init_ui() def init_ui(self): self.setWindowTitle('Claptrap Login') self.setWindowIcon(QtGui.QIcon('img\\claptrap.ico')) self.resize(300, 300) self.host_label = QtGui.QLabel('Host', self) self.host_input = QtGui.QLineEdit('127.0.0.1', self) self.port_label = QtGui.QLabel('Port', self) self.port_input = QtGui.QLineEdit('6666', self) self.username_label = QtGui.QLabel('Username', self) self.username_input = QtGui.QLineEdit('xiaoming', self) self.password_label = QtGui.QLabel('Password', self) self.password_input = QtGui.QLineEdit('123', self) self.login_btn = QtGui.QPushButton('Login', self) self.login_btn.clicked.connect(self.login) layout = QtGui.QVBoxLayout(self) layout.addWidget(self.host_label) layout.addWidget(self.host_input) layout.addWidget(self.port_label) layout.addWidget(self.port_input) layout.addWidget(self.username_label) layout.addWidget(self.username_input) layout.addWidget(self.password_label) layout.addWidget(self.password_input) layout.addWidget(self.login_btn) # todo: limit chars def login(self): host = self.host_input.text() port = self.port_input.text() if not host or not port: QtGui.QMessageBox.warning(self, 'Error', 'Please input host address and port number.') return try: self.client = ChatClient(host, port) except: QtGui.QMessageBox.warning(self, 'Connection Failed', 'Failed to connect to host.') return username = unicode(self.username_input.text()) if not username: QtGui.QMessageBox.warning(self, 'Error', 'Please input username.') return password = self.password_input.text() if not username: QtGui.QMessageBox.warning(self, 'Error', 'Please input password.') return login_resp = self.client.login(username, password) if not login_resp: QtGui.QMessageBox.warning(self, 'Login Failed', 'Wrong username/password. Or the user has already logged in.') return self.client.username = username self.callback_window.client = self.client print 'login OK' self.accept() def keyPressEvent(self, QKeyEvent): if QKeyEvent.key() == QtCore.Qt.Key_Return: self.login()