Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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?
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]( image url)
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.
That's always the same layout with the same line edits at the same place ?
Bauke last edited by Bauke
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.
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"
Bauke last edited by Bauke
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() self.setLayout(grid) # 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) + "-" + str(positions)) cell = name = "cel" + str(positions) + "-" + str(positions) if cells == "": continue elif cells == 'le': cell = QLineEdit() cell.setAlignment(Qt.AlignRight) cell.textChanged.connect(lambda x, pos=positions, name=name: self.my_custom_fn(x, pos, name)) if positions == 3 or positions == 4: cell.setDisabled(1) else: cell = QLabel(cells) if positions == 0: cell.setAlignment(Qt.AlignCenter) grid.addWidget(cell, *positions) widget = QWidget() widget.setLayout(grid) self.setCentralWidget(widget) 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() ui.show() sys.exit(app.exec_())
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.
Thanks, QHash is very new to me. Will look into that.