Scott Logan
Scott Logan

Reputation: 1516

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>

Upvotes: 0

Views: 1959

Answers (2)

thuga
thuga

Reputation: 12931

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.

Upvotes: 2

user2734982
user2734982

Reputation:

I am getting proper window when I ran the program with your ui file content. I don't know why it is not creating problem like you have.

I would recommend you to use Qt designer inbuilt in Qt Creator which I felt easier to use.

Also, I think the problem can be one of these:

  1. Generally central widget is good as it provides a base on which you can place all your layouts. So, it is good to have it in your UI.

    Here, take care that after inserting widgets into layouts you don't simply break those layouts. The designer will reset the widget width and height. So you have to set them again.

  2. Next, this may have occured if the ui file loaded in designer is not up-to-date while it is changed on disk.

    Keep track of all the layouts, the geometry and sizePolicy of widgets. I won't be able to answer in code since when I used the ui file content which you have given, it doesn't cause problem here.

  3. Create a layout on your mainwindow in which you would put your QScrollArea. This would expand it according to the layout.

P.S. : I open it in Qt Creator.

So please give some more info whether it is fixed when you set the geometry of scrollarea manually.

Upvotes: 1

Related Questions