maurobio
maurobio

Reputation: 1587

PyQt: QLineEdit with attached label

This is probably a very silly question, but anyway I am struck by it :-(

I have a window with two side-by-side lists and a line edit control with a label below them (see the attached image). What I want is to have the line edit field placed just after the corresponding label and not below the second list.

enter image description here

Here is my test code:

import sys
from PyQt4 import Qt, QtCore, QtGui

class MainWindow(QtGui.QWidget):
     def __init__(self):
        QtGui.QWidget.__init__(self)

        centralLayout = QtGui.QGridLayout()
        centralLayout.setSpacing(2)
        treelist1 = QtGui.QTreeWidget()
        treelist2 = QtGui.QTreeWidget()
        treelist1.setColumnCount(1)
        treelist1.setHeaderLabels(["List 1"])
        treelist2.setColumnCount(1)
        treelist2.setHeaderLabels(["List 2"])
        label = QtGui.QLabel("Line:")
        linedit = QtGui.QLineEdit()
        centralLayout.setRowStretch(0, 8)
        centralLayout.setRowStretch(1, 1)
        centralLayout.addWidget(treelist1, 0, 0)
        centralLayout.addWidget(treelist2, 0, 1)
        centralLayout.addWidget(label, 1, 0)
        centralLayout.addWidget(linedit, 1, 1)
        self.setLayout(centralLayout)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

Thanks in advance for any assistance you can provide!

Upvotes: 3

Views: 14848

Answers (2)

maurobio
maurobio

Reputation: 1587

Here is the answer to my question, based on the suggestion by ypnos:

import sys
from PyQt4 import Qt, QtCore, QtGui

class MainWindow(QtGui.QWidget):
     def __init__(self):
        QtGui.QWidget.__init__(self)

        verticalLayout = QtGui.QVBoxLayout()
        verticalLayout.setSpacing(2)
        horizontalLayout1 = QtGui.QHBoxLayout()
        horizontalLayout2 = QtGui.QHBoxLayout()
        treelist1 = QtGui.QTreeWidget()
        treelist2 = QtGui.QTreeWidget()
        treelist1.setColumnCount(1)
        treelist1.setHeaderLabels(["List 1"])
        treelist2.setColumnCount(1)
        treelist2.setHeaderLabels(["List 2"])
        label = QtGui.QLabel("Line:")
        linedit = QtGui.QLineEdit()
        horizontalLayout1.addWidget(treelist1)
        horizontalLayout1.addWidget(treelist2)
        horizontalLayout2.addWidget(label)
        horizontalLayout2.addWidget(linedit)
        verticalLayout.addLayout(horizontalLayout1)
        verticalLayout.addLayout(horizontalLayout2)
        self.setLayout(verticalLayout)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

enter image description here

Upvotes: 4

ypnos
ypnos

Reputation: 52397

What you need here is:

  • Set the widget up in a vertical layout, not grid layout
  • In that vertical layout, add two horizontal layouts
  • Add the two tree widgets to the first horizontal layout
  • Add your label and lineedit to the second horizontal layout

Upvotes: 4

Related Questions