【问题标题】:Qt Designer - Why does number get pushed to the left when LCD Number is resized?Qt Designer - 为什么调整 LCD 数字大小时数字会被推到左边?
【发布时间】:2021-07-16 06:41:39
【问题描述】:

我正在 Qt Designer(Qt 版本:5.15.2)中构建一个非常基本的计算器演示应用程序。我注意到当我调整窗口大小时,LCD 数字显示中的数字被推到左边。为什么会出现这种情况?

调整大小之前:

调整大小后:

【问题讨论】:

  • 这里是.ui文件的内容:pastebin.com/B8eqUArA
  • 您是否在控制台中看到任何有关绑定循环或转换或任何内容的警告或错误?如果是这样,Qt 有时会放弃完全充实布局,因为它不知道该怎么做。
  • 当我将 .ui 转换为 .py 并在基本 PyQt5 程序中使用布局时,控制台中没有警告或错误。

标签: qt


【解决方案1】:

我放了一个horizontalSpacer输出是这样的,试试这个:

<?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>473</width>
    <height>274</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QGridLayout" name="gridLayout">
      <item row="3" column="0">
       <widget class="QPushButton" name="radicalButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>√</string>
        </property>
        <property name="flat">
         <bool>false</bool>
        </property>
       </widget>
      </item>
      <item row="1" column="2">
       <widget class="QPushButton" name="mMinusButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>M-</string>
        </property>
       </widget>
      </item>
      <item row="5" column="3">
       <widget class="QPushButton" name="equalsButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>=</string>
        </property>
       </widget>
      </item>
      <item row="1" column="0">
       <widget class="QPushButton" name="offButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>OFF</string>
        </property>
       </widget>
      </item>
      <item row="2" column="4">
       <widget class="QPushButton" name="timesButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>×</string>
        </property>
       </widget>
      </item>
      <item row="2" column="3">
       <widget class="QPushButton" name="button9">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>9</string>
        </property>
       </widget>
      </item>
      <item row="5" column="0">
       <widget class="QPushButton" name="acButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>AC</string>
        </property>
       </widget>
      </item>
      <item row="2" column="0">
       <widget class="QPushButton" name="percentButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>%</string>
        </property>
       </widget>
      </item>
      <item row="4" column="2">
       <widget class="QPushButton" name="button2">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>2</string>
        </property>
       </widget>
      </item>
      <item row="4" column="0">
       <widget class="QPushButton" name="cButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>C</string>
        </property>
       </widget>
      </item>
      <item row="4" column="3">
       <widget class="QPushButton" name="button3">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>3</string>
        </property>
       </widget>
      </item>
      <item row="1" column="4">
       <widget class="QPushButton" name="divideButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>÷</string>
        </property>
       </widget>
      </item>
      <item row="5" column="2">
       <widget class="QPushButton" name="dotButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>.</string>
        </property>
       </widget>
      </item>
      <item row="3" column="1">
       <widget class="QPushButton" name="button4">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>4</string>
        </property>
       </widget>
      </item>
      <item row="5" column="1">
       <widget class="QPushButton" name="button0">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>0</string>
        </property>
       </widget>
      </item>
      <item row="4" column="1">
       <widget class="QPushButton" name="button1">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>1</string>
        </property>
       </widget>
      </item>
      <item row="1" column="1">
       <widget class="QPushButton" name="mrcButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>MRC</string>
        </property>
       </widget>
      </item>
      <item row="3" column="2">
       <widget class="QPushButton" name="button5">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>5</string>
        </property>
       </widget>
      </item>
      <item row="1" column="3">
       <widget class="QPushButton" name="mPlusButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>M+</string>
        </property>
       </widget>
      </item>
      <item row="2" column="1">
       <widget class="QPushButton" name="button7">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>7</string>
        </property>
       </widget>
      </item>
      <item row="2" column="2">
       <widget class="QPushButton" name="button8">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>8</string>
        </property>
       </widget>
      </item>
      <item row="3" column="3">
       <widget class="QPushButton" name="button6">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>6</string>
        </property>
       </widget>
      </item>
      <item row="3" column="4">
       <widget class="QPushButton" name="minusButton">
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>-</string>
        </property>
       </widget>
      </item>
      <item row="4" column="4" rowspan="2">
       <widget class="QPushButton" name="plusButton">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string>+</string>
        </property>
       </widget>
      </item>
      <item row="0" column="4">
       <widget class="QLCDNumber" name="lcdNumber">
        <property name="enabled">
         <bool>true</bool>
        </property>
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="cursor">
         <cursorShape>ForbiddenCursor</cursorShape>
        </property>
        <property name="autoFillBackground">
         <bool>true</bool>
        </property>
        <property name="frameShape">
         <enum>QFrame::NoFrame</enum>
        </property>
        <property name="frameShadow">
         <enum>QFrame::Raised</enum>
        </property>
        <property name="smallDecimalPoint">
         <bool>false</bool>
        </property>
        <property name="digitCount">
         <number>1</number>
        </property>
        <property name="segmentStyle">
         <enum>QLCDNumber::Flat</enum>
        </property>
        <property name="value" stdset="0">
         <double>0.000000000000000</double>
        </property>
        <property name="intValue" stdset="0">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="0" column="0" colspan="4">
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

【讨论】:

  • 这确实解决了数字向左移动的问题,所以我将它标记为接受。但是,这确实使显示比我想要的小得多。
  • 设置数字计数 1 而不是 10。
  • 如果你想显示 10 位数的数字,你需要更多的空间,因此,它显示得更小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 2013-11-15
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多