0

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("")

1 Answers1

1

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("")
Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • Thanks @Paul Cornelius!! Do you know how to move the labels to the left of the input boxes? – Ilya Josefson Nov 11 '17 at 00:07
  • You can have a separate HBoxLayout for each label/input pair. Add the label first and the input second, so the label is on the left. Then you can have a main layout manager for the whole window, and add each label/input layout to the main layout with the addLayout() function. The addLayout and addWidget functions also take flags that let you control the positioning and weight. Once you get the hang of it, it's very flexible. – Paul Cornelius Nov 11 '17 at 00:54
  • You rock! Thanks! – Ilya Josefson Nov 11 '17 at 01:47