2

I understand how to add a scrollArea to a particular widget. However in my case Qwidget has multiple child widgets and these are all set using QVBoxLayout. Now how can I add a scroll bar in this case? Here QWidget is not the center widget, its one of the pages of the TabWidget. My code looks like:

QTabWIdget *center = new QTabWidget; setCentralWIdget(center);

xTab = new QWidget;

formLayout = new QFormLayout; formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...);

xTab->setLayout(formLayout);

Now how can I set the scrollBar to xTab? I tried using

scrollArea = new QScrollArea;
scrollArea->setWidget(xTab);

however, this isn't working.

Any idea/suggestions are helpful and appreciated.

ymoreau
  • 3,402
  • 1
  • 22
  • 60
user408307
  • 21
  • 1
  • 2
  • What should be scrolling? The whole tab widget or just the contents of a single tab? Have you tried creating your UI in Qt Designer? – andref Aug 03 '10 at 19:12
  • I just want the contents of the tab widget to be scrolling. I am not using Qt designer for my app, however I tried a sample program with Qt designer where I added a vertical scroll bar. The result was that it adds the scroll bar but doesn't scroll the contents. – user408307 Aug 05 '10 at 04:27

2 Answers2

2

Have you tried using QScrollArea as the tab page?

QTabWIdget *center = new QTabWidget; setCentralWIdget(center);

xTab = new QScrollArea; 
formLayout = new QFormLayout; formLay....
xTab->setLayout(formLayout);
center->addTab(xTab, "XXX Tab");
ak.
  • 3,329
  • 3
  • 38
  • 50
  • I tried that way too. However in this case its resizing the widgets. In this tab I have a textBrowser and instead of enabling the scrollBar to scroll the contents, the TextBrowser is resized much like a lineEdit and all the widgets are adjusted in the available space. – user408307 Aug 05 '10 at 04:48
  • I exclusively set xTab->setWidgetResizable(false) - even though its by default. Even this didn't help. Also, I am wondering this way and setWidget(xTab) aren't same functionally. – user408307 Aug 05 '10 at 04:55
  • 1
    by default most of the widgets have their size policy set to QSizePolicy::Preferred which means that the widget can be expanded, but there is no advantage to do this. You may want to set the minimum size constraints on the child widgets to ensure their usability – ak. Aug 05 '10 at 08:46
0

I had success using the following:

scroll=new QScrollArea(mainWindow->centralWidget);
scroll->setGeometry(mainWindow->tabWidget->geometry());
scroll->setWidget(mainWindow->tabWidget);
scroll->show();

The QScrollArea defines where the scrollable widget will appear. If parent is 0, it's a non-modal window. setGeometry sets the QScrollArea instance to the desired dimensions (that of the tab). setWidget defines what widget the QScrollArea will actually be scrolling.

Bruce
  • 2,230
  • 18
  • 34