Important: Please read the Qt Code of Conduct -

QLineEdit and position within QgridLayout

  • I have dynamically created lineedits(QLineEdit) in gridLayout in pyQT5. How can I get the position (index) of the edited lineEdit in the layout?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why do you need that information ?

  • Based on the position I have to calculate some other lineEdits. Its a kind of very little spreadsheet prog. Below a picture (I hope)

    ![alt text](0_1517347978906_Schermafdruk_2018-01-30_22-30-39.png image url)

  • Lifetime Qt Champion

    Then why not use a QTableWidget ?

    In any case, rather than relying on the position of your line edits, keep a reference to them for doing your manipulation. That will keep your code cleaner and more readable.

  • Will try that! I am very new to QT5 and just trying to get some results.
    Thanks for your answer!

    And how can I keep a ref. to my line edits? Its dynamicly build and all the line edits are called "cell"


    PS: Had to wait before I could post again.

  • Lifetime Qt Champion

    That's always the same layout with the same line edits at the same place ?

  • @SGaist
    Yes it is. I now have the reference of the sending cell with:
    cell.textChanged.connect(lambda x, pos=positions: self.my_custom_fn(x, pos))
    Or is there a better way?
    It gives me a tuple with row, col. Ex. row 2 and col 2.
    Now I do some calculation and want to put the result in cell(row 2, col 3) (disabled) with cell.setText().
    But how do I reach the cell at row 2 col 3?

    I will look at the TableWdget, but this should work too.

  • Lifetime Qt Champion

    Then you can use something like:

    self.aantal_btc = QtWidgets.QLineEdit()
    grid_layout.addWidget(self.aantal_btc, 0, 1);

    Then it's easy to update the line edit for "Aantal BTC"

  • But that will be the end of the dynamic part, won't it?

    Here is the code sofar:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    import sys
    class MainWindow(QMainWindow):
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
            self.setWindowTitle("Waarde Coins")
            grid = QGridLayout()
            # create matrix: "le" wordt een LineEdit, "" wordt overgeslagen en de rest wordt Label
            cells = ["", "<b>Totaal</b>", "<b>BTC</b>", "<b>ETH</b>", "<b>LTC</b>",
                     "Aantal:", "", "le", "le", "le",
                     "Aantal DGB:", "", "le", "le", "le",
                     "Waarde USD:", "le", "le", "le", "le",
                     "<b>Waarde EURO:</b>", "le", "le", "le", "le",
                     "Investering in EURO:", "le", "", "", "", ]
            positions = [(row, col) for row in range(6) for col in range(5)]
            for positions, cells in zip(positions, cells):
                # Create the labels and lineEdits from  matrix
                # print("Positions (row,col):", "cel" + str(positions[0]) + "-" + str(positions[1]))
                cell = name = "cel" + str(positions[0]) + "-" + str(positions[1])
                if cells == "":
                elif cells == 'le':
                    cell = QLineEdit()
                    cell.textChanged.connect(lambda x, pos=positions, name=name: self.my_custom_fn(x, pos, name))
                    if positions[0] == 3 or positions[0] == 4:
                    cell = QLabel(cells)
                    if positions[0] == 0:
                grid.addWidget(cell, *positions)
            widget = QWidget()
        def my_custom_fn(self, a, pos, name):
            print("Invoer : ", a)
            print("Pos    : ", pos)
            print("Name   : ", name)
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        ui = MainWindow()

  • Lifetime Qt Champion

    Rather than creating everything in your MainWindow class, create one widget that is your "spread sheet". You use a QHash to associate your cells with some key to return them quickly rather than having to scan the grid layout until you find the one you want.

  • @SGaist
    Thanks, QHash is very new to me. Will look into that.

Log in to reply