PDA

View Full Version : Scrolling Contents in QScrollArea



sujan.dasmahapatra
2nd February 2011, 07:01
Dear Friends
I am using a QScrollArea which I can see some contents in it. By using the following code snippet I am able to see both the vertical and horizontal scrollbar. I am also implementing the virtual function


#include <QScrollArea>

Class PageForGraph : public QScrollArea
{
Q_OBJECT
Public:
PageForGraph(QWidget *parent=0);
virtual ~PageForGraph();
};

PageForGraph::PageForGraph(QWidget *parent)
{
verticalScrollBar()->setRange(0, 2 * desk->height());
horizontalScrollBar()->setRange(0, 2 * desk->width());

}
void PageForGraph::scrollContentsBy(int dx, int dy)
{
widget()->move(widget()->x() + dx, widget()->y() + dy);
const int x = horizontalScrollBar()->value() + dx;
horizontalScrollBar()->setValue( x );
const int y = verticalScrollBar()->value() + dy;
verticalScrollBar()->setValue( y );
update();
}
But when I moving the scrollbar my contents are not moving. Any help would be highly appreciated. Thanks sujan

Lykurg
2nd February 2011, 07:17
So why in hell you have reimplementated scrollContentsBy()? And what is desk? Please send a complete code. Furthermore how do you setup the scroll area?

And use [code] tags!

sujan.dasmahapatra
2nd February 2011, 08:32
Hi Lykurg

desk = new QDesktopWidget;

desk is a QDesktopWidget.

I think for scrolling we need to reimplement scrollContentsBy().
I want the contents in the QScrollArea to move as you scroll. How can I achieve this.

Lykurg
2nd February 2011, 08:53
I think for scrolling we need to reimplement scrollContentsBy().
No you don't. And by the way, why you are subclassing QScrollArea? What are you trying to acheive?

I want the contents in the QScrollArea to move as you scroll. How can I achieve this.That works yout of the box. Probably you make something wrong during setup and/or when adding the widget to the scroll area.

wysota
2nd February 2011, 10:39
desk = new QDesktopWidget;

desk is a QDesktopWidget.
By the way, this has no chance on working. You can't instantiate a new desktop :)

sujan.dasmahapatra
2nd February 2011, 10:42
Hi
Thanks for reply. See I have done that for example.
I have subclassed QScrollArea for some other functionalities , such as printing etc. See the code snippet below


class MyScrollArea : public QScrollArea
{
Q_OBJECT
Public:
MyScrollArea(QWidget *parent=0);
~MyScrollArea();
};

MyScrollArea::MyScrollArea()
{
QPushButton *button1, *button2;
button1 = new QPushButton(this);
button2 = new QPushButton(this);

setWidget(button1);
setWidget(button2);
}

I want the buttons to one on extreme left and one on extreme right so that I need to scroll them to see.
This much is my code...But I am not able to see the scrollbars on the scrollarea and not able to scroll even. Please tell me what Can be done on this. Thanks sujan

wysota
2nd February 2011, 11:11
If you set one widget on the scroll area and then you set another then the latter replaces the former. QScrollArea handles one widget. If you want more, place them all into a single widget and set that widget on the scroll area. Also pay attention to what the "widgetResizable" property does.