Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QLineEdit and position within QgridLayout

    Installation and Deployment
    2
    11
    3100
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      Bauke last edited by

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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        Why do you need that information ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • B
          Bauke last edited by

          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)

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by SGaist

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • B
              Bauke last edited by

              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,
              Bauke

              PS: Had to wait before I could post again.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply Reply Quote 0
                • B
                  Bauke @SGaist last edited by Bauke

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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"

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • B
                      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[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_())
                      
                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        B 1 Reply Last reply Reply Quote 0
                        • B
                          Bauke @SGaist last edited by

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

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post