Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQT5 - align left
#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


Messages In This Thread
PyQT5 - align left - by frohr - May-07-2022, 04:55 PM
RE: PyQT5 - align left - by menator01 - May-07-2022, 05:27 PM
RE: PyQT5 - align left - by frohr - May-07-2022, 05:39 PM
RE: PyQT5 - align left - by deanhystad - May-07-2022, 05:40 PM
RE: PyQT5 - align left - by menator01 - May-07-2022, 06:01 PM
RE: PyQT5 - align left - by frohr - May-07-2022, 06:16 PM
RE: PyQT5 - align left - by Axel_Erfurt - May-07-2022, 07:18 PM
RE: PyQT5 - align left - by deanhystad - May-07-2022, 09:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to auto align x-axis label SamLiu 2 952 Jan-27-2023, 11:10 PM
Last Post: SamLiu
  How did one column get left-justified? Mark17 6 2,063 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  "ModuleNotFoundError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package" chipx 3 7,747 Dec-09-2021, 07:05 AM
Last Post: chipx
  Explanation of the left side of this statement please rascalsailor 3 2,601 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Center align Kristenl2784 1 2,026 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align logging messages Mekala 3 7,043 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 4,072 Dec-28-2019, 04:04 PM
Last Post: SriRajesh
  Why is left mouse click not working? yeto 3 6,340 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  str.format rounding to the left of the decimal ClassicalSoul 2 2,557 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Move a character left or right in a file. DreamingInsanity 4 4,980 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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