0

I am trying to align vertically some custom widgets into a scrolledArea. Some research lead me to make a QVboxLayout, adding a QScrollArea into it. I tried and i didn't manage to do it in a proper way.

I found that some people use a two QVboxLayout and Widget level, for example on those pages : http://www.linuxforums.org/forum/programming-scripting/155452-solved-using-qt-scroll-area.html http://kunalmaemo.blogspot.fr/2010/07/scrolling-in-custom-widget-using.html I don't understand why but i tried also.

I read also this and tried, but still doesn't work : QScrollArea missing Scrollbar.

I must do something wrong but i can't tell what. Sometimes my widgets are not scrolled and are just flattened, sometimes the widget doesn't appear at all but never succeed to show a single scroll area. Here is my current code version(simplified with which i think is sufficient) :

MainWIndowImp.h

class MainWindowImp : public QMainWindow, public Ui_MainWindow
{
Q_OBJECT
public :
    MainWindowImp() ;
    ~MainWindowImp() ;

    //more
public slots :
    //some stuff
private
    std::vector<SliderFloat*> _sliders ; //custom widget, works fine
}

ui_mainwindow.h

class Ui_MainWindow
{
public:
    QWidget *centralwidget;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        centralwidget = new QWidget(MainWindow);
                _slider_GB = new QGroupBox(centralwidget);
        _slider_GB->setObjectName(QString::fromUtf8("_slider_GB"));
        _slider_GB->setGeometry(QRect(630, 10, 351, 351));
        _mainScrollArea = new QScrollArea(_slider_GB);
        _mainScrollArea->setObjectName(QString::fromUtf8("_mainScrollArea"));
        _mainScrollArea->setGeometry(QRect(10, 40, 331, 301));
        _mainScrollArea->setWidgetResizable(true);
        scrollAreaWidgetContents = new QWidget();
        scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
        scrollAreaWidgetContents->setGeometry(QRect(0, 0, 329, 299));
        _mainScrollArea->setWidget(scrollAreaWidgetContents);
    }
}

MainWindowImp.cpp

MainWindowImp::MainWindowImp()
{
    setupUi(this)
}

void MainWindowImp::updateSliders()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this) ;
    mainLayout->addWidget(_mainScrollArea);
    QWidget *contents = new QWidget ;
    QVBoxLayout *layout = new QVBoxLayout(contents) ;

    for(uint32_t i=0;i<_approx->latentVariables().cols();++i)
    {
        layout->addWidget(_sliders[i]) ;
    }
    layout->setSizeConstraint(QLayout::SetMinimumSize);
    _mainScrollArea->setWidget(contents);

}

Notes that i used QtDesigner for the GUI, and i just set into it the QScrollArea _mainScrollArea, and it automatically set a QWidget scrollAreaWidgetContents and set it to the QScrollArea. Maybe my issues come from it ?

Thank you for the replies

EDIT :

here is the mainwindow.ui file (removed a lot of things that aren' relevant) :

<?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>1002</width>
    <height>524</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Main Window</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="minimumSize">
    <size>
     <width>981</width>
     <height>0</height>
    </size>
   </property>
   <widget class="MyGLViewer" name="_qglv" native="true">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>600</width>
      <height>400</height>
     </rect>
    </property>
    <property name="minimumSize">
     <size>
      <width>125</width>
      <height>100</height>
     </size>
    </property>
    <property name="maximumSize">
     <size>
      <width>800</width>
      <height>600</height>
     </size>
    </property>
    <property name="mouseTracking">
     <bool>false</bool>
    </property>
   </widget>
   <widget class="QGroupBox" name="_slider_GB">
    <property name="geometry">
     <rect>
      <x>630</x>
      <y>10</y>
      <width>351</width>
      <height>351</height>
     </rect>
    </property>
    <property name="title">
     <string>Coordinates</string>
    </property>
    <widget class="QScrollArea" name="_mainScrollArea">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>40</y>
       <width>331</width>
       <height>301</height>
      </rect>
     </property>
     <property name="widgetResizable">
      <bool>true</bool>
     </property>
     <widget class="QWidget" name="scrollAreaWidgetContents">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>329</width>
        <height>299</height>
       </rect>
      </property>
     </widget>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
  <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1002</width>
     <height>29</height>
    </rect>
   </property>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>MyGLViewer</class>
   <extends>QWidget</extends>
   <header>myglviewer.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
Community
  • 1
  • 1
Irminsul
  • 171
  • 9
  • `_mainScrollArea` is a member of UI class, so it should be `ui->_mainScrollArea` in `MainWindowImp`, unless you have omitted some important code. Besides, can you post your `mainwindow.ui` file content? – Pavel Strakhov May 23 '15 at 08:14
  • Thank you for your answer. Where does the ui->_mainScrollArea is supposed to appear ? Why creating a new QScrollArea into setupUi then calling setupUi when creating the MainWindow isn't enough ? – Irminsul May 26 '15 at 07:31
  • Never mind that. I didn't notice that you're using multiple inheritance of the form class instead of aggregation. Anyway, I run your code, and it seems to work fine: http://i.imgur.com/0lqHAsx.png – Pavel Strakhov May 26 '15 at 09:52
  • 1
    Humm i am quite a newbie and i did not even think to replace my custom widget by a standard one like you did. I just tried and it works also for me. So the issue came from my custom widget which didn't had a minimal height ! Thanks a lot – Irminsul May 26 '15 at 12:06

0 Answers0