QLineEdit and position within QgridLayout
-
Hi and welcome to devnet,
Why do you need that information ?
-
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"
Regards,
BaukePS: Had to wait before I could post again.
-
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.
-
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() 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[0]) + "-" + str(positions[1])) cell = name = "cel" + str(positions[0]) + "-" + str(positions[1]) 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[0] == 3 or positions[0] == 4: cell.setDisabled(1) else: cell = QLabel(cells) if positions[0] == 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.
-
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.