如何使用Qt设计器将QWidget置于中间位置,而不依赖Pyside2中的屏幕大小?

2024-04-20 07:53:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在使用Pyside2>;Qt Desiger和我希望通过视觉影响程序的反射

在用py上的ui翻译他并在文本编辑器中编辑他之前 并且想知道QtDesiger是否有可能在不依赖其尺寸的情况下将QWidget精确地放在屏幕中间。 只有在我找到并能够使用这个样式表中的代码的地方,他才接受css中的所有命令

我不想在任何时候使用命令行,因为对我来说,不断地更改一个小细节并重新启动代码都会让我感到不舒服 我想在Qt Designer上创建一个模板,然后在命令行中编辑它,如果可能的话,如何编辑它,而不是关闭代码Qt Designer,我想知道如何做

enter image description here


Tags: 代码命令行pygt程序编辑ui尺寸
1条回答
网友
1楼 · 发布于 2024-04-20 07:53:30

由于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>

相关问题 更多 >