PDA

View Full Version : Adding scroll bars to a main window



dougab
24th December 2007, 20:30
I want to add H and V scroll bars to my application which only has a QMainWindow. I can't seem to find a property for it and if I try to add a QScrollArea to either the mainwindow or the centralwidget it doesn't seem to work. I've looked through Assistant and through The Book of Qt 4 by Daniel Molkentin but haven't been able to figure it out.

Thanks
Doug

jacek
24th December 2007, 20:36
The scroll bar will appear automagically when QScrollArea contents becomes to big.

dougab
24th December 2007, 20:59
Isn't working for me.

jpn
24th December 2007, 21:02
May we see some code? How do you construct the scroll area and its contents? Is the scroll area in a layout or as central widget?

dougab
24th December 2007, 23:38
In the constructor of the main window I have the following code:

setupUi(this)
QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(mw);

mw is a pointer to this instance of the main window; the window will display but without scroll bars. If I use setWidget(this) (which I would think would be the correct way to do it) the window never shows up at all. If I do setWidget(centralwidget) I just get a blank window.

Thanks
Doug

jacek
25th December 2007, 00:04
You should set the QScrollArea as the central widget for your main window and add the contents of the main window to the scroll area.

dougab
25th December 2007, 01:07
And how do I do that?

dougab
25th December 2007, 19:30
Or I guess to make the "How do I do that?" question clearer - I'm using Designer to create the window and in it's ui_xxx.h file it is creating its own central widget - how do I over-ride that with my own central widget without having to edit the ui header?

Thanks
Doug

marcel
25th December 2007, 19:35
Well, at some point you either have to subclass the UI class or include it in your own class. You can use setCentralWidget to override the central widget sometime before your window is first shown.

Be careful not to delete the previous central widget. The main window will do that.

wysota
25th December 2007, 20:27
If you want your main window to contain a scroll area with something inside, an option might be to promote a QFrame to QScrollArea in Designer, put the contents you want inside the promoted frame and call QScrollArea::setWidget on the contents of the promoted frame inside the widget constructor, right after calling setupUi().