Search code examples
pythonqlineedit

Python: Having multiple QLineEdit boxes (pyqt4)


I would like to have 2 input text boxes next to each other.

I've attempted to copy/past the code for one text box but that has not yielded the proper result. Only one text box shows.

If possible I'd like to get the labels to sit in-line with the text boxes as well.

class UI_central(QtGui.QDialog):

def __init__(self, parent=None):
    super(UI_central, self).__init__(parent)

    """
    Input box for stock name
    """
    label1 = QtGui.QLabel('Stock', self)
    label1.move(50, 0)

    self.line_edit = QtGui.QLineEdit()
    self.line_edit.setText("Stock name")

    hbox = QtGui.QHBoxLayout()
    hbox.addWidget(self.line_edit)
    self.setLayout(hbox)

    """
    Input box for stock amount
    """
    label2 = QtGui.QLabel('How Many?', self)
    label2.move(100, 0)

    self.line_edit2 = QtGui.QLineEdit()
    self.line_edit2.setText("Stock amount")

    hbox2 = QtGui.QHBoxLayout()
    hbox2.addWidget(self.line_edit2)
    self.setLayout(hbox2)        

    """
    Push buttons
    """
    submit_button = QtGui.QPushButton("Submit")
    clear_button = QtGui.QPushButton("Clear")

    hbox.addWidget(submit_button)
    hbox.addWidget(clear_button)

    self.connect(submit_button, QtCore.SIGNAL("clicked()"),
                 self.submit)

    self.connect(clear_button, QtCore.SIGNAL("clicked()"),
                 self.clear)
    return

def submit(self):
    str = self.line_edit.text()
    # check str before doing anything with it!
    print(str)

def clear(self):
    print ("cleared")
    self.line_edit.setText("")

Solution

  • Your code creates two LineEdits but there's a layout issue. There can be only one layout manager per window. Your second call to setLayout(self) removes the first layout manager.

    BTW you can nest one layout manager inside another (the BoxLayout managers have a function addLayout for this purpose).

    Also I don't know what is going to happen when you mix calls to move with a layout manager. I have always let the layout managers take charge of positioning all the children.

    I removed the second layout and now both LineEdits should appear.

    class UI_central(QtGui.QDialog):
        def __init__(self, parent=None):
            super(UI_central, self).__init__(parent)
    
            """
            Input box for stock name
            """
            label1 = QtGui.QLabel('Stock', self)
            label1.move(50, 0)
    
            self.line_edit = QtGui.QLineEdit()
            self.line_edit.setText("Stock name")
    
            hbox = QtGui.QHBoxLayout()
            hbox.addWidget(self.line_edit)
            self.setLayout(hbox)
    
            """
            Input box for stock amount
            """
            label2 = QtGui.QLabel('How Many?', self)
            label2.move(100, 0)
    
            self.line_edit2 = QtGui.QLineEdit()
            self.line_edit2.setText("Stock amount")
    
            hbox.addWidget(self.line_edit2)
    
            """
            Push buttons
            """
            submit_button = QtGui.QPushButton("Submit")
            clear_button = QtGui.QPushButton("Clear")
    
            hbox.addWidget(submit_button)
            hbox.addWidget(clear_button)
    
            self.connect(submit_button, QtCore.SIGNAL("clicked()"),
                     self.submit)
    
            self.connect(clear_button, QtCore.SIGNAL("clicked()"),
                     self.clear)
    
    def submit(self):
        str = self.line_edit.text()
        # check str before doing anything with it!
        print(str)
    
    def clear(self):
        print ("cleared")
        self.line_edit.setText("")