0

I have a 3 QLineEdit and a QTableWidget in a Qtform. I want to insert 3 QLineEdit data entries as a row in the table using pyqt in python.

Could someone, please, point me in the direction how can I do this?

Thanks in advance.

NirMH
  • 4,769
  • 3
  • 44
  • 69

1 Answers1

0

Here the code, 3 QLineEdit and a QTableWidget are in QtWidget, not in QtForm, but it answers your question. If you have any questions feel free to ask :

import sys
from PyQt4 import QtGui


class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.table = QtGui.QTableWidget(self)
        self.table.setRowCount(0)
        self.table.setColumnCount(3)

        self.textInput1 = QtGui.QLineEdit()
        self.textInput2 = QtGui.QLineEdit()
        self.textInput3 = QtGui.QLineEdit()

        self.button = QtGui.QPushButton("insert into table")
        self.button.clicked.connect(self.populateTable)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.table)
        layout.addWidget(self.textInput1)
        layout.addWidget(self.textInput2)
        layout.addWidget(self.textInput3)
        layout.addWidget(self.button)

    def populateTable(self):

        text1 = self.textInput1.text()
        text2 = self.textInput2.text()
        text3 = self.textInput3.text()

  #EDIT - in textInput1 I've entered 152.123456
        print text1, type(text1) #152.123456 <class 'PyQt4.QtCore.QString'>
        floatToUse = float(text1) # if you need float convert QString to float like this
        print floatToUse , type(floatToUse) #152.123456 <type 'float'>
        # you can do here something with float and then convert it back to string when you're done, so you can put it in table using setItem
        backToString= "%.4f" % floatToUse # convert your float back to string so you can write it to table
        print backToString, type(backToString) #152.1235 <type 'str'>


        row = self.table.rowCount()
        self.table.insertRow(row)

        self.table.setItem(row, 0, QtGui.QTableWidgetItem(text1))
        self.table.setItem(row, 1, QtGui.QTableWidgetItem(text2))
        self.table.setItem(row, 2, QtGui.QTableWidgetItem(text3))


if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 300, 500, 500)
    window.show()
    sys.exit(app.exec_())
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • Thank you so much Mr. Aleksander. But my imput2 and imput3 is a float, tha function setItem works only with string type. – Abdelmalek May 14 '14 at 11:22
  • `stringRepresentationOfFloat = "%.4f" % yourFloatInput` , where there is 4 digits after the floating point. More about conversion float to string you can find [here](http://stackoverflow.com/questions/15263597/python-convert-floating-point-number-to-certain-precision-then-copy-to-string) – Aleksandar May 14 '14 at 19:24
  • do you convert `text1` to float? since type of `text1` is `QString`... `self.textInput1.text()` returns `QString` even if you entered for example `152.177` ... anyway, doesn't matter what type you have, there is always a way to convert it to string so you can use `setItem` – Aleksandar May 15 '14 at 08:41
  • In fact the textInput1 and textInput2 is a x,y coordinates that I will use in ather function (create a new polygon) so I should returns self.textInput1.text() and self.textInput2.text() as floats. – Abdelmalek May 15 '14 at 09:46
  • I try x = float(self.textInput1.text()) but I can't use setItem to insert in table. Now, I think that I should use self.textInput1.text() and self.textInput2.text() as string, after, in the function of creating the polygon, I use the transformation to float. I don't sure if that will works or no. – Abdelmalek May 15 '14 at 09:54
  • exactly, I've added code so you can see how to convert from qstring to float, do with float what you want, convert it back to string and write it to your table. Take a look at EDIT – Aleksandar May 15 '14 at 15:23
  • Great idea, thanks Aleksandar. I have an other question please: I have a layer with polygonal features, I would like to create a buffer around a selected feature and search the features within the buffer that satisfy others conditions. the buffer value well be in meters and specify by user. – Abdelmalek May 16 '14 at 14:05