0

code:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

def main():
    app = QApplication(sys.argv)
    main = QMainWindow()
    main.setWindowTitle("First GUI")
    form_widget = QWidget()
    form_widget.layout = QFormLayout()
    form_widget.layout.addRow(QLabel("city 1"), QLabel("delhi"))
    form_widget.layout.addRow(QLabel("city 2"), QLabel("chennai"))
    main.setCentralWidget(form_widget)

    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Above code opened an empty window with "First GUI" as title.

But rows were not added to that window. Why ? How to correct this ?

3
  • May be you need to set size to your layout? Commented Aug 6, 2014 at 11:34
  • 1
    Style nitpick: addRow doesn't need the explicit instantiation of QLabel for the first argument. You can invoke it simply as form_widget.layout.addRow("city 1", QLabel("delhi")). Commented Aug 7, 2014 at 1:12
  • @folibis The whole point of layouts is that you don't need to explicitly deal with their sizing. Commented Aug 7, 2014 at 1:12

1 Answer 1

3

You need to use setLayout() to assign the layout to the widget.

form_widget = QWidget()
layout = QFormLayout()
layout.addRow(QLabel("city 1"), QLabel("delhi"))
layout.addRow(QLabel("city 2"), QLabel("chennai"))
form_widget.setLayout(layout)
Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, pass the form_widget as argument to QFormLayout. It saves one line of code - the setLayout call is not needed anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.