[solved] link GraphicsSceneItem size to GraphicsView size



  • Hi,

    i want to link the size of some QGraphicsSceneItems to the QGraphicsView where they are displayed.
    Example:
    There are X (e.g. 3) rectangles on the GraphicsView and the GraphicsView has the width Y (e.g. 90).
    I want the rectangles to have the size Y/X (=10).

    Easy right? But i want it to be updated everytime there cames a new Rectangle and when the View size changes.
    Example:
    Button press generates an new rectangle, so now the size of each rectangle needs to be 90/4 = 22,5
    And when the User changes the size of the Window, the size of the View will change, too. So lets say the View is now 100 width, and the rectangles need to be 25 width now.

    Someone an idea how to do this??



  • Create a signal in Inheritance of QGraphicsView, and emit its when it is resizing. After connet the singnal with GraphicsSceneItem.



  • How do you mean that. Do you mean to make an new class, inherit from QGraphicsView and than reimplement void QWidget::resizeEvent to resize the objects, or is there an other way where i don't need a new class??

    ==EDIT==

    I solved it with an custom resize event.

    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    QMainWindow::resizeEvent(event);

    if (anzObjekte == 0)
    	return;
    int widthGesamt = ui->graphicsView->size().width();
    int x(0);
    int breite = (widthGesamt - 10) / anzObjekte;
    
    for(QVector<QGraphicsRectItem*>::iterator it = list.begin(); it != list.end(); it++)
    {
    	(*it)->setRect(x, 0, breite, 40);
    	x += breite;
    }
    

    }

    But now there is the scrollbar still left if you make the window smaller. The objects are all resized correctly, but the scrollbar is still there. How can i remove it??



  • Yes, you can use it

    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    


  • Thank you. That solved it :D
    I need to set a fix scene rect for the view, because it always move the rectangle to the left boarder when i changed the size, but now everything is working :D



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.