Hi All!

I have a problem with QGraphicsScene and it's rect. I have few trees
of GraphicItems, while only one tree is visible at a time. I want to
share single instance of QGraphicsScene for this purpose. But when I
am using scene.setSceneRect(rect); when switching between trees - it
is not working as described in javadoc, no scrollbars are created when
I am moving item off the rectangle. But javadoc says: "i.e., a
rectangle that grows when items are added to or moved in the scene,
but never shrinks", but it never grown either.

Steps to reproduce:
1. Press new and move rectangle somewhre for horizontal scrollbar to appear.
2. Press new and move second item somewhere for vertical scrollbar to appear.
3. Switch to first item and try to move it somewhere down, and
vertical scrollbar will not appear.

Here is the code to reproduce, take a look at comments "NOTE: changing
scene rect"

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
import java.util.ArrayList;
import java.util.List;

public class ScenePositioning {

    public static void main(String[] args) {
        QApplication.initialize(args);
        new ScenePositioning().createAndShowGui();
        QApplication.exec();
    }

    private int count;
    private QGraphicsScene scene;
    private List<GraphItem> items = new ArrayList<GraphItem>();
    private QListWidget listWidget;

    private void createAndShowGui() {
        scene = new QGraphicsScene();
        QGraphicsView view = new QGraphicsView();
        view.setScene(scene);

        QMainWindow mainWindow = new QMainWindow();
        mainWindow.setCentralWidget(view);
        mainWindow.addDockWidget(DockWidgetArea.LeftDockWidgetArea,
createDock());
        mainWindow.show();
    }

    private QDockWidget createDock() {

        QPushButton newButton = new QPushButton("New");
        newButton.clicked.connect(this, "createNew()");

        listWidget = new QListWidget();
        listWidget.currentItemChanged.connect(this,
"currentItemChanged(com.trolltech.qt.gui.QListWidgetItem,
com.trolltech.qt.gui.QListWidgetItem)");

        QLayout layout = new QVBoxLayout();
        layout.addWidget(newButton);
        layout.addWidget(listWidget);

        QWidget widget = new QWidget();
        widget.setLayout(layout);

        QDockWidget dock = new QDockWidget();
        dock.setWidget(widget);
        return dock;
    }

    private void createNew() {
        GraphItem item = new GraphItem();
        int s = items.size();
        items.add(item);
        listWidget.addItem(String.valueOf(++count));
        listWidget.setCurrentRow(s);
    }

    private void currentItemChanged(QListWidgetItem current,
QListWidgetItem previous) {
        if (previous != null) {
            GraphItem item = items.get(listWidget.row(previous));
            item.sceneRect = scene.sceneRect();
            scene.removeItem(item);
        }
        GraphItem item = items.get(listWidget.row(current));
        scene.addItem(item);
        QRectF rect = item.sceneRect;
        if (rect != null) {
            // NOTE: changing scene rect
            scene.setSceneRect(rect);
        } else {
            // NOTE: changing scene rect
            //scene.setSceneRect(scene.itemsBoundingRect());
        }
        scene.update();
    }

    private static class GraphItem extends QAbstractGraphicsShapeItem {

        private QRectF sceneRect;
        private final QRectF bounds = new QRectF(0, 0, 20, 20);

        public GraphItem() {
            setFlag(GraphicsItemFlag.ItemIsMovable, true);
        }

        public QRectF getSceneRect() {
            return sceneRect;
        }

        @Override
        public QRectF boundingRect() {
            return bounds;
        }
        @Override
        public void paint(QPainter painter, QStyleOptionGraphicsItem
option, QWidget widget) {
            painter.fillRect(bounds, GlobalColor.gray);
            painter.setPen(QColor.black);
            painter.drawRect(bounds);
        }
    }

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

Reply via email to