PDA

View Full Version : QScrollArea madness



innerhippy
13th October 2008, 18:11
I'm trying to do something which seems really, really simple, but QT is not playing nicely.

Is is possible in QT to have a QFrame (from QT Designer, so a QDialog parent) inside lives a QScrollArea - into which I add dynamic home-brew widgets arranged as a QVBoxLayout?

I've read may threads which have dummy QWidgets being instantiated, QFrame bring re-parented size klugdes, etc etc.

Is there any decent examples of this anywhere or is this the wrong approach.

Thanks in advance

JimDaniel
13th October 2008, 19:23
Do you want the QFrame to be the scrollable area or contain the scrollable area?

innerhippy
13th October 2008, 20:11
The QFrame should contain the scrollable area - I guess it doesn't need to be a QFrame necessarily..

JimDaniel
13th October 2008, 20:43
Not quite sure what you're looking for. Maybe this example will help you out. Here the scroll area is set to the dialog, but you could easily enough set it to a QFrame or any other QWidget.



#ifndef SCROLLTEST_H
#define SCROLLTEST_H

#include <QtGui>

class ScrollTest : public QDialog
{
Q_OBJECT

public:
ScrollTest(QWidget * parent = 0);
virtual ~ScrollTest();
};

#endif // SCROLLTEST_H

#include "scrolltest.h"

ScrollTest::ScrollTest(QWidget * parent) : QDialog(parent)
{
QWidget * scroll_widget = new QWidget();

QVBoxLayout * scroll_layout = new QVBoxLayout();
for(int i = 0; i < 25; i++)
{
QPushButton * button = new QPushButton("ScrollTest");
scroll_layout->addWidget(button);
}

scroll_widget->setLayout(scroll_layout);

QScrollArea * scroll_area = new QScrollArea();
scroll_area->setWidget(scroll_widget);

QVBoxLayout * main_layout = new QVBoxLayout();
main_layout->addWidget(scroll_area);

setLayout(main_layout);
}

ScrollTest::~ScrollTest()
{}

#include <QApplication>
#include "scrolltest.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ScrollTest st;
st.show();
return a.exec();
}

innerhippy
13th October 2008, 20:58
Works a treat - many thanks!