[GIS] return the text entered in a QTableWidget

pyqgispyqtqt-designer

I am creating a plugin in Qgis and I need to enter values to a QTableWidget through the interface, but I cannot find a function that returns them to me.
I do not do it with a LineEdit because I need the number of spaces to fill out equal to those of a listWidget.
try to do it with QTableWidget itemAt but the return is a QTableWidgetItem.
the code is the following:

def Input_shape (self):
    filefolder= QFileDialog.getExistingDirectory(self.dlg, "", "/desktop") 
    filename = glob.glob(filefolder+"/"+"\\*.shp") 
    self.dlg.comboBox_2.clear() 
    self.dlg.listWidget.clear()
    self.dlg.comboBox_2.addItems([filefolder])
    self.dlg.listWidget.addItems(filename)
    self.dlg.tableWidget.clear()
    self.dlg.tableWidget.setRowCount(len(filename))
    self.dlg.tableWidget.setColumnCount(2)

def run(self):
    if self.first_start == True:
        self.first_start = False
    self.dlg = SubsueloDialog()
    self.dlg.boton_entrada.clicked.connect(self.Input_shape)
    filefolder= self.dlg.comboBox_2.currentText() 
    filenames = glob.glob(filefolder+"/"+"\\*.shp") 
    limites= self.dlg.tableWidget.item(1,1)

The interface looks like this:

enter image description here

Best Answer

In following code, you have an example where you can get all values typed in a QTableWidget object by using a function connected to a QPushButton.

from PyQt5.QtCore import Qt

class Dlg(QDialog):

    def __init__(self):
        QDialog.__init__(self)
        self.layout = QGridLayout(self)
        self.label1 = QLabel('FERTILIZACION SUBSUELO')
        self.btn = QPushButton('RUN')
        self.btn.setFixedWidth(100)

        nb_row = 5
        nb_col = 2

        #creating empty table
        data = [ [] for i in range(nb_row) ]

        for i in range(nb_row):
            for j in range(nb_col):
                data[i].append('')

        self.table1 = QTableWidget()

        self.table1.setRowCount(nb_row)
        self.table1.setColumnCount(nb_col)
        self.table1.setHorizontalHeaderLabels(['LIMITE 1', 'LIMITE 2'])

        for row in range (nb_row):
            for col in range(nb_col):
                item = QTableWidgetItem(str(data[row][col]))
                self.table1.setItem(row, col, item)

        self.layout.addWidget(self.label1, 0, 0)
        self.layout.addWidget(self.table1, 1, 0)
        self.layout.addWidget(self.btn, 2, 0)

        self.btn.clicked.connect(self.print_table_values)

    def print_table_values(self, data):

        nb_row = 5
        nb_col = 2

        for row in range (nb_row):
            for col in range(nb_col):
                print(row, col, self.table1.item(row, col).text())

w = Dlg()
w.resize(350,300)
w.setWindowTitle('fertilizacion_abono')
w.setWindowFlags(Qt.WindowStaysOnTopHint)
w.show()

After running the code, I got a Dialog similar to yours where I typed arbitrary values for each QTableWidgetItem objects.

enter image description here

After clicking RUN button, print_table_values function printed in Python Console all values as expected.

enter image description here