填充组合框可编辑 human_name 和行编辑 phone 以及来自 mariadb 的电子邮件
populate combobox editable human_name and line edit phone and email from mariadb
我有 table hr 和 1 个带有 hr 列表的组合框,我想向 hremail_lineEdit
和 hrphone_lineEdit
显示电子邮件和 phone,但我只能显示 phone 至 hrphone_lineEdit
。
def hr_name(self):
self._conn = pymysql.connect(host=127.0.0.1, port=3306, user='root', passwd=root, db='testhr', charset='utf8')
self._cur = self._conn.cursor()
sql_coop4hr = 'select human_name,email,phone from hr'
count_coop4hr = self._cur.execute(sql_coop4hr)
res_coop4hr = self._cur.fetchall()
for row in res_coop4hr:
un, email, phone = row
self.hr_name_comboBox.addItem(un, email)
def hr_email(self):
self.hremail_lineEdit.setText(self.hr_name_comboBox.currentData()) #email
self.hrphone_lineEdit.setText(self.hr_name_comboBox.currentData()) #phone
有几种选择:
将电子邮件和 phone 作为元组(或列表)传递给用户数据:
self.hr_name_comboBox.clear()
for row in res_coop4hr:
un, email, phone = row
self.hr_name_comboBox.addItem(un, (email, phone))
def hr_email(self):
email, phone = self.hr_name_comboBox.currentData()
self.hremail_lineEdit.setText(email)
self.hrphone_lineEdit.setText(phone)
创建与每个数据关联的 2 个角色:
EMAIL_ROLE = Qt.UserRole
PHONE_ROLE = Qt.UserRole + 1
self.hr_name_comboBox.clear()
for i, row in enumerate(res_coop4hr):
un, email, phone = row
self.hr_name_comboBox.insertItem(i, un)
self.hr_name_comboBox.setItemData(i, email, EMAIL_ROLE)
self.hr_name_comboBox.setItemData(i, phone, PHONE_ROLE)
def hr_email(self):
index = self.hr_name_comboBox.currentIndex()
email = self.hr_name_comboBox.itemData(index, EMAIL_ROLE)
phone = self.hr_name_comboBox.itemData(index, PHONE_ROLE)
self.hremail_lineEdit.setText(email)
self.hrphone_lineEdit.setText(phone)
我有 table hr 和 1 个带有 hr 列表的组合框,我想向 hremail_lineEdit
和 hrphone_lineEdit
显示电子邮件和 phone,但我只能显示 phone 至 hrphone_lineEdit
。
def hr_name(self):
self._conn = pymysql.connect(host=127.0.0.1, port=3306, user='root', passwd=root, db='testhr', charset='utf8')
self._cur = self._conn.cursor()
sql_coop4hr = 'select human_name,email,phone from hr'
count_coop4hr = self._cur.execute(sql_coop4hr)
res_coop4hr = self._cur.fetchall()
for row in res_coop4hr:
un, email, phone = row
self.hr_name_comboBox.addItem(un, email)
def hr_email(self):
self.hremail_lineEdit.setText(self.hr_name_comboBox.currentData()) #email
self.hrphone_lineEdit.setText(self.hr_name_comboBox.currentData()) #phone
有几种选择:
将电子邮件和 phone 作为元组(或列表)传递给用户数据:
self.hr_name_comboBox.clear() for row in res_coop4hr: un, email, phone = row self.hr_name_comboBox.addItem(un, (email, phone))
def hr_email(self): email, phone = self.hr_name_comboBox.currentData() self.hremail_lineEdit.setText(email) self.hrphone_lineEdit.setText(phone)
创建与每个数据关联的 2 个角色:
EMAIL_ROLE = Qt.UserRole PHONE_ROLE = Qt.UserRole + 1
self.hr_name_comboBox.clear() for i, row in enumerate(res_coop4hr): un, email, phone = row self.hr_name_comboBox.insertItem(i, un) self.hr_name_comboBox.setItemData(i, email, EMAIL_ROLE) self.hr_name_comboBox.setItemData(i, phone, PHONE_ROLE)
def hr_email(self): index = self.hr_name_comboBox.currentIndex() email = self.hr_name_comboBox.itemData(index, EMAIL_ROLE) phone = self.hr_name_comboBox.itemData(index, PHONE_ROLE) self.hremail_lineEdit.setText(email) self.hrphone_lineEdit.setText(phone)