当我调整窗口大小时,无法使窗口小部件正常生长

时间:2016-07-28 14:33:00

标签: qt

我制作了一个如下形式的表格:screenshot of the form。 .ui在下面。

右侧的QTextEdit是唯一一个将其水平大小策略设置为“扩展”的小部件。所有其他小部件都是Preferred或Minimum。然而,当我水平调整窗口大小时,文本编辑是增长的唯一小部件。它保持相同的大小,而所有其他小部件水平扩展。

我想有些东西我不明白所有这些是如何运作的。当我调整窗口大小时,我该怎么做才能使文本编辑扩展?

.ui文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWidget</class>
 <widget class="QWidget" name="MainWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>634</width>
    <height>303</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="3" column="4" rowspan="7" colspan="2">
    <widget class="QTextEdit" name="textEdit">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="minimumSize">
      <size>
       <width>300</width>
       <height>0</height>
      </size>
     </property>
     <property name="readOnly">
      <bool>true</bool>
     </property>
    </widget>
   </item>
   <item row="5" column="2">
    <widget class="QLabel" name="label_5">
     <property name="text">
      <string>Data written:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="6" column="1">
    <widget class="QLabel" name="readSpeedLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="5" column="0">
    <widget class="QLabel" name="label_3">
     <property name="text">
      <string>Write speed:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="4" column="0">
    <widget class="QLabel" name="label_2">
     <property name="text">
      <string>Test Mode:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="5" column="3">
    <widget class="QLabel" name="dataWrittenLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="6" column="0">
    <widget class="QLabel" name="label_7">
     <property name="text">
      <string>Read speed:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="4" column="1" colspan="3">
    <widget class="QPushButton" name="testModePushButton">
     <property name="text">
      <string>Select testing mode</string>
     </property>
    </widget>
   </item>
   <item row="9" column="2" colspan="2">
    <widget class="QPushButton" name="startStopButton">
     <property name="enabled">
      <bool>false</bool>
     </property>
     <property name="text">
      <string>Start test</string>
     </property>
    </widget>
   </item>
   <item row="3" column="1" colspan="3">
    <widget class="QPushButton" name="devicePushButton">
     <property name="text">
      <string>Select device</string>
     </property>
    </widget>
   </item>
   <item row="7" column="3">
    <widget class="QLabel" name="ETALabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="5" column="1">
    <widget class="QLabel" name="writeSpeedLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="7" column="2">
    <widget class="QLabel" name="label_11">
     <property name="text">
      <string>ETA:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="6" column="3">
    <widget class="QLabel" name="dataReadLabel">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Sunken</enum>
     </property>
     <property name="text">
      <string>TextLabel</string>
     </property>
    </widget>
   </item>
   <item row="6" column="2">
    <widget class="QLabel" name="label_9">
     <property name="text">
      <string>Data Read:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
   <item row="8" column="0" colspan="4">
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>40</height>
      </size>
     </property>
    </spacer>
   </item>
   <item row="3" column="0">
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Device:</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

正在弄乱你的布局的QLabel项目。

为每个Fixed将水平尺寸政策设置为QLabel并且有效。

现在,QLabel PreferredQTextEdit Expanding的增长速度比Preferred Fix的增长速度更快,我不知道。特别是在将Preferred更改为sizeHint()时可以解决问题。

Qt文档声明:

  

QSizePolicy :: Expanding 小部件可以利用额外的空间,因此它应该尽可能多地占用空间。

     

QSizePolicy :: Preferred 可以扩展窗口小部件,但它大于sizeHint()没有任何优势。

     

QSizePolicy :: Fixed QWidget :: sizeHint()是唯一可接受的选择,因此小部件永远不会增长或缩小。

看着这个,似乎Expanding的小部件在QPushButton的另一个小部件旁边不会比@RequestMapping(value = "/deleteContentFromMarineCustomer", method = RequestMethod.DELETE, produces = "application/json") public HttpEntity<String> deleteContentFromMarineCustomer(@RequestParam(value = "srcId") String srcId, @RequestParam(value = "customerId") String customerId, @RequestParam(value = "locale") String locale, @RequestParam(value = "method") String method) { ResponseEntity<String> responseEntity = null; try { // oneChartService.deleteContentFromMarineCustomer(customerId, srcId); // responseEntity = new ResponseEntity<String>(HttpStatus.OK); throw new Exception("test"); } catch (Exception e) { responseEntity = new ResponseEntity<String>("Delete", HttpStatus.SERVICE_UNAVAILABLE); } return responseEntity; } 大。但在您的情况下,标签的行为类似于优先于文本编辑。 请注意,function deleteSelectedContent() { var link = "content/deleteContentFromMarineCustomer?srcId=" + srcId + "&customerId=" + customerId + "&locale=" + locale+"&method=Delete"; jQuery.ajax({ url : link, type : 'DELETE', success : function(data, status) { window.location.reload(); }, error : function(data, status) { jQuery("#errorContentModal").show(); } }); 似乎不是这种情况。

对我来说这看起来像个错误。