简体   繁体   中英

How to access a QLineEdit inside a cellWidget?

I try to access the QLineEdit that are inside the cells of a QTableWidget, to be able to send a.text() value to each one of them but I don't know how to access the.text() property of the QLineEdit:

This is what I'm looking for:

for row in range(self.Table.rowCount()):
    for column in range(self.Table.columnCount()):
        widget = self.Table.item(row,column)
        if isinstance(widget,QLineEdit)
            print(widget.objectName())
            widget.setText("text")
            list.append(widget.objectName())

But it doesn't work I hope you can help me, I annex the complete code

from PyQt5.QtWidgets import QMainWindow,QApplication,QTableWidget,QTableWidgetItem,QWidgetItem,QLabel,QLineEdit,QVBoxLayout,QWidget

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.Table = QTableWidget(self)
        self.Table.resize(400,300)
        self.Table.setColumnCount(5)
        self.Table.setRowCount(5)
        self.listw = []

        for row in range(3):
            for column in range(4):
                widget = QWidget()
                layout = QVBoxLayout()
                label = QLabel()
                label.setText("row "+str(row))
                line = QLineEdit()
                line.setPlaceholderText("text")
                layout.addWidget(label)
                layout.addWidget(line)
                widget.setLayout(layout)
                self.Table.setCellWidget(row,column,widget)

        self.Table.resizeRowsToContents()

        for row in range(self.Table.rowCount()):
            for column in range(self.Table.columnCount()):
                widget = self.Table.item(row,column)
                if isinstance(widget,QLineEdit):
                    print(widget.objectName())
                    widget.setText("some text")
                    self.listw.append(widget.objectName())

app = QApplication([])
m = Main()
m.show()
m.resize(800,600)
app.exec_()

If you want to access the widget set with the setCellWidget method then you must use the cellWidget () method, but that method will get the container widget and not the QLineEdit so if you want to get the QLineEdit you could use the findChild method, but one more way Elegant is to create a custom widget that contains the widgets and that allows you to obtain each element:

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._label = QLabel()
        self._line = QLineEdit(placeholderText="text")

        lay = QVBoxLayout(self)
        lay.addWidget(self.label)
        lay.addWidget(self.line)

    @property
    def label(self):
        return self._label

    @property
    def line(self):
        return self._line


class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.Table = QTableWidget(self)
        self.Table.resize(400, 300)
        self.Table.setColumnCount(5)
        self.Table.setRowCount(5)
        self.listw = []

        for row in range(3):
            for column in range(4):
                widget = Widget()
                widget.label.setText("row " + str(row))
                self.Table.setCellWidget(row, column, widget)

        self.Table.resizeRowsToContents()

        for row in range(self.Table.rowCount()):
            for column in range(self.Table.columnCount()):
                widget = self.Table.cellWidget(row, column)
                if isinstance(widget, Widget):
                    widget.line.setText("some text")

Update:

using findChild:

for row in range(self.Table.rowCount()):
    for column in range(self.Table.columnCount()):
        widget = self.Table.cellWidget(row, column)
        if isinstance(widget, QWidget):
            lineedit = widget.findChild(QLineEdit)
            if lineedit is not None:
                lineedit.setText("some text")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM