PDA

View Full Version : widget qgraphicsview scaling



mistertoony
5th March 2007, 16:38
i am a new user to qt. i would like to be able to scale my current window only horizontally if possible. if users drag the corner it would only allow horizontal changes, with this i would like to know how to make the button and graphicsview to scale and move accordingly. if anyone could post some general help to get me started or point me in the direction of a definitive resource. here is what i have so far:

http://www2.bc.edu/~nguyenah/sff_viewer.JPG

Claymore
5th March 2007, 21:57
Have you tried reimplementing QWidget::resizeEvent ( QResizeEvent * event ) (http://doc.trolltech.com/4.2/qwidget.html#resizeEvent) for your main window? In doing so, you could just reset the vertical size to the current vertical size of the application, thus "ignoring" it.

mistertoony
6th March 2007, 17:05
hi thanks for the reply. i looked at the doc and the scribble example but im still having trouble implementing it into my program. would anyone be able to help me or provide a simple example?

here's a snippet from the scribble example:


void ScribbleArea::resizeEvent(QResizeEvent *event)
{
if (width() > image.width() || height() > image.height()) {
int newWidth = qMax(width() + 128, image.width());
int newHeight = qMax(height() + 128, image.height());
resizeImage(&image, QSize(newWidth, newHeight));
update();
}
QWidget::resizeEvent(event);
}

and:


void ScribbleArea::resizeImage(QImage *image, const QSize &newSize)
{
if (image->size() == newSize)
return;

QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(qRgb(255, 255, 255));
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), *image);
*image = newImage;
}

but i'm not sure how this applies in my case. im trying to scale the graphicsview and button in my widget with changes to the main window. any help please? this is what i tried to do??? what functions do i need to access to make the size changes to the graphicsview?


void scalingApp::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
//scene.setSceneRect(0, 0, width()-4, height()-4);
graphicsView->resize(width(),height());
}

mistertoony
6th March 2007, 17:26
i used this code:


void scalingApp::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
graphicsView->resize(width(),220);
}

this allows the graphicsview to only be resize'ed horizontally. however, the button is fixed and doesnt move to the right like i would like it to. (the button is situated on the bottom within a horizontal layout including the button and a horizontal spacer) any hints/help?

http://www2.bc.edu/~nguyenah/scaling1.JPG

mistertoony
6th March 2007, 18:17
okay... i am using the following code in attempt to get the button to relocate correctly, but things are not working out.


void scalingApp::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
int extraWidth = width() - minimumWidth();
pushButton->setGeometry(303 + extraWidth,1,75,24);
graphicsView->resize(width(),220);
}

as i increase the window's horizontal side the button appears to get more and more cut off, i dont know why this is happening. i tried playing around with the sizePolicy but this hasn't worked. any help?

http://www2.bc.edu/~nguyenah/scaling2.JPG

mistertoony
7th March 2007, 16:35
does anyone have any idea?

giverson
7th March 2007, 17:25
I would use a QGridLayout.

Use a 2 X 2 display.

Span the View across the entire width on the top row and set the columnStrech property on the 1st column to some absurd number like a million and then columnStrech property on the 2nd column to 1.

As the window resizes horizontally, the grid should autoexpand the 1st column and only increase your push button's 1/millionth of a pixel for every pixel it is expanded and keeping it right aligned. (you can also try setting that stretch to 0 but I get weird results with that in PyQt).

rajesh
8th March 2007, 08:10
use the following to scale image horizontaly
graphicsView->scale(dx,dy); --> if dy is 1 then it will scale only horizontaly.
graphicsView->scale(1.25,1); --> increase image horizontaly
graphicsView->scale(0.8,1); --> decrease image horizontaly

mistertoony
20th March 2007, 15:36
is there a way to scale, QGraphicsPathItem?

giverson
20th March 2007, 16:57
link to all members of this class...

http://doc.trolltech.com/4.2/qgraphicspathitem-members.html

http://doc.trolltech.com/4.2/qgraphicsitem.html#scale

Bitto
20th March 2007, 23:46
You can very easily scale a path, but it's usually wrong; scaling the view is the right thing to do in most cases.

To scale a path, simply call QMatrix::map(). So, something like:



QMatrix matrix;
matrix.scale(2, 1);
QPainterPath scaledPath = matrix.map(originalPath);


In Qt 4.3, you can also use QTransform.