0

So I've been working on a quiz software with PyQt5. Before displaying the results, I designed it to show a table of the questions that have been answered and the answers selected for them (MCQs only) so that if the user wanted to correct an answer they could do that before submitting. I could think of two ways to do this. One was to get the row number of the cell clicked and then use that to index through the list(self.history) I used to input questions.

for x in self.history:
        number = self.history.index(x)
        item = QtWidgets.QTableWidgetItem()
        
        item.setText(f"Question no. {number + 1}")
        item = self.answers_table.verticalHeaderItem(number)

        question = qtw.QTableWidgetItem()
        self.answers_table.setItem(number, 0, question)
        question.setText(x.question)
        question.setFlags(QtCore.Qt.ItemIsEnabled)

        answer_select = qtw.QTableWidgetItem()
        self.answers_table.setItem(number, 1, answer_select)
        answer_select.setText(x.answer_select)
        answer_select.setFlags(QtCore.Qt.ItemIsEnabled)

So if I could get the row number, I could then use it as an index to that object in the list and that would work. But I can't seem to find a way to get that.

The other way I thought I could do this was if I could keep that index as an invisible value like I can in QComboBox in the currentData() variable but I can't seem to find a way to do that either without making it a visible column.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • I sincerely don't understand what exactly are you asking. You say that you need to know the row number, but it's not clear when/where you need it. Also, don't use `index()` to find the row during iteration: just use `for number, x in enumerate(self.history):`. – musicamante Jan 12 '22 at 16:26
  • Also, please avoid writing that you're in a hurry and you need answer as soon as possible: nobody would be encouraged to stop whatever they're doing in order to answer you just because you wrote that (on the contrary, many will completely ignore your question for that request). This is not a debugging or code provider service, it's a website that provides answers that are useful for the community, not only for those who ask questions, and those answers can also be given months or years after the question has been posted. Please read this: https://meta.stackoverflow.com/q/326569 – musicamante Jan 12 '22 at 16:46
  • @musicamante Thanks for the edit. I'm a new user so I don't really know how this works. What I was trying to ask for is a way to find which cell was clicked. Let me explain my system a bit. Every time the user answers a question, an instance of the class answer is made which is then added to the list self.history. Then when I am displaying the results, I loop through that list to upload the questions and answers in the QTableWidget so therefore the row number equals the index of that object in the list. I want a way to get that row number of the clicked cell. – Mahad Hashmi Jan 15 '22 at 10:04
  • Does this answer your question? [Retrieving cell data from a selected cell in a tablewidget](https://stackoverflow.com/questions/14588479/retrieving-cell-data-from-a-selected-cell-in-a-tablewidget) – musicamante Jan 15 '22 at 10:36
  • The [`cellClicked`](https://doc.qt.io/qt-5/qtablewidget.html#cellClicked) signal returns the clicked row and column, so you just need to connect that signal to a function that accepts two arguments, and the first one will be the row. – musicamante Jan 15 '22 at 10:37

0 Answers0