0

I have a QTreeWidget that lists

  column 0   |  column 1   |column 2
-----------------------------------

    file1
      +      | found item  | (checkbox) new item
      +      | found item  | (checkbox) new item 
    file2
      +      | found item  | (checkbox) new item
             | found item  | (checkbox) new item 

I'm looking for a way to display all the data when the user hit's a button and if the new item is unchecked, it would skip that line.

I've found something similar to what I'm after here, but this only prints the item that's been checked/unchecked. How do I do this for all?

Adrian
  • 13
  • 2
  • I do not understand how you want the output to be, for example what would be the output for the example that you show – eyllanesc Dec 06 '18 at 23:43
  • Fair point. Essentially, I want to eventually create a dictionary where the file is the key and it contains a list of tuples, like: `{fileX: [(found_item, new_item),(found_item,new_item)], file2:[(found_item, new_item)......]}` But first, I just want to find the values for everything, so I can create the dictionary – Adrian Dec 07 '18 at 01:22
  • Should that tuple only contain items with checked? – eyllanesc Dec 07 '18 at 01:55
  • The tuple should only contain the found_item and the new_item if the new item is checked.. – Adrian Dec 07 '18 at 02:42
  • some feedback?? – eyllanesc Mar 25 '19 at 00:27

1 Answers1

0

The idea is to iterate through the items and verify the conditions:

from PyQt4 import QtCore, QtGui
import random

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.tree = QtGui.QTreeWidget(columnCount=3)
        self.tree.setHeaderLabels(["column {}".format(i) for i in range(3)])
        button = QtGui.QPushButton("Press Me", 
            clicked=self.on_clicked)
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.tree)
        lay.addWidget(button)
        self.create_data()
        self.resize(640, 480)

    def create_data(self):
        for i in range(4):
            it = QtGui.QTreeWidgetItem(["file{}".format(i)])
            self.tree.addTopLevelItem(it)
            for j in range(random.randint(2, 5)):
                vals = ["+", "found item-{}-{}".format(i, j), "new item-{}-{}".format(i, j)]
                child_it = QtGui.QTreeWidgetItem(vals)
                child_it.setCheckState(2, random.choice([QtCore.Qt.Checked, QtCore.Qt.Unchecked]))
                it.addChild(child_it)
        self.tree.expandAll()

    @QtCore.pyqtSlot()
    def on_clicked(self):
        d = dict()
        for i in range(self.tree.topLevelItemCount()):
            top_item = self.tree.topLevelItem(i)
            v = []
            for j in range(top_item.childCount()):
                child_item = top_item.child(j)
                if child_item.checkState(2) == QtCore.Qt.Checked:
                    v.append(tuple(child_item.text(i) for i in (1, 2)))
            d[top_item.text(0)] = v
        print(d)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Output:

enter image description here

{'file0': [('found item-0-2', 'new item-0-2'), ('found item-0-3', 'new item-0-3')], 'file1': [('found item-1-0', 'new item-1-0'), ('found item-1-1', 'new item-1-1'), ('found item-1-2', 'new item-1-2')], 'file2': [('found item-2-0', 'new item-2-0'), ('found item-2-1', 'new item-2-1'), ('found item-2-3', 'new item-2-3')], 'file3': [('found item-3-0', 'new item-3-0'), ('found item-3-1', 'new item-3-1')]}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241