如何使用 Qt Designer 程序将 QWidget 放在中间而不依赖于 Pyside2 中的屏幕大小?

How to put QWidget on a middle in not dependence on screen sizes in Pyside2 using the program Qt Designer?

我现在用的是Pyside2>Qt Desiger 而且我想通过视觉来影响程序的反射。

在 py 上用 ui 翻译他并在文本编辑器中编辑他之前 并且想知道 Qt Desiger 内部是否有可能将 QWidget 准确地放在屏幕中间而不依赖于他的大小。 仅在我找到并能够使用此样式表的代码的地方,并且并非他接受来自 css 的所有命令。

我不想同时使用命令行,因为对我来说,不停地更改一个小细节并重新启动代码会让我很不舒服 正如她所代表的那样理解什么一开始我想在 Qt Designer 上创建一个模板然后已经在命令行中编辑它如果可能如何编辑它而不关闭我想知道的代码 Qt Designer去做

由于 python 没有答案,我发布了这个答案,它是对 the official example of Qt 的翻译。

import sys

from PySide2 import QtCore, QtWidgets


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QWidget()
    w.resize(640, 480)

    w.setGeometry(
        QtWidgets.QStyle.alignedRect(
            QtCore.Qt.LeftToRight,
            QtCore.Qt.AlignCenter,
            w.size(),
            QtWidgets.QApplication.instance().primaryScreen().availableGeometry()
            # or
            # QtWidgets.QApplication.instance().desktop().availableGeometry(),
        )
    )
    w.show()

    sys.exit(app.exec_())

更新:

你可以在QGridLayout中设置stretch到第一列和第三列,类似于行:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout" rowstretch="1,0,1" columnstretch="1,0,1">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item row="0" column="1">
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>142</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="0">
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>254</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="1">
     <widget class="QWidget" name="widget" native="true">
      <property name="styleSheet">
       <string notr="true">background-color: rgb(239, 41, 41);</string>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <widget class="QLabel" name="label">
         <property name="font">
          <font>
           <pointsize>21</pointsize>
          </font>
         </property>
         <property name="text">
          <string>Text</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QTextEdit" name="textEdit">
         <property name="styleSheet">
          <string notr="true">background-color: rgb(255, 255, 255);</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="2">
     <spacer name="horizontalSpacer_2">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>254</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="2" column="1">
     <spacer name="verticalSpacer_2">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>141</height>
       </size>
      </property>
     </spacer>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>