Frage

I've just started to work with python, so I run into problem. I've searched everywhere, but I couldn't find similar example. So, the problem is following: I made a simple GUI using QT-Designer. QTableWidget is being filled out by clicking on button Analyse. (as you can see on the picture) link for picture

When I select one checkBox the rest of them are being selected randomly, and I don't know why. As I said, I'm new at Python, so the good explanation would mean a lot to me. Here is the source code:

import sys
from PyQt4 import QtGui,QtCore
from IDCS import *

class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.connect(self.ui.analyseButton, QtCore.SIGNAL('clicked()'), self.doAnalyse)
        self.connect(self.ui.quitButton, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))

    def doAnalyse(self):
        self.ui.tableWidget.setRowCount(10)

        chkBoxItem = QtGui.QTableWidgetItem()
        chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(QtCore.Qt.Unchecked)       

        for i in range(10):
            self.ui.tableWidget.setItem(i, 0, chkBoxItem)
            self.ui.tableWidget.setItem(i, 1, QtGui.QTableWidgetItem("description %s" % (i+1)))
            self.ui.tableWidget.setItem(i, 2, QtGui.QTableWidgetItem("name %s" % (i+1)))
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())e
War es hilfreich?

Lösung

You are making only one instance of the CheckBox and putting it in 10 different places and that is wrong. You have to make 10 instances of CheckBox, and you will do that by moving those 3 lines of code, in which you are creating instance of CheckBox, under the for loop below.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top