populate combobox editable username and line edit password from sqlite3

后端 未结 1 1103
滥情空心
滥情空心 2021-01-14 20:42

I have 1 combobox editable username and 1 line edit password i want load user list from database sqlite3 to combobox. After load list i want action on itemlist combobox get

相关标签:
1条回答
  • 2021-01-14 21:16

    Assuming that the table is called "user" then you can use the userData to save the information and set it in the QLineEdit when the currentIndex of the QComboBox changes:

    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_())
    

    Another possible solution is to use QSqlQueryModel with 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_())
    
    0 讨论(0)
提交回复
热议问题