PDA

View Full Version : Scroll View for Canvas



Kapil
25th March 2006, 05:46
Hi,

I have created a Canvas....
Now when i set image for it, it goes beyond its size.. I would like to set up a scroll view for it so that the user can scroll and view the entire image even if it is greater than the canvas size...
How do i do that...

Kapil

munna
25th March 2006, 05:52
Are you using Q3Canvas ?

Kapil
25th March 2006, 06:19
Are you using Q3Canvas ?

yes.. i am using Q3Canvas and to set the view, Q3CanvasView..

Kapil

munna
25th March 2006, 06:28
use resizeContents(int width ,int height) to set the size of the Q3CanvasView.

Kapil
25th March 2006, 06:36
use resizeContents(int width ,int height) to set the size of the Q3CanvasView.

Hi,

resizeContents() would resize the canvas view size to that of the image... but i dont need that.. i explicitly want to add a scroll area to it so that the canvas size remains same and the image can be viewed by moving the scroll bar up-down and right-left..

Kapil

munna
25th March 2006, 06:42
i explicitly want to add a scroll area to it so that the canvas size remains same and the image can be viewed by moving the scroll bar up-down and right-left..

Q3CanvasView inherits Q3ScrollView which means that the scrollbars will automatically come if contents size is greater than the viewable area.

Kapil
25th March 2006, 06:49
Q3CanvasView inherits Q3ScrollView which means that the scrollbars will automatically come if contents size is greater than the viewable area.

Yeaps,

It does give the scroll bar if the area goes greater than the canvas...
one more doubt...
Now i will not know the size of the imgae which is getting loaded..
So can it resize the CanvasView dynamically by extracting the size of the image and then calling the resizeContents function with those values...

Kapil

munna
25th March 2006, 06:52
I dont think it can do that. You have to somehow find the size of your canvas and dynamically change the size.

use size() of Q3Canvas to find the size

Cheers

Kapil
25th March 2006, 07:07
I dont think it can do that. You have to somehow find the size of your canvas and dynamically change the size.

use size() of Q3Canvas to find the size

Cheers

hi,

Thanks a lot...

i tried that.. i extracted the size of the image and then was able to resize it and it happened... but it is giving improper view...
the scroll bars comes and it resets the entire window to the size of the image but when i move the scroll bars up and down, they get disappeared... though the functionality of scrollbar is happening but they are not visible...

why does this happens...

munna
25th March 2006, 07:14
can i see the code where you are doing this?

Kapil
25th March 2006, 07:19
can i see the code where you are doing this?

Here is the file:

Its the open() function which does the functioning.




#include "imagezoomer.h"
#include <QtGui>
#include <qdir.h>
#include <QColor>

ImageZoomer::ImageZoomer(Ui::MainWindow *_mwin): mwin(_mwin)
{

frame = new QFrame(mwin->centralwidget);
frame->setGeometry(QRect(60, 70, 591, 571));
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Plain);

canvas = new Q3Canvas(500,500);
canview = new Q3CanvasView(frame);
canview->setCanvas(canvas);
canvas->setBackgroundColor(Qt::black);

resize(500, 400);
connect(mwin->actionOpen, SIGNAL(activated()), this, SLOT(open()));

}
ImageZoomer::~ImageZoomer()
{
}

void ImageZoomer::open()
{
int x,y;
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),QDir::currentPath());

if (!filename.isEmpty())
{
QImage image(filename);
if (image.isNull())
{
QMessageBox::information(this, tr("Image Zoomer"),tr("Cannot load %1.").arg(filename));
return;
}
canvas->setBackgroundPixmap(QPixmap::fromImage(image));
scalefactor = 1.0;

x = image.width();
y = image.height();

canview->resizeContents(x,y);

}
}