开发者社区> 问答> 正文

从sqlite3填充combobox可编辑的用户名和行编辑密码

我有一个combobox可编辑的用户名和一行编辑密码我想加载用户列表从数据库sqlite3到combobox。加载列表后,我想行动的项目列表combobox获得密码当前选定用户从数据库。 pyqt5应用程序

表列

表记录

######Combo Box 2 - PM's
        self.groupBox_2 = QtWidgets.QGroupBox(self.tab)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 140, 191, 51))
        self.groupBox_2.setObjectName("groupBox_2")
        self.comboBox_3 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_3.setGeometry(QtCore.QRect(10, 20, 171, 22))
        self.comboBox_3.setObjectName("comboBox_3")
        self.comboBox_3.addItems(self.pm_Combo)

def pm_Combo(self):
        conn = sqlite3.connect('testdb.db')
        c = conn.cursor()
        c.execute("SELECT DISTINCT projectmanager FROM testtable2")
        pmList = c.fetchall()
        conn.commit()
        conn.close()

问题来源StackOverflow 地址:/questions/59386218/populate-combobox-editable-username-and-line-edit-password-from-sqlite3

展开
收起
kun坤 2019-12-25 22:06:31 566 0
1 条回答
写回答
取消 提交回答
  • 假设表名为“user”,那么您可以使用userData保存信息,并在QComboBox的currentIndex更改时在QLineEdit中设置它:

    import os
    import sqlite3
    import sys
    
    from PyQt5 import QtWidgets, QtSql
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.username_combo = QtWidgets.QComboBox()
            self.password_lineedit = QtWidgets.QLineEdit()
    
            button_login = QtWidgets.QPushButton(self.tr("Login"))
    
            lay = QtWidgets.QFormLayout(self)
            lay.addRow(self.tr("Username:"), self.username_combo)
            lay.addRow(self.tr("Password:"), self.password_lineedit)
            lay.addRow(button_login)
    
            self.username_combo.currentIndexChanged.connect(self.update_password)
    
            self.load_usernames()
    
        def load_usernames(self):
            current_dir = os.path.dirname(os.path.realpath(__file__))
            db_path = os.path.join(current_dir, "testdb.db")
    
            self.username_combo.clear()
            with sqlite3.connect(db_path) as conn:
                cursor = conn.execute("SELECT DISTINCT username, password FROM User")
                for result in cursor.fetchall():
                    username, password = result
                    self.username_combo.addItem(username, password)
    
        def update_password(self):
            self.password_lineedit.setText(self.username_combo.currentData())
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    

    另一个可能的解决方案是使用QSqlQueryModel与QDataWidgetMapper:

    import os
    import sys
    
    from PyQt5 import QtWidgets, QtSql
    
    
    def create_connection():
        current_dir = os.path.dirname(os.path.realpath(__file__))
        db_path = os.path.join(current_dir, "testdb.db")
    
        db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        db.setDatabaseName(db_path)
        if not db.open():
            print("error: {}".format(db.lastError().text()))
            return False
        return True
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.username_combo = QtWidgets.QComboBox()
            self.password_lineedit = QtWidgets.QLineEdit()
    
            button_login = QtWidgets.QPushButton(self.tr("Login"))
    
            lay = QtWidgets.QFormLayout(self)
            lay.addRow(self.tr("Username:"), self.username_combo)
            lay.addRow(self.tr("Password:"), self.password_lineedit)
            lay.addRow(button_login)
            self.load_data()
    
        def load_data(self):
            self.model = QtSql.QSqlQueryModel()
            self.model.setQuery("""SELECT DISTINCT username, password FROM User""")
            self.mapper = QtWidgets.QDataWidgetMapper()
            self.mapper.setModel(self.model)
            self.mapper.addMapping(self.password_lineedit, 1)
            self.username_combo.currentIndexChanged.connect(self.mapper.setCurrentIndex)
            self.username_combo.setModel(self.model)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
    
        if not create_connection():
            sys.exit(-1)
    
        w = Widget()
        w.show()
    
        sys.exit(app.exec_())
    
    2019-12-25 22:06:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载