c++qt

Setting QScrollArea as central widget in Qt


In Qt I have removed the central widget in an xml editor and replaced it with a QScrollArea, this works when I preview it in QtDesigner:

enter image description here

but when I run the program it looks like this:

enter image description here

Are you not meant to remove the central widget or is there a sizePolicy I have to change?

Here is the ui file:

 <?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>179</width>
    <height>191</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QScrollArea" name="scrollArea">
   <property name="widgetResizable">
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="scrollAreaWidgetContents">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>224</width>
      <height>628</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout_2">
     <item>
      <widget class="QLabel" name="label">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="font">
        <font>
         <pointsize>75</pointsize>
        </font>
       </property>
       <property name="midLineWidth">
        <number>2</number>
       </property>
       <property name="text">
        <string>label

</string>
       </property>
       <property name="alignment">
        <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>179</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="1.qrc"/>
 </resources>
 <connections/>
</ui>

Solution

  • Check your ui_mainwindow.h. See if there is a line like:

    MainWindow->setCentralWidget(scrollArea);
    

    You can use QMainWindow::setCentralWidget function in your mainwindow.cpp to set a central widget to your main window:

    setCentralWidget(myScrollArea);
    

    I don't think there is any way of changing the central widget from the designer.