在一个单元格小部件中处理多个QComboBox

时间:2017-09-12 16:14:30

标签: python pyqt pyqt4 qtablewidget qcombobox

代码:

class Example(QtGui.QWidget):
    def __init__(self, parent):
        super(Accounts, self).__init__()
        self.initUI()

    def initUI(self):
        self.table = QtGui.QTableWidget()
        self.table.setColumnCount(5)

        database = 'Setting.db'
        connection = sqlite3.connect(database)
        cursor = connection.cursor()

        cursor.execute('''
                                SELECT ID, User, Text
                                FROM Data ''')


        data = cursor.fetchall()
        num = 0
        for row in data:
            self.table.setRowCount(num+2)
            self.table.setItem(num, 0, QtGui.QTableWidgetItem(str(row[0])))
            self.table.setItem(num, 1, QtGui.QTableWidgetItem(str(row[1])))
            self.table.setItem(num, 2, QtGui.QTableWidgetItem(str(row[2])))
            self.table.setCellWidget(num, 3, Perms(self, row[0]))

            save_user = QtGui.QPushButton("Save")
            save_user.clicked.connect(self.save)

            self.table.setCellWidget(num, 5, save_user)

            num= num+1

        main_layout = QtGui.QGridLayout()
        main_layout.addWidget(self.table)
        self.setLayout(main_layout)

    def save(self):
        button = self.sender()
        index = self.table.indexAt(button.pos())
        row = index.row()
        a_id = str(self.table.item(row, 0).text())
        data = Permissions(self, a_id).update_database()

class Perms(QtGui.QWidget):
    def __init__(self, parent, a_id):
        super(Perms, self).__init__()
        self.a_id = a_id
        self.init_ui()

    def init_ui(self):
        database = 'Settings.db'
        conn = sqlite3.connect(database)
        cursor = conn.cursor()

        cursor.execute('''
                                SELECT control, sub
                                FROM Perms
                                WHERE ID= ?''', (self.a_id,))

        row = cursor.fetchone()

        control_label = QtGui.QLabel("Control")
        sub_label = QtGui.QLabel("Sub")

        self.control_options = QtGui.QComboBox()
        self.control_options.addItem("Y")
        self.control_options.addItem("N")
        if str(row[0]) == "Y":
            self.control_options.setCurrentIndex(0)
        else:
            self.control_options.setCurrentIndex(1)

        self.sub_options = QtGui.QComboBox()
        self.sub_options.addItem("Y")
        self.sub_options.addItem("N")
        if str(row[1]) == "Y":
            self.sub_options.setCurrentIndex(0)
        else:
            self.sub_options.setCurrentIndex(1)

        layout = QtGui.QGridLayout()

        layout.addWidget(full_control_label, 1, 1)
        layout.addWidget(self.full_control_options, 1, 2)

        layout.addWidget(tills_label, 2, 1)
        layout.addWidget(self.tills_options, 2, 2)

        self.setLayout(layout)

    def update_database(self):
        control = str(self.control_options.currentText())
        sub = str(self.sub_options.currentText())
        return (control, sub)

忽略我没有指定的任何错误,因为我只是为了一个例子而快速写下来。我面临的问题是:在循环中创建保存按钮以便多次使用。它连接到一个获取组合框当前文本的函数。但问题是,如果您更改组合框中的当前文本然后单击“保存”,它将从组合框中获取文本,但不会获得正确的文本 - 您始终会得到相同的结果。

例如:

  

QComboBox1:Y
  QComboBox2:N

如果我们点击保存,则会返回(' Y',' N')

如果我们更改组合框值:

  

QComboBox1:N
  QComboBox2:N

并点击保存,它会返回(' Y',' N')

但它永远不会从组合框中返回新值。

如何修复它以便获取更新的值而不是旧值?

1 个答案:

答案 0 :(得分:0)

Perm方法目前没有多大意义。当然,您需要从表中获取def save(self): button = self.sender() index = self.table.indexAt(button.pos()) row = index.row() # get the new values from the table data = self.table.cellWidget(row, 3).update_database() 小部件,而不是每次都创建一个新小部件:

{{1}}