Issue
I have this window where the mainlayout is a QVBoxlayout
with nested QHBoxlayout
On top a have set two labels, for some reasons
pressen_label = qtw.QLabel("Choose A")
label is changing its position after the window is moved or resized with the mouse.
before moving/change size of the window with mouse
after moving the window
How can I lock the position of the label so its not moving?
pressen_label = qtw.QLabel("Choose A")
quality_label = qtw.QLabel("Choose B")
hblayout_labelcombo = qtw.QHBoxLayout()
hblayout_labelcombo.addWidget(pressen_label)
hblayout_labelcombo.addStretch(1)
hblayout_labelcombo.addWidget(quality_label)
hblayout_labelcombo.addStretch(10)
hblayout_labelcombo.setAlignment(qtc.Qt.AlignTop)
full code
#!/usr/bin/env python
"""
template interface
"""
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
class SwellingwindowParent(qtw.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# your code will go here
# position
qtRectangle = self.frameGeometry()
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveTop(100)
# size
self.resize(1400, 910)
# frame title
self.setWindowTitle("Parent Window")
# widgets
# combobox label and layout
pressen_label = qtw.QLabel("Choose A")
quality_label = qtw.QLabel("Choose B")
hblayout_labelcombo = qtw.QHBoxLayout()
hblayout_labelcombo.addWidget(pressen_label)
hblayout_labelcombo.addStretch(1)
hblayout_labelcombo.addWidget(quality_label)
hblayout_labelcombo.addStretch(10)
hblayout_labelcombo.setAlignment(qtc.Qt.AlignTop)
# combobox widget and layout
comobox_selectpressen = qtw.QComboBox()
comobox_selectpressen.setFixedWidth(130)
comobox_selectquality = qtw.QComboBox()
comobox_selectquality.setFixedWidth(130)
hblayout_widgetcombobox = qtw.QHBoxLayout()
hblayout_widgetcombobox.addWidget(comobox_selectpressen)
hblayout_widgetcombobox.addStretch(1)
hblayout_widgetcombobox.addWidget(comobox_selectquality)
hblayout_widgetcombobox.addStretch(30)
hblayout_widgetcombobox.setAlignment(qtc.Qt.AlignTop)
# mss
heading_messschieber = qtw.QLabel("Mss stuff")
button_messschiber = qtw.QPushButton("mss start")
# M
messchiber_heading = qtw.QLabel("want to start msss ?")
hblayout_labelmsslayout = qtw.QHBoxLayout()
hblayout_labelmsslayout.addWidget(messchiber_heading)
hblayout_labelmsslayout.addStretch(2)
hblayout_labelmsslayout.addWidget(quality_label)
hblayout_labelmsslayout.addStretch(30)
msss_messuungstartenbutton = qtw.QPushButton("msss start")
hblayout_mssbuttonlayout = qtw.QHBoxLayout()
hblayout_mssbuttonlayout.addWidget(msss_messuungstartenbutton)
hblayout_mssbuttonlayout.addStretch(2)
hblayout_mssbuttonlayout.addWidget(quality_label)
hblayout_mssbuttonlayout.addStretch(30)
# spacer set custum vertical space
verticalSpacer1 = qtw.QSpacerItem(10, 10, qtw.QSizePolicy.Minimum, vPolicy=qtw.QSizePolicy.Fixed)
verticalSpacer2 = qtw.QSpacerItem(10, 5, qtw.QSizePolicy.Minimum, vPolicy=qtw.QSizePolicy.Fixed)
verticalSpacer3 = qtw.QSpacerItem(10, 50, qtw.QSizePolicy.Minimum, vPolicy=qtw.QSizePolicy.Fixed)
verticalSpacer4 = qtw.QSpacerItem(3, 1, vPolicy=qtw.QSizePolicy.Fixed)
# main layout
mainlayout = qtw.QVBoxLayout()
mainlayout.setAlignment(qtc.Qt.AlignTop) # needed for custom spacers
mainlayout.addSpacerItem(verticalSpacer1)
mainlayout.addLayout(hblayout_labelcombo)
mainlayout.addSpacerItem(verticalSpacer2)
mainlayout.addLayout(hblayout_widgetcombobox)
mainlayout.addSpacerItem(verticalSpacer3)
mainlayout.addLayout(hblayout_labelmsslayout)
mainlayout.addLayout(hblayout_mssbuttonlayout)
# mainlayout.addSpacerItem(verticalSpacer1)
self.setLayout(mainlayout)
# your code ends here
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = SwellingwindowParent()
sys.exit(app.exec_())
Solution
Describing your mistakes can be complicated (and even a waste of time) for example quality_label
is added to 3 layouts: hblayout_labelcombo.addWidget(quality_label)
, hblayout_labelmsslayout.addWidget(quality_label)
and hblayout_mssbuttonlayout.addWidget(quality_label)
which is illogical.
So my answer focuses on implementing the solution, in this case it is enough to use a QGridLayout and establish the appropriate stretching factors.
import sys
from PyQt5 import QtWidgets as qtw
class Widget(qtw.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.choose_a_label = qtw.QLabel(self.tr("Choose A"))
self.choose_b_label = qtw.QLabel(self.tr("Choose B"))
self.choose_a_combo = qtw.QComboBox()
self.choose_a_combo.setFixedWidth(130)
self.choose_b_combo = qtw.QComboBox()
self.choose_b_combo.setFixedWidth(130)
self.label_question = qtw.QLabel(self.tr("want to start msss ?"))
self.button = qtw.QPushButton(self.tr("mss start"))
lay = qtw.QGridLayout(self)
lay.addWidget(self.choose_a_label, 0, 0)
lay.addWidget(self.choose_b_label, 0, 1)
lay.addWidget(self.choose_a_combo, 1, 0)
lay.addWidget(self.choose_b_combo, 1, 1)
lay.addWidget(self.label_question, 2, 0)
lay.addWidget(self.button, 3, 0)
lay.setColumnStretch(3, 1)
lay.setRowStretch(4, 1)
self.resize(640, 480)
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.