如何使我的小部件不显示在中间

问题描述 投票:0回答:1

我将所有小部件都放在网格和水平布局中(除了左侧的小部件,'cauz我只希望它在那里)。让我告诉你:

all widgets in the centre

我希望将它们像这样放置在下图中,而不是将所有小部件放在上面的图像中:

all widgets at corner and one at center

如您所见,顶部的一个在角落,中间的一个矩形在中心。我希望将它们放置在我想要的任何位置,无论是在中间还是在角落,但同时,不要丢失我的网格和添加的间距,因为当您水平/垂直拉伸此网格时,它将在两个之间增加更多/更少的空间小部件。如您所见,通过扩展该应用程序。

这是我的代码:

import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtGui import QCursor
from PySide2.QtWidgets import QFrame
from PySide2.QtCore import QRect

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(250, 80, 800, 600)
        self.setWindowTitle("test app")
        self.setStyleSheet("background-color: #ffffff; border")

        label = QtWidgets.QLabel(self)
        label.setFixedWidth(260)
        label.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.994, y1:0.527, x2:0.999682, y2:0.005, stop:0 rgba(246, 246, 246, 255), stop:1 rgba(255, 248, 248, 255));")

        lineedit = QtWidgets.QLineEdit()
        lineedit.setFixedWidth(310)
        lineedit.setFixedHeight(30)
        lineedit.setStyleSheet("""
        background-color: #ffffff; 
        border-width: 1px; 
        border-color: rgb(200, 200, 200); 
        border-style: solid;
        border-radius: 4;
        padding: 6px 12px;
        color: rgb(50,50,50);
        font-family: SourceSansPro-Regular;
        font-size: 12px;
        """)
        f = lineedit.font()
        f.setLetterSpacing(QtGui.QFont.PercentageSpacing, 100)
        lineedit.setFont(f)

        button = QtWidgets.QPushButton("New Product")
        button.setFixedSize(110, 28)
        button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        button1 = QtWidgets.QPushButton("Delete Product")
        button1.setFixedSize(110, 28)
        button1.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button1.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        button2 = QtWidgets.QPushButton("Next product")
        button2.setFixedSize(110, 28)
        button2.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button2.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        widget = QtWidgets.QLabel()
        widget.setFixedSize(180, 180)
        widget.setStyleSheet("""background-color: rgb(200, 255, 250)""")

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        right_container = QtWidgets.QWidget()

        glay = QtWidgets.QGridLayout(right_container)
        glay.addWidget(lineedit, 0, 0)
        glay.addWidget(widget, 1, 0)
        glay.addWidget(button, 2, 0)

        glay.addWidget(button1, 2, 1)

        glay.addWidget(button2, 0, 1)

        hlay = QtWidgets.QHBoxLayout(central_widget)
        hlay.setContentsMargins(0, 0, 0, 0)
        hlay.addWidget(label)
        hlay.addWidget(right_container)


def main():
    app = QtWidgets.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()


我尝试了很多事情,但最终都出错了。关于如何实现这一点,我就没有逻辑了。

提前感谢...

编辑1:我尝试了注释中建议的这两种方法:

1)添加glay.addWidget(lineedit, 0, 0, alignment = QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)。即使应用更改,它也会产生相同的结果。

2)使用QVBoxLayout并没有帮助。某些事情不允许我在下面绘制的矩形处添加小部件。因为我不能在矩形边添加小部件。 (请参见下图...)

enter image description here

python python-3.x pyside2
1个回答
1
投票

void QGridLayout :: setColumnStretch(int column,int Stretch)

设置要扩展的列的拉伸因子。第一列是数字0。


void QGridLayout :: setRowStretch(int row,int Stretch)

设置要拉伸的行的拉伸因子。第一行是数字0。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QFrame
from PyQt5.QtCore import QRect

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(250, 80, 800, 600)
        self.setWindowTitle("test app")
        self.setStyleSheet("background-color: #ffffff; border")

        label = QtWidgets.QLabel(self)
        label.setFixedWidth(260)
        label.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.994, y1:0.527, x2:0.999682, y2:0.005, stop:0 rgba(246, 246, 246, 255), stop:1 rgba(255, 248, 248, 255));")

        lineedit = QtWidgets.QLineEdit()
        lineedit.setFixedWidth(310)
        lineedit.setFixedHeight(30)
        lineedit.setStyleSheet("""
        background-color: #ffffff; 
        border-width: 1px; 
        border-color: rgb(200, 200, 200); 
        border-style: solid;
        border-radius: 4;
        padding: 6px 12px;
        color: rgb(50,50,50);
        font-family: SourceSansPro-Regular;
        font-size: 12px;
        """)
        f = lineedit.font()
        f.setLetterSpacing(QtGui.QFont.PercentageSpacing, 100)
        lineedit.setFont(f)

        button = QtWidgets.QPushButton("New Product")
        button.setFixedSize(110, 28)
        button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        button1 = QtWidgets.QPushButton("Delete Product")
        button1.setFixedSize(110, 28)
        button1.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button1.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        button2 = QtWidgets.QPushButton("Next product")
        button2.setFixedSize(110, 28)
        button2.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        button2.setStyleSheet("""
        QPushButton {
        background-color: #474767;
        border: none;
        color: white;
        text-align: center;
        font-size: 14px;
        padding: 7px 10px;
        border-radius: 3;
        font-family: Source Sans Pro SemiBold;
        }
        QPushButton:hover{
        background-color: #474757;
        }
        """)

        widget = QtWidgets.QLabel()
#        widget.setFixedSize(180, 180)
        widget.setMinimumHeight(180)                                           # +++

        widget.setStyleSheet("""background-color: rgb(200, 255, 250)""")

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        right_container = QtWidgets.QWidget()

# ++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        glay = QtWidgets.QGridLayout(right_container)
        glay.addWidget(lineedit, 0, 0)
        glay.addWidget(button2, 0, 2)

        glay.addWidget(widget, 2, 0, 1, 3)                              # 2

        glay.addWidget(button, 4, 0)                                    # 4
        glay.addWidget(button1, 4, 2)

        glay.setColumnStretch(1, 1)                                     # setColumnStretch
        glay.setRowStretch(1, 1)                                        # setRowStretch
        glay.setRowStretch(2, 2)                                        # setRowStretch
        glay.setRowStretch(3, 1)                                        # setRowStretch
# ++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        hlay = QtWidgets.QHBoxLayout(central_widget)
        hlay.setContentsMargins(0, 0, 0, 0)
        hlay.addWidget(label)
        hlay.addWidget(right_container)


def main():
    app = QtWidgets.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()  

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.