0

I have the following code where I use python and Qt to make a ui where I can see the data that is in a table. Works well, but I am trying to insert a button into the last column, which is just a column that I have created as a "placeholder" for the button. (I don't use the data in that column)

But my problem is that I get the button inserted, but only into every second line. The it goes on inserting every second line even when the rows from the table have stopped.

col = self.dbu.GetColumn_names()                                    
        table = self.dbu.GetTable()                                         

        for c in range (len(col)):                                          
            self.treeWidget.headerItem().setText(c, col[c])                 

        self.treeWidget.clear()                                             
        cw = QTreeWidget()
        cw.setColumnCount(len(col))
        for item in range(len(table)):                                      
            QTreeWidgetItem(self.treeWidget)                                
            for value in range(len(table[item])):                           
                if value != 4:
                    self.treeWidget.topLevelItem(item).setText(value, str(table[item][value]))          
                else:
                    i = QTreeWidgetItem(self.treeWidget)
                    b = QPushButton("push me " + str(value), cw)
                    self.treeWidget.setItemWidget(i, [4][0], b)

I copied and pasted the code from everywhere and have tried a hundred things, and the best I got was to get a button on every line, except the first line.

And most of the literature discussing this problem is in C#/C++ and I have no idea how to convert the code to python and try it.

Please show me with code, if possible, where I am going wrong, or if I must use a different class.

Regards

Alfa Bravo
  • 1,961
  • 2
  • 25
  • 45

1 Answers1

1

i = QTreeWidgetItem(self.treeWidget) appends a new row, so you appended one for the data, and another for the widget, giving you alternating rows. Also I'm not sure what

cw = QTreeWidget()
cw.setColumnCount(len(col))

is. It seems to be a fresh QTreeWidget that you are just discarding.

And [4][0] is the weirdest way of writing 4 that I have seen.

Anyway, here is your main for loop fixed:

for row in range(len(table)):
    # appends new row to self.treeWidget                                    
    rowItem = QTreeWidgetItem(self.treeWidget)

    for column in range(len(table[item])):                           
        if column != 4:
            rowItem.setText(column, str(table[row][column]))          
        else:
            button = QPushButton("push me " + str(value), self.treeWidget)
            self.treeWidget.setItemWidget(rowItem, 4, button)
Joseph Ireland
  • 2,465
  • 13
  • 21
  • Thank you Joseph, I have actually just came back right now to right that I have found my mistake, which was exactly what you mentioned. Also the weird way of writing "4" was just some left over artifacts that I had from testing a hundred different things. You should have seen my code before I posted it, allot of print statements and grayed out description lines. :) Thanks again for the help and the code! – Alfa Bravo Dec 02 '16 at 11:58
  • Would you also maybe be able to tell me how I would be able to connect that button to an action. What I would be using is the following code " button.clicked.connect(self.select_file)" but I am not sure if I can put it right under the code, or if I would have to put it at the top where I declare all my variables. I tried to declare the button at the top with self.button as well, but when I ran the code it just crashed python every time. – Alfa Bravo Dec 02 '16 at 12:27
  • 1
    To do something with the button, I'd go for something like `button.clicked.connect(lambda rowItem=rowItem: self.button_clicked(rowItem))`, and implement that button_clicked function to do what you want,given that rowItem. Remeber that there are loads of buttons and you need to tell them apart. – Joseph Ireland Dec 02 '16 at 14:10
  • Excellent answer and well said so that I can understand it completely. Thanks for the help!!! – Alfa Bravo Dec 02 '16 at 16:53
  • I can not work out the "button.clicked.connect(lambda rowItem=rowItem: self.button_clicked(rowItem))" very well. I have made the self.button_clicked a function that prints the rowItem that it gives through, which is just False the whole time... – Alfa Bravo Dec 03 '16 at 11:01
  • 1
    oops you are right, [clicked][http://doc.qt.io/qt-5/qabstractbutton.html#clicked] has a parameter - `checked`. Try `(lambda checked=False, rowItem=rowItem: self.button_clicked(rowItem))` – Joseph Ireland Dec 04 '16 at 01:41