Hello !

I'm still working on my mathematical function plot program and I still
have problems to make it work as expected.

Thanks to Eskil, I found the ItemIgnoresTransformations flag that permit
to make some items (especially text items) ignoring scale transformations.
But item positioning is still a bit confusing for me. Especially when view
is resized.

I wrote a small snippet to explain my problem. The code below should
display an ellipse and a rectangle fitting the view (thanks to fitInView()
method). I'd also like to display a label displaying rectangle height at
the top left corner outside the rectangle. To do this, once all my objects
are created, I set the label position using :

label.setPos(scene.sceneRect().left(), scene.sceneRect().top());

This works fine except the label is displayed inside the rectangle. To
display it outside the rectangle, I substract the width of the label from
the x coordinate of its position. So the previous line becomes :

label.setPos(scene.sceneRect().left()-label.boundingRect().width(),
scene.sceneRect().top());

My label is now positioned outside the rectangle, but when I resize the
window, the distance between the top left corner of the rectangle and the
label is varying. My problem is to make this distance constant. I'd like
to be able to put my label at a constant position relatively to the scene
bounding rectangle.

Here is the full snippet code :
===
package test;

import com.trolltech.qt.core.QSize;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QGraphicsEllipseItem;
import com.trolltech.qt.gui.QGraphicsItem;
import com.trolltech.qt.gui.QGraphicsRectItem;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QGraphicsSimpleTextItem;
import com.trolltech.qt.gui.QGraphicsView;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QResizeEvent;

public class Test extends QMainWindow {
        private QGraphicsScene scene;
        private MyGraphicsView view;

        public static void main(String[] args) {
                QApplication.initialize(args);
                Test test = new Test();
                test.show();
                QApplication.exec();
        }

        public Test(){
                scene = new QGraphicsScene(this);

                QGraphicsEllipseItem ellipse = new QGraphicsEllipseItem(10.0, 
20.0,
250.0, 150.0);
                scene.addItem(ellipse);
                
scene.setSceneRect(scene.sceneRect().united(ellipse.boundingRect()));

                QGraphicsRectItem rect = new QGraphicsRectItem(10.0, 20.0, 
250.0, 150.0);
                scene.addItem(rect);
                
scene.setSceneRect(scene.sceneRect().united(rect.boundingRect()));

                QGraphicsSimpleTextItem label = new
QGraphicsSimpleTextItem(String.valueOf(-scene.sceneRect().top()));
                /*
                 * With this line the label position relative to other items 
change when
window is resized
                 */
                
label.setPos(scene.sceneRect().left()-label.boundingRect().width(),
scene.sceneRect().top());
                /*
                 * With this line the label position relative to other items 
doesn't
change when window is resized
                 */
//      label.setPos(scene.sceneRect().left(), scene.sceneRect().top());
                
label.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIgnoresTransformations,
true);
                scene.addItem(label);
                
scene.setSceneRect(scene.sceneRect().united(label.boundingRect()));

                view = new MyGraphicsView(scene);
                this.setCentralWidget(view);

                this.resize(new QSize(400, 
300).expandedTo(this.minimumSizeHint()));
        }

        class MyGraphicsView extends QGraphicsView {

                public MyGraphicsView(QGraphicsScene scene) {
                        super(scene);
                }

                @Override
                protected void resizeEvent(QResizeEvent event) {
                        super.resizeEvent(event);
                        fitInView(scene.sceneRect(), 
Qt.AspectRatioMode.IgnoreAspectRatio);
                        scale(0.8, 0.8);
                }
        }
}
===

Thanks for any help.

jMax

_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to