PDA

View Full Version : QFrame within QScrollArea



CSwanepoel
8th May 2011, 22:52
I can't find a way to get the scroll bar to work with QFrame. My class is derived from QFrame and I display a couple of images within it. I want to add a scrollbar as soon as the images gets to many. I tried a lot of examples without success for my particular design


Monitors::Monitors(QWidget *parent)
: QFrame(parent)
{
for (int i = 0; i < 4; i++)
{
QLabel *MonitorIcon = new QLabel(this);
QPixmap pic = QPixmap("C:/DevelopmentWork/TestFrame/test.png");

QPainter paint;
paint.begin(&pic);
paint.drawPixmap(225, 162, pic);
paint.end();

MonitorIcon->setPixmap(pic);

if (i < 2)
MonitorIcon->move(20 + (i * 100), 20);
else
MonitorIcon->move(20 + ((i -2) * 100), 20 + 100);

MonitorIcon->show();
MonitorIcon->setAttribute(Qt::WA_DeleteOnClose);
}
}

The following example doesn't work on my class either http://discussion.forum.nokia.com/forum/showthread.php?193621-How-to-put-Scroll-Bar-in-QFrame

wysota
8th May 2011, 23:06
You forgot to use layouts.

ChrisW67
8th May 2011, 23:11
The example you link to works as advertised. A scroll bar appears when the content of the QScrollArea is larger than the space available to display that content.

Tell us what you have done to try and get a scroll bar? Nothing in this code suggests you have tried anything. (It also suggests that you have not grasped the notion of layouts: if you want a 2x2 grid of labels then you should consider QGridLayout.)

CSwanepoel
9th May 2011, 00:19
Thanks Chris, you're right.

I got it to work by using the following code, similar to the example.
However I'd to calculate the size of my widget depending on the total images I added.
By doing a resize within my widget the scrollbars works fine.



QHBoxLayout* MYCLASS::CreateMonLayout()
{
QHBoxLayout *sLayout = new QHBoxLayout();

MonWidget *screenW = new MonWidget();
screenW->setParent(NULL);

// Assign Widget to scroll area to allow scrolling
QScrollArea *scroll = new QScrollArea;
scroll->setWidget(screenW);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
sLayout->addWidget(scroll);

return sLayout;
}


I'd tried 3 days to get it work automatically with layouts, as suggested by wysota but it ends up the layout will control the size of my widget and result in the images just being cut. It would be great to know if there's a better way though but I'd to move one. I wasted to much time on this one

Thanks for your help.