1

我正在尝试使用 QDataWidgetmapper 更新数据库中的一行,但我的问题是在尝试调用更新函数时 row 不是全局变量,我尝试在调用 Qdialog 输入 int 的同一函数中使用它. 我无法让它工作。我已经尝试了很多变化,但我现在已经束手无策了。我确定我缺少一些简单的东西,但我仍在学习 python 和 pyqt。

import sys
from testdbtableform import *
from PyQt4 import QtSql, QtGui

def createConnection():
    """Creates the pyqt connection to the database"""
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('demo.db')
    if db.open():
        return True
    else:
        print db.lastError().text()
        return False


class MyForm(QtGui.QDialog):
    """Creates the class"""

    def __init__(self, parent=None):
        """Initiates the class"""
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.model = QtSql.QSqlRelationalTableModel(self)
        self.model.setTable("userlist")
        self.model.setSort(0, 0)
        self.model.setEditStrategy(2)
        self.model.select()
        self.mapper = QtGui.QDataWidgetMapper(self)
        self.mapper.setSubmitPolicy(1)
        self.mapper.setModel(self.model)
        self.mapper.addMapping(self.ui.lineEditUser, 0)
        self.mapper.addMapping(self.ui.lineEditEmail, 1)
        self.ui.tableView.setModel(self.model)
        self.ui.pushSearch.clicked.connect(self.dbfilter)
        self.ui.Submit.clicked.connect(self.dbinput)
        self.ui.pushButton.clicked.connect(self.mapindex)
        self.ui.updateButton.clicked.connect(self.updaterow)
        self.ui.updateButton.hide()

    def mapindex(self):
        """Function called to update a row in the tableview and database"""
        i, ok = QtGui.QInputDialog.getInteger(self,
                "Update Row", "Row:", 1, 1, 100, 1)
        row = "%d" % i
        row = int(row) - 1
        if ok:
            self.ui.Submit.hide()
            self.ui.updateButton.show()
            self.mapper.setCurrentIndex(row)

    def updaterow(self):
        """Function to update data from line edits to the database"""
        text1 = self.ui.lineEditUser.text()
        text2 = self.ui.lineEditEmail.text()
        self.model.setData(self.model.index(row, 0), str(text1).upper())
        self.model.setData(self.model.index(row, 1), str(text2))
        self.model.submitAll()
        self.ui.lineEditUser.clear()
        self.ui.lineEditEmail.clear()
        self.ui.Submit.show()
        self.ui.updateButton.hide()

    def dbinput(self):
        """Function to input data from line edits to the database"""
        self.model.insertRow(0)
        text1 = self.ui.lineEditUser.text()
        text2 = self.ui.lineEditEmail.text()
        self.model.setData(self.model.index(0, 0), str(text1).upper())
        self.model.setData(self.model.index(0, 1), str(text2))
        self.model.submitAll()
        self.ui.lineEditUser.clear()
        self.ui.lineEditEmail.clear()

    def dbfilter(self):
        """Function to search through the database for the username"""
        self.model.setFilter("USERNAME like '"+self.ui.lineEditSearch.text()+"%'")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())
4

1 回答 1

1

好吧,在我把头撞在墙上几分钟之后,然后只是在 pyqt irc 频道中观看随机事物的随机 youtube 视频,davidb 问我“行是否与 self.mapper.currentIndex() 相同”所以我改变了下面的行将函数更新为 row = self.mapper.currentIndex() 并且它起作用了。一些简单的事情,我因为感到沮丧而过度关注。下面是正确的函数。

def mapindex(self):
    """Function called to update a row in the tableview and database"""
    i, ok = QtGui.QInputDialog.getInteger(self,
            "Update Row", "Row:", 1, 1, 100, 1)
    row = "%d" % i
    row = int(row) - 1
    if ok:
        self.ui.Submit.hide()
        self.ui.updateButton.show()
        self.mapper.setCurrentIndex(row)


def updaterow(self):
    """Function to update data from line edits to the database"""
    text1 = self.ui.lineEditUser.text()
    text2 = self.ui.lineEditEmail.text()
    row = self.mapper.currentIndex() #being the missing link
    self.model.setData(self.model.index(row, 0), str(text1).upper())
    self.model.setData(self.model.index(row, 1), str(text2))
    self.model.submitAll()
    self.ui.lineEditUser.clear()
    self.ui.lineEditEmail.clear()
    self.ui.Submit.show()
    self.ui.updateButton.hide()
于 2013-06-25T20:14:19.013 回答