Ishaan
Ishaan

Reputation: 747

PyQt QVBoxLayout adding elements at bottom

I'm a beginner learning Python/PyQt. I'm trying to add QLabel and QLineEdits to a QVBoxLayout however all the widgets get added at the bottom of the screen.

I've tried using vbox.setAlignment(Qt.AlignTop) but that does not seem to work either.

Any pointers are appreciated!

main.py

import sys
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)

class m_Window(QWidget):

    def __init__(self, scale = 1):
        QWidget.__init__(self)
        self.initUI(scale)

    def initUI(self, scale):
        #initialize window sizes
        win_width = app.desktop().screenGeometry().width() * scale
        win_height = app.desktop().screenGeometry().height() * scale
        #init widgets
        project_name_lbl = QLabel('<b>Project Name</b>', self)
        project_name_inp = QLineEdit(self)
        frameworks = ['Skeleton CSS','Bootstrap','UIKit','Foundation','JQuery']
        framework_cmbx = QComboBox(self)
        framework_cmbx.addItems(frameworks)
        #add items to layout
        vbox = QVBoxLayout()
        vbox.addStretch()
        vbox.addWidget(project_name_lbl)
        vbox.addWidget(project_name_inp)
        vbox.addWidget(framework_cmbx)
        #self settings
        self.setLayout(vbox)
        self.setWindowTitle('Website Template Maker')
        self.setMinimumSize(QSize(win_width, win_height))

    def run(self):
        self.show()
        sys.exit(app.exec_())

m_Window(.5).run()

pic:

<code>QVBoxLayout()</code> adding widgets to the bottom

Upvotes: 1

Views: 3193

Answers (1)

jonspaceharper
jonspaceharper

Reputation: 4347

Move the line

vbox.addStretch()

To after you've added your widgets:

vbox = QVBoxLayout()
vbox.addWidget(project_name_lbl)
vbox.addWidget(project_name_inp)
vbox.addWidget(framework_cmbx)
vbox.addStretch()

This will make the layout push your widgets up instead of down.

Upvotes: 3

Related Questions