将qwidget添加到QMainWindow作为中心窗口小部件

时间:2016-02-02 16:23:21

标签: qt user-interface qwidget

我在QtCreator中创建了一个Qwidget,并希望将它作为中心窗口小部件放入我的QMainWindow中。这是我在QMainWindow的构造函数中执行的行。

 q = new QWidget();
 setCentralWidget(q);
 ui->setupUi(q);

在下面的照片中,您可以看到我的qWidget的外观。 enter image description here

然而,将它添加到QMainWindow之后就知道它是什么样子了 enter image description here

在这里你可以找到.ui文件。可能是什么问题?

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QWidget" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>785</width>
    <height>600</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>763</width>
    <height>600</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="">
   <layout class="QHBoxLayout" name="horizontalLayout_4">
    <property name="sizeConstraint">
     <enum>QLayout::SetDefaultConstraint</enum>
    </property>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <property name="sizeConstraint">
       <enum>QLayout::SetDefaultConstraint</enum>
      </property>
      <item>
       <widget class="MainGlWidget" name="MainScreen" native="true">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>640</width>
          <height>480</height>
         </size>
        </property>
        <property name="cursor">
         <cursorShape>OpenHandCursor</cursorShape>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_2">
      <property name="sizeConstraint">
       <enum>QLayout::SetDefaultConstraint</enum>
      </property>
      <item>
       <widget class="SideGlWidget" name="widget" native="true">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>120</width>
          <height>120</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>50000</width>
          <height>50000</height>
         </size>
        </property>
       </widget>
      </item>
      <item>
       <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>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>751</width>
     <height>64</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_3">
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>30</horstretch>
          <verstretch>30</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>30</width>
          <height>30</height>
         </size>
        </property>
        <property name="focusPolicy">
         <enum>Qt::ClickFocus</enum>
        </property>
        <property name="text">
         <string>OFF</string>
        </property>
        <property name="icon">
         <iconset>
          <normaloff>off.png</normaloff>
          <normalon>on.jpg</normalon>off.png</iconset>
        </property>
        <property name="iconSize">
         <size>
          <width>50</width>
          <height>50</height>
         </size>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_5">
      <item>
       <layout class="QHBoxLayout" name="horizontalLayout_5">
        <item>
         <widget class="QLabel" name="Wifi">
          <property name="text">
           <string>Wifi Signal</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QProgressBar" name="wifiProgressBar">
          <property name="value">
           <number>95</number>
          </property>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QHBoxLayout" name="horizontalLayout_3">
        <item>
         <widget class="QLabel" name="BatteryLabel">
          <property name="text">
           <string>Battery</string>
          </property>
          <property name="alignment">
           <set>Qt::AlignCenter</set>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QProgressBar" name="BatteryPB">
          <property name="value">
           <number>100</number>
          </property>
          <property name="textDirection">
           <enum>QProgressBar::TopToBottom</enum>
          </property>
         </widget>
        </item>
       </layout>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>MainGlWidget</class>
   <extends>QWidget</extends>
   <header>MainGlWidget.h</header>
   <container>1</container>
   <slots>
    <slot>setXRotation(int)</slot>
    <slot>setZRotation(int)</slot>
    <slot>setYRotation(int)</slot>
   </slots>
  </customwidget>
  <customwidget>
   <class>SideGlWidget</class>
   <extends>QWidget</extends>
   <header>SideGlWidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:0)

您应该使用顶级布局来排列子布局元素 horizo​​ntalLayout horizo​​ntalLayout_4

这是我们修复的.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QWidget" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>785</width>
    <height>600</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>763</width>
    <height>600</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_4">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_3">
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
           <horstretch>30</horstretch>
           <verstretch>30</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>30</width>
           <height>30</height>
          </size>
         </property>
         <property name="focusPolicy">
          <enum>Qt::ClickFocus</enum>
         </property>
         <property name="text">
          <string>OFF</string>
         </property>
         <property name="icon">
          <iconset>
           <normaloff>off.png</normaloff>
           <normalon>on.jpg</normalon>off.png</iconset>
         </property>
         <property name="iconSize">
          <size>
           <width>50</width>
           <height>50</height>
          </size>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_5">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_5">
         <item>
          <widget class="QLabel" name="Wifi">
           <property name="text">
            <string>Wifi Signal</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QProgressBar" name="wifiProgressBar">
           <property name="value">
            <number>95</number>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <item>
          <widget class="QLabel" name="BatteryLabel">
           <property name="text">
            <string>Battery</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QProgressBar" name="BatteryPB">
           <property name="value">
            <number>100</number>
           </property>
           <property name="textDirection">
            <enum>QProgressBar::TopToBottom</enum>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_4">
     <property name="sizeConstraint">
      <enum>QLayout::SetDefaultConstraint</enum>
     </property>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <property name="sizeConstraint">
        <enum>QLayout::SetDefaultConstraint</enum>
       </property>
       <item>
        <widget class="MainGlWidget" name="MainScreen" native="true">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>640</width>
           <height>480</height>
          </size>
         </property>
         <property name="cursor">
          <cursorShape>OpenHandCursor</cursorShape>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_2">
       <property name="sizeConstraint">
        <enum>QLayout::SetDefaultConstraint</enum>
       </property>
       <item>
        <widget class="SideGlWidget" name="widget" native="true">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>120</width>
           <height>120</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>50000</width>
           <height>50000</height>
          </size>
         </property>
        </widget>
       </item>
       <item>
        <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>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>MainGlWidget</class>
   <extends>QWidget</extends>
   <header>MainGlWidget.h</header>
   <container>1</container>
   <slots>
    <slot>setXRotation(int)</slot>
    <slot>setZRotation(int)</slot>
    <slot>setYRotation(int)</slot>
   </slots>
  </customwidget>
  <customwidget>
   <class>SideGlWidget</class>
   <extends>QWidget</extends>
   <header>SideGlWidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>