简体   繁体   English

如何在所有固定小部件之间添加间距

[英]How to add spacing between all fixed widgets

I have a based GUI application.我有一个基于的 GUI 应用程序。 I just want all the widgets like Textbox or Buttons to have a fixed size.我只希望文本框按钮等所有小部件具有固定大小。 And i successfully did that!我成功地做到了! But

I want that if someone resizes the window, the widgets must have their same fixed size BUT it should increase/decrease the spaces between them, rather than changing the size of widgets.我希望如果有人调整 window 的大小,小部件必须具有相同的固定大小,它应该增加/减少它们之间的空间,而不是改变小部件的大小。 (I don't want my widgets to be resized, that's why i opt this method to increase/decrease their spaces between each other). (我不希望我的小部件被调整大小,这就是为什么我选择这种方法来增加/减少它们之间的空间)。

For example:例如: 原始应用程序状态 (Original application state) (原申请状态) 垂直调整大小 (Trying to resize the window to bigger size vertically, but you can see that no buttons widget's size changed. Just more space was added...) (尝试将 window 垂直调整为更大的尺寸,但您可以看到按钮小部件的尺寸没有改变。只是添加了更多空间......) 水平调整大小 (Trying to resize horizontally) (尝试水平调整大小)

I found that QSizePolicy does the same thing that i want, but didn't get any good examples anywhere.我发现QSizePolicy做了我想要的同样的事情,但在任何地方都没有得到任何好的例子。

This is my code:这是我的代码:

from PySide2 import QtWidgets
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtGui import QPainter, QBrush, QPen, QColor, QPolygon
from PySide2.QtCore import Qt, QRect, QPoint, QRectF
import sys

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

    def initUI(self):
        self.setGeometry(200, 200, 300, 300)
        self.setWindowTitle("test app")

        self.label = QtWidgets.QLineEdit(self)
        self.label.setText("my first label!")
        self.label.move(110,10)
        self.label.resize(180,25)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("Fixed Button size!")
        self.b1.move(180,250)
        self.b1.resize(110, 25)
        self.b1.setStyleSheet("""
        background-color: #4CAF50;
        border: none;
        color: white;
        text-align: center;
        font-size: 12px;
        font-family: Arial
        """)

    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()

    def drawRectangles(self, qp):

        col = QColor(0, 0, 0)
        col.setNamedColor('#d4d4d4')
        qp.setPen(col)

        qp.setBrush(QColor(200, 50, 8))
        qp.drawRect(0, 0, 100, 299)

        qp.setBrush(QColor(250, 255, 250))
        qp.drawRect(110, 50, 180, 180)


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

window()


It hides the widgets when resized.它在调整大小时隐藏小部件。 I want to make the buttons and text widgets to add spaces between them, when resized.我想让按钮和文本小部件在调整大小时在它们之间添加空格。 Now it's hiding those widgets because i didn't used hstack or vstack .现在它隐藏了这些小部件,因为我没有使用hstackvstack I know.我知道。

Just don't know how to use QSizePolicy for the spacing purpose.只是不知道如何将QSizePolicy用于间距目的。

Thanks in advance...提前致谢...

For what you want to obtain there are multiple solutions, my solution uses the properties of the layouts:对于您想要获得的有多种解决方案,我的解决方案使用布局的属性:

import sys

from PySide2 import QtCore, QtGui, QtWidgets


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

    def initUI(self):
        self.setGeometry(200, 200, 300, 300)
        self.setWindowTitle("test app")

        label = QtWidgets.QLabel(self)
        label.setFixedWidth(100)
        label.setStyleSheet("background-color: rgb(200, 50, 8)")

        lineedit = QtWidgets.QLineEdit()
        lineedit.setFixedWidth(180)

        button = QtWidgets.QPushButton("Fixed Button size!")
        button.setFixedSize(110, 25)
        button.setStyleSheet(
            """
        background-color: #4CAF50;
        border: none;
        color: white;
        text-align: center;
        font-size: 12px;
        font-family: Arial
        """
        )

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

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

        right_container = QtWidgets.QWidget()

        botton_widget = QtWidgets.QWidget()
        botton_widget.setContentsMargins(0, 0, 0, 0)
        botton_widget.setFixedWidth(180)
        hlay2 = QtWidgets.QHBoxLayout(botton_widget)
        hlay2.setContentsMargins(0, 0, 0, 0)
        hlay2.addStretch()
        hlay2.addWidget(button)

        glay = QtWidgets.QGridLayout(right_container)
        glay.addWidget(QtWidgets.QWidget(), 0, 0, 5, 1)
        glay.addWidget(lineedit, 0, 1)
        glay.addWidget(QtWidgets.QWidget(), 1, 1)
        glay.addWidget(widget, 2, 1, alignment=QtCore.Qt.AlignCenter)
        glay.addWidget(QtWidgets.QWidget(), 3, 1)
        glay.addWidget(QtWidgets.QWidget(), 0, 2, 5, 1)
        glay.addWidget(botton_widget, 4, 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM