elastic QGraphicsItem (layout problem)
Hy
I need a elastic layout so that when you stretch the mainwindow all items in it also stretch.
I have:
1) QMainWindow
2) GraphicsView
3) GraphicsItem on the Scene
Currently i have a grid layout added to my class.
My GraphicsView i set to expanding in the designer so that's good. But i have a QGraphicsItem circle that does not expanding together with his parent.
What i need is when you make window smaller, item also smaller. When you make window bigger item also bigger. How can i acomplish this? I'm searching and reading some hour or so and can't find it.
I'm not sure what i'm looking for, but i thought that something like this in the constructor i need:
pseudo-code:
this_constructed_object->stretch_with_parent_QGraphicsView
i don't really know...but i think that i need something like that in the constructor of my class:
Re: elastic QGraphicsItem (layout problem)
It will not be solved using a custom layout.
You may need to subclass QGraphicsView and override its resizeEvent(). In that, you need to zoom/scale the view so each item will be scaled respectively.
Re: elastic QGraphicsItem (layout problem)
Thanks, i understand what you'r saying, but is there a tutorial covering something similair maybe?
Re: elastic QGraphicsItem (layout problem)
Hi,
I dont think that your problem is related to the constructor of your item.
In fact, you want a no conventionnal display of your QGraphicsView. Remember that is only a view of a QGraphicsScene.
So, what do you want to do exactly? When your view is resized, do you changes the positions and size of your items in the scene? Or you just want that the scene rendering will stretch the size of the view?
Not quite same needs, isn't it?
And what about moving in the scene? Don't you need that features?
Anyway, have a look at QGraphicsView::transform and QGraphicsView::scaleDont forget, if you scale the view according to its rect size, to reset the transformation matrix because scale() scales the view form current matrix.
I hope I was helpful.
Re: elastic QGraphicsItem (layout problem)
Please see this:
Code:
#include <QApplication>
#include <QDialog>
#include <QLayout>
#include <QGraphicsView>
{
public:
{}
protected:
{
this->scale(x,y);//DO SOME WORK HERE
}
};
int main(int argc, char **argv)
{
MyView mv(&dlg);
mv.setScene(&sc);
sc.addRect(10,10,200,200);
vl->addWidget(&mv);
dlg.show();
return app.exec();
}
You just need to calculate the scaling factor depending upon the current size of gv.
Pass that scaling factor in scale function of gv in resizeEvent.
Re: elastic QGraphicsItem (layout problem)
@Scasio:
http://lookpic.com/i/395/bGQxNt0B.jpeg
...
http://lookpic.com/i/856/NcrNsyzh.jpeg
Btw.
Yes, thx for the info but i haven't understood everything. I just need a simple way to resize items accordingly to the window (parent)
Re: elastic QGraphicsItem (layout problem)
Re: elastic QGraphicsItem (layout problem)
Quote:
Originally Posted by
yogeshgokul
You just need to calculate the scaling factor depending upon the current size of gv.
Pass that scaling factor in scale function of gv in resizeEvent.
Thanks yogeshgokul i will do some simple examples to figure it out.
It's different because in my app i have different setup.
here i go:
Code:
//Program entry point: instancing object of RuletV1 class
int main(int argc, char *argv[])
{
RuletV1 w;
w.show();
return a.exec();
}
Code:
//RuletV1.cpp
RuletV1
::RuletV1(QWidget *parent, Qt
::WFlags flags
)
ui.setupUi(this);
ui.graphicsView->setScene(scena);
m_elipsa = new CircleItem();
scena->addItem(m_elipsa);
} RuletV1::~RuletV1(){}
Code:
//CirceItem.cpp
//destructor
CircleItem::~CircleItem() {}
QRectF CircleItem
::boundingRect() const { return QRectF(0,
0,
100,
100);
}
{
painter
->setRenderHint
(QPainter::Antialiasing);
painter->drawLine(0,50, 100, 50);
painter->drawLine(50, 0, 50, 100);
painter->drawEllipse(0, 0, 100, 100);
}
If i understand then you have also GraphicsView but not like me, you make yourself a class that inherits QGraphicsView, you named it MyView. And in that class you override a method called resizeEvent.
The rest i understand...i need to study that resizeEvent method and how to calculate scaling. Thx
Re: elastic QGraphicsItem (layout problem)
Quote:
Originally Posted by
yogeshgokul
Have you tried my way ?
i'm trying -> will post result later
Re: elastic QGraphicsItem (layout problem)
You only need to compute the right scale factor, according to the size of the widget,.
try something like this :
Code:
void YourGraphicsView
::resizeEvent ( QResizeEvent * event
) {
const QSize& oldSize = event->oldSize();
const QSize& newSize = event->size();
//be aware of integer division and division by zero
double scaleFactorX = (oldSize.width ()>0) ? ((double)newSize.width())/oldSize.width ():1;
double scaleFactorY = (oldSize.height()>0) ?((double)newSize.height())/oldSize.height():1;
//sclae the view
scale(scaleFactorX, scaleFactorY);
}
But I am not sure that the behaviour will be correct for all cases.