J
J
Johnem2021-04-03 15:29:47
Python
Johnem, 2021-04-03 15:29:47

How to make a button in a QTableWidget cell?

Hello, I have created a table, it has 4 columns and n rows. The first column is a QSpinBox , the second is a QComboBox , the third is just a cell, and the fourth is a button ( QPushButton ). I sort of figured out how to place and set information in columns (in specific widgets) (if I did it wrong, or you can do it better, please write how),
but I had problems reading information from widgets, especially with buttons ( after all, by clicking on the button located in the i-th line, the i-th line should be deleted) . How would you recommend to implement such an idea?
The way I "set" the table is:

def set_table(self):
        table = self.te.get_table() #Просто список списков

        self.ui.table.setRowCount(len(table))
        self.ui.table.setColumnCount(4)

        self.ui.table.setColumnWidth(0, 50)
        self.ui.table.setColumnWidth(1, 150)
        self.ui.table.setColumnWidth(2, 500)
        self.ui.table.setColumnWidth(3, 20)

        for i in range(len(table)):
            self.ui.table.setRowHeight(i, 20)

            lang = QtWidgets.QComboBox()
            lang.addItems(open('langs.txt', encoding='utf-8').read().split('\n')) #В lang.txt просто название языков через \n
            lang.setCurrentIndex(table[i][1] + 1)

            id_ = QtWidgets.QSpinBox()
            id_.setValue(table[i][0])

            del_row_ = QtWidgets.QPushButton()
            del_row_.clicked.connect(self.ui.table.removeRow) #Я пытался передавать в ф-цию i, но не чего не вышло

            self.ui.table.setCellWidget(i, 0, id_)
            self.ui.table.setCellWidget(i, 1, lang)
            self.ui.table.setItem(i, 2, QTableWidgetItem(str(table[i][2])))
            self.ui.table.setCellWidget(i, 3, del_row_)
        self.ui.table.setHorizontalHeaderLabels(('ID', 'Язык', 'Текст', ''))

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Johnem, 2021-04-03
@Johnem

Partial answer:
https://stackoverflow.com/questions/54316791/pyqt5...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question