0

So I have a list of QTableWidgetItems via pyqt and I want to check if the rows of these elements are all in succession of each other, otherwise I would need to error-handle. I tried:

for i in list(selectedItems):
    if ((i+1).row - i.row) != 1:
        print("Selected Items are not succeeding each other")

And I tried to use itertools.cycle with no avail. Any leads?

Edit: Ok, so here is a bit more info. My first proposed solution will get me the error Message:

TypeError: unsupported operand type(s) for +: 'QTableWidgetItem' and 'int'

And when using a cycle I could no find a possibility to reach the 'QTableWidgetItem.row()' method. What I want is a simple function that will check if the difference in rows stays at 1 for all the elements I have currently selected, otherwise it means there are items selected which have a "gap" between them which my code currently cannot handle. When that case happens I will simply disable the grouping function until the selection falls within my criteria again.

  • Perhaps something like `lst = list(selectedItems); for a, b in zip(lst, lst[1:]): print(a, b)`? – Wondercricket Feb 23 '22 at 15:13
  • What's wrong with what you have? What should happen when two elements are not in succession of each other? What's your question exactly? – martineau Feb 23 '22 at 15:18

1 Answers1

-1
# iterate through the list
for i in SelectedItems:
     # Use an if statement to check for difference between current
     # and previous item is 1
     if selectedItems[i-1] - i != 1:
        print("Selected Items are not succeeding each other")
        break  # stop if this happens in any two items

There are a bunch of things which will throw this off. But without access to the list/data-types I don't think I can put statements to handle them.

Sid
  • 3,749
  • 7
  • 29
  • 62
  • While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Feb 23 '22 at 15:17
  • 2
    what happens when you reach the last element of the list? – bananenpampe Feb 23 '22 at 15:17
  • 1
    @bananenpampe it is checking with the previous item. – Sid Feb 23 '22 at 15:22
  • `SelectedItems` does not match `selectedItems`, and Python for loops do not give indices by default. – Karl Knechtel Feb 23 '22 at 16:23
  • This is basically what I have tried as well but unfortunately I am handling objects and not integers and therefore cannot use simple arithmetic functions here. – InFumumVerti Feb 23 '22 at 16:24