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

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

我现在使用Pyside2> Qt Desiger而且我希望通过视觉影响程序的反映。

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

我不想花时间使用命令行,因为对我来说,不断地更改一个小细节并重新启动代码已经足够不舒服了代表她时要了解什么首先,我想在Qt Designer上创建一个模板,然后已经在命令行中对其进行了编辑。如果有可能如何编辑而不关闭我想知道的代码Qt Designer,去做

enter image description here

python qt-designer pyside2
1个回答
0
投票

由于没有针对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中的拉伸设置为第一行和第三列,类似于行:

enter image description here

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.