将 qwidget 添加到 QMainWindow 作为中央小部件

adding a qwidget into QMainWindow as central widget

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

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

在下面的照片中,您可以看到我的 qWidget 的样子。

但是在将它添加到 QMainWindow 之后它就是这个样子

在这里您可以找到 .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>

您应该使用顶级布局来安排您的子布局元素水平布局horizontalLayout_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>