I have an object of type QTableWidget in which I need to add a certain number of times to the new row, to the first column the row number, and to the second new object of the type QTextEditor. But the number will be set by the user. And in the future it will be necessary to read the value of each of the QTextEditor. I tried to implement it by brute force, but the object does not appear in the table. Here is a piece of code:

for _ in range(self.output_text.count(replacement_symbol)): input_replacement = QTextEdit(self.table_of_replacements) input_replacement.resize(195, 25) self.list_of_replacement.append(input_replacement) self.table_of_replacements.setCellWidget(_, 0, input_replacement) 

    1 answer 1

    I didn’t quite understand how many will be specified by the user, but добавить QTextEdit в QTableWidget like this:

     import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * class MyWin(QMainWindow): def __init__(self, parent=None): super().__init__() centralWidget= QWidget() self.setCentralWidget(centralWidget) self.textEdit = QTextEdit() self.tableWidget = QTableWidget(0, 2) self.tableWidget.setHorizontalHeaderLabels(["номер \nстроки", "Text"]) self.tableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents) self.button = QPushButton("button") self.button.clicked.connect(self.onButton) lay = QGridLayout(centralWidget) lay.addWidget(self.textEdit, 1, 0) lay.addWidget(self.tableWidget, 2, 0) lay.addWidget(self.button, 3, 0) def onButton(self): listText = self.textEdit.toPlainText().split('\n') row = self.tableWidget.rowCount() for r, _text in enumerate(listText): it_nn = QTableWidgetItem("{:>10}".format(r+1)) it_text = QTableWidgetItem(_text) self.tableWidget.insertRow(self.tableWidget.rowCount()) for c, item in enumerate((it_nn, it_text)): self.tableWidget.setItem(row+r, c, item) if __name__ == '__main__': app = QApplication(sys.argv) main = MyWin() main.show() sys.exit(app.exec_()) 

    enter image description here