Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQT5 - align left
#1
Hi, I am stuck on very stupid thing.
I need align items left and width must be according text length and for line_edit fixed length.
Thank for any advice.

IMAGE


 self.label = QLabel("Low-pass (Hz): ", self)
        self.label.setFont(QFont('Arial', 10))
        self.label.setAlignment(Qt.AlignLeft)

        self.line_edit = QLineEdit("10", self)
        self.line_edit.setFont(QFont('Arial', 10))
        self.line_edit.setAlignment(Qt.AlignLeft)
        self.line_edit.setFixedWidth(120)
   
        self.label1 = QLabel("High-pass (Hz): ", self)
        self.label1.setFont(QFont('Arial', 10))
        self.label1.setAlignment(Qt.AlignLeft)
   
        self.line_edit1 = QLineEdit("10", self)
        self.line_edit1.setFont(QFont('Arial', 10))
        self.line_edit1.setAlignment(Qt.AlignLeft)
        self.line_edit1.setFixedWidth(120)
   
 
        self.combo_box = QComboBox(self)
        self.combo_box.setFont(QFont('Arial', 10))
         
        geek_list = ["Hanning", "Hamming", "Flat", "None"]
        self.combo_box.setEditable(False)
        self.combo_box.addItems(geek_list)
        self.combo_box.setFixedWidth(120)
        
        
        buttonbox3.addWidget(self.label)
        buttonbox3.addWidget(self.line_edit)
        buttonbox3.addWidget(self.label1)
        buttonbox3.addWidget(self.line_edit1)
        buttonbox3.addWidget(self.combo_box)
Reply
#2
What type of container is buttonbox3?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
buttonbox3 = QHBoxLayout()

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
        self.setCentralWidget(self.canvas)
        self.showMaximized() 
        toolbar = NavigationToolbar(self.canvas, self)
        layout = QtWidgets.QVBoxLayout()
      
        buttonbox = QHBoxLayout()  
        buttonbox2 = QHBoxLayout() 
        buttonbox3 = QHBoxLayout()
Reply
#4
What is the problem? I ran your code and everything lines up to the left.

If you want to compute the fixed width of your LineEdit you can do that using the font metrics
import PySide6.QtWidgets as QtWidgets
from PySide6.QtGui import QFontMetrics
from PySide6.QtGui import Qt

def fitToText(widget, text, padding=10):
        """Set width to fit text"""
        pixels = QFontMetrics(widget.font()).boundingRect(text).width() + padding
        widget.setFixedWidth(pixels)
        return pixels

class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.lp_label = QtWidgets.QLabel("Low-pass (Hz): ", self)
        self.lp_label.setAlignment(Qt.AlignLeft)

        self.lp_edit = QtWidgets.QLineEdit("10", self)
        self.lp_edit.setAlignment(Qt.AlignLeft)
        fixed_width = fitToText(self.lp_edit, "000.00")

        self.hp_label = QtWidgets.QLabel("High-pass (Hz): ", self)
        self.hp_label.setAlignment(Qt.AlignLeft)

        self.hp_edit = QtWidgets.QLineEdit("10", self)
        self.hp_edit.setAlignment(Qt.AlignLeft)
        self.hp_edit.setFixedWidth(fixed_width)


        self.hanning_sel = QtWidgets.QComboBox(self)
        self.hanning_sel.addItems(["Hanning", "Hamming", "Flat", "None"])
        fitToText(self.hanning_sel, "Hamming", 30)


        buttonbox3 = QtWidgets.QVBoxLayout(self)
        buttonbox3.addWidget(self.lp_label)
        buttonbox3.addWidget(self.lp_edit)
        buttonbox3.addWidget(self.hp_label)
        buttonbox3.addWidget(self.hp_edit)
        buttonbox3.addWidget(self.hanning_sel)

app = QtWidgets.QApplication()
window = MyWindow()
window.show()
app.exec()
Please provide a runnable example next time you post
menator01 likes this post
Reply
#5
Or use qgridlayout and not set a fixed size on your line edit. If you are planning on letting the window expand.

from PyQt5.QtWidgets import (QGridLayout, QLabel, QComboBox, QLineEdit, QApplication, QWidget)
from PyQt5.QtCore import Qt
import sys

app = QApplication(sys.argv)
widget = QWidget()
container = QGridLayout()

label = QLabel('Low-pass (Hz): ')
line_edit = QLineEdit()

label2 = QLabel('High-pass (Hz): ')
line_edit2 = QLineEdit()

combo = QComboBox()
geek_list = ['Hanning', 'Hamming', 'Flat', 'None']
combo.addItems(geek_list)

container.addWidget(label, 0, 0, 1, 1)
container.addWidget(line_edit, 0, 1, 1, 1)
container.addWidget(label2, 0, 2, 1, 1)
container.addWidget(line_edit2, 0, 3, 1, 1)
container.addWidget(combo, 0, 4, 1, 1)

widget.setLayout(container)
widget.show()
sys.exit(app.exec())
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#6
(May-07-2022, 06:01 PM)menator01 Wrote: Or use qgridlayout and not set a fixed size on your line edit. If you are planning on letting the window expand.

I think it is my misunderstanding, sorry for that... What I want is:
https://pasteboard.co/j23sW1gpTldR.jpg
Reply
#7
You want it horizontal

import PyQt5.QtWidgets as QtWidgets
from PyQt5.QtGui import QFontMetrics
from PyQt5.QtCore import Qt
 
 
class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 800, 50)
        self.lp_label = QtWidgets.QLabel("Low-pass (Hz): ", self)
        self.lp_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        self.lp_label.setFixedWidth(self.get_metrics(self.lp_label))
 
        self.lp_edit = QtWidgets.QLineEdit("150", self)
        self.lp_edit.setAlignment(Qt.AlignLeft)
        self.lp_edit.setFixedWidth(self.get_metrics(self.lp_edit) + 10)
 
        self.hp_label = QtWidgets.QLabel("High-pass (Hz): ", self)
        self.hp_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        self.hp_label.setFixedWidth(self.get_metrics(self.hp_label))
 
        self.hp_edit = QtWidgets.QLineEdit("1000", self)
        self.hp_edit.setAlignment(Qt.AlignLeft)
        self.hp_edit.setFixedWidth(self.get_metrics(self.hp_edit) + 10)
 
        self.hanning_sel = QtWidgets.QComboBox(self)
        self.hanning_sel.addItems(["Hanning", "Hamming", "Flat", "None"])
        self.hanning_sel.setFixedWidth(100)
        
        #space
        space = QtWidgets.QWidget()
 
        buttonbox3 = QtWidgets.QHBoxLayout(self)
        buttonbox3.addWidget(self.lp_label)
        buttonbox3.addWidget(self.lp_edit)
        buttonbox3.addWidget(self.hp_label)
        buttonbox3.addWidget(self.hp_edit)
        buttonbox3.addWidget(self.hanning_sel)
        buttonbox3.addWidget(space)
        
    def get_metrics(self, label):
        return label.fontMetrics().boundingRect(label.text()).width()
        
 
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    app.exec()
menator01 likes this post
Reply
#8
Is the problem that you want to make a lot of these horizontal groups and everything line up vertically as well?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to auto align x-axis label SamLiu 2 2,263 Jan-27-2023, 11:10 PM
Last Post: SamLiu
  How did one column get left-justified? Mark17 6 3,348 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  "ModuleNotFoundError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package" chipx 3 10,956 Dec-09-2021, 07:05 AM
Last Post: chipx
  Center align Kristenl2784 1 2,762 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align logging messages Mekala 3 9,796 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 5,485 Dec-28-2019, 04:04 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020