pyqt5- qgridlayout中的中心对象

发布于 2025-02-05 07:12:13 字数 1503 浏览 2 评论 0原文

我有一个问题。我尝试了PYQT5中的布局,它可以部分工作。但不是我想要的方式。

这是我的代码:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class CompanyLogin(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.layout = QGridLayout()

        self.setLayout(self.layout)
        self.setWindowTitle('Window')
        self.setGeometry(0, 0, 1920, 1080)

        self.setupUI()

    def setupUI(self):
        company_note = QLabel(parent=self, text='Company: ')
        self.company = QComboBox(parent=self)
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel(parent=self, text='Password: ')
        self.password = QLineEdit(parent=self)

        submit = QPushButton(parent=self, text='Submit')

        verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.layout.addItem(verticalSpacer, 6, 0, Qt.AlignTop)

        self.layout.addWidget(company_note, 1, 0, Qt.AlignLeft)
        self.layout.addWidget(self.company, 1, 1, Qt.AlignRight)

        self.layout.addWidget(password_note, 2, 0, Qt.AlignLeft)
        self.layout.addWidget(self.password, 2, 1, Qt.AlignRight)

        self.layout.addWidget(submit, 3, 0, 1, 0, Qt.AlignCenter)

这是我窗口到目前为止的样子:

“现在”

,这就是我希望它看起来的样子:

“ aff

I have a question. I have tried layouts in PyQt5 and it works partially. But not the way I want it.

This is my code:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class CompanyLogin(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.layout = QGridLayout()

        self.setLayout(self.layout)
        self.setWindowTitle('Window')
        self.setGeometry(0, 0, 1920, 1080)

        self.setupUI()

    def setupUI(self):
        company_note = QLabel(parent=self, text='Company: ')
        self.company = QComboBox(parent=self)
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel(parent=self, text='Password: ')
        self.password = QLineEdit(parent=self)

        submit = QPushButton(parent=self, text='Submit')

        verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.layout.addItem(verticalSpacer, 6, 0, Qt.AlignTop)

        self.layout.addWidget(company_note, 1, 0, Qt.AlignLeft)
        self.layout.addWidget(self.company, 1, 1, Qt.AlignRight)

        self.layout.addWidget(password_note, 2, 0, Qt.AlignLeft)
        self.layout.addWidget(self.password, 2, 1, Qt.AlignRight)

        self.layout.addWidget(submit, 3, 0, 1, 0, Qt.AlignCenter)

This is what my window looks like so far:

Window Now

And this is how I want it to look:

Window After

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2025-02-12 07:12:13

目前,最简单的解决方案 是添加以下简单的行:

self.layout.setAlignment(Qt.AlignTop|Qt.AlignHCenter)

但是,实际上,设置布局对齐通常是导致意外问题的原因,事实是,您的代码中存在一些问题取而代之的是:

  • 您是从 second row开始添加小部件;
  • 添加窗口小部件时,您正在使用对齐方式,但这仅确保将使用窗口小部件最小大小提示,并且小部件将然后遵循对齐方式,这显然不是什么你需要;
  • 像您在最后一行中所做的0个值跨度是错误的,因为它指示了小部件占据了多少行或列单元格(不是基于索引),因此应为1或更多;
  • 您应该使用 setRowsTretch() setColumnStretch() ;

其他小笔记: layout() < < /a>是所有qwidgets的现有动态函数,您不得覆盖它;另外,如果您然后将小部件添加到父级的布局中,则小部件构造函数中的parent参数是毫无意义的。最后,仅在需要时使用关键字参数。

    def setupUI(self):
        self.layout.setColumnStretch(0, 1)
        self.layout.setColumnStretch(3, 1)
        self.layout.setRowStretch(3, 1)

        company_note = QLabel('Company: ')
        self.company = QComboBox()
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel('Password: ')
        self.password = QLineEdit()

        submit = QPushButton('Submit')

        self.layout.addWidget(company_note, 0, 1)
        self.layout.addWidget(self.company, 0, 2)

        self.layout.addWidget(password_note, 1, 1)
        self.layout.addWidget(self.password, 1, 2)

        self.layout.addWidget(submit, 2, 1, 1, 2)

Right now the simplest solution could be to add this simple line:

self.layout.setAlignment(Qt.AlignTop|Qt.AlignHCenter)

But, actually, setting the layout alignment is often cause of unexpected problems, and the fact is that there are some issues in your code that should be fixed instead:

  • you are adding widgets starting from the second row;
  • you are using alignments when adding widgets, but that only ensures that the widget minimum size hint will be used, and the widget will then follow the alignment, which is clearly not what you need;
  • a 0 value span as you did in the last line is wrong, since it indicates how many row or column cells a widget occupies (it's not index based), so it should be 1 or more;
  • you should use setRowStretch() and setColumnStretch();

Other small notes: layout() is an existing dynamic function of all QWidgets, you shall not overwrite it; also, the parent argument in widget constructors is pointless if you then add the widget to the parent's layout; finally, use keyword arguments only when required.

    def setupUI(self):
        self.layout.setColumnStretch(0, 1)
        self.layout.setColumnStretch(3, 1)
        self.layout.setRowStretch(3, 1)

        company_note = QLabel('Company: ')
        self.company = QComboBox()
        self.company.addItem('t1')
        self.company.addItem('t2')

        password_note = QLabel('Password: ')
        self.password = QLineEdit()

        submit = QPushButton('Submit')

        self.layout.addWidget(company_note, 0, 1)
        self.layout.addWidget(self.company, 0, 2)

        self.layout.addWidget(password_note, 1, 1)
        self.layout.addWidget(self.password, 1, 2)

        self.layout.addWidget(submit, 2, 1, 1, 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文