Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. QLineEdit and position within QgridLayout
Forum Updated to NodeBB v4.3 + New Features

QLineEdit and position within QgridLayout

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
11 Posts 2 Posters 4.0k Views 1 Watching
  • 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 Offline
    B Offline
    Bauke
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • B Offline
        B Offline
        Bauke
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          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
          0
          • B Offline
            B Offline
            Bauke
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • SGaistS SGaist

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

                B Offline
                B Offline
                Bauke
                wrote on last edited by Bauke
                #7

                @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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0
                  • B Offline
                    B Offline
                    Bauke
                    wrote on last edited by Bauke
                    #9

                    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
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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
                      0
                      • SGaistS SGaist

                        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.

                        B Offline
                        B Offline
                        Bauke
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved