[help!] QGraphicsScene background image manipulation - adding the margin next to the background image



  • Hello Qt deveopers :).

    I have some problems regarding the QGraphicsScene image manipulation.

    However, before describing my main problem I would like to ask You about different issue: some time ago I had registered here and today I wanted to log-in with that old account. I forgot my password so I followed the "password reseting" procedure, but it failed. I received an email with a new temporary pass but after typing it Qt-project web shown the login error... I reseted the password for 4 times but unsuccessfully... Do You know how to fix this?

    Regarding my problem with the image: I have created the scene, view and some items placed on the scene. I set the background image with following way:

    @
    QString m_strBackgroundPath("/home/pic.jpg");
    QImage oImage( m_strBackgroundPath );
    m_pScene->setSceneRect( oImage.rect() );
    m_pScene->setBackgroundBrush( oImage );
    @

    So far everything is fine: the picture is painted in the background, the items are painted/shown "on" this picture and everything is presented in the view.

    But, sometimes, for certain items I would like to show a little information in the right side of the view. The information is shown in some-kind of information widget. The problem is that this information widget may overlap the item. In such case I would like to move the whole scene with all items to the left, so the focused item would be still visible. I figured out that in the right side of the scene I can add some margin and then "move" the view. I tried to extend the scene rectangle to the right with the following code:

    @
    QRectF myGraphicsScene::sceneRect() const
    {
    const QRectF oSceneRect = QGraphicsScene::sceneRect();
    QRectF oAdjRect(oSceneRect);
    oAdjRect.adjust(0,0,150,0);
    qDebug() << "CPTGraphicsScene::sceneRect()" << oSceneRect << oAdjRect;
    return oAdjRect;
    }
    @

    and draw the margin this way:
    @void myGraphicsScene::drawBackground(QPainter* pPainter, const QRectF& rRect)
    {
    QBrush oBrush = backgroundBrush();

    QRectF oRect( rRect.x()-rRect.width(), rRect.y()
    , 150, rRect.height() );
    qDebug() << "CPTGraphicsScene::drawBackground!" << rRect << sceneRect() << oRect;

    if ( oBrush.style() != Qt::NoBrush )
    {
    pPainter->save();
    pPainter->setBrushOrigin(0, 0);
    pPainter->fillRect(rRect, oBrush);
    pPainter->fillRect( oRect, Qt::red );
    pPainter->restore();
    }
    }//END drawBackground()@

    Value "150" is just for tests here ;). Unfortunately this doesn't work... :(

    Do You have any idea what I'm doing wrong...

    I would be grateful for any hind and advice.

    Robert :)



  • Hello, hello again :). Do You have any idea?



  • Bump, bump :(



  • Any idea? Anybody?


  • Moderators

    the rRect parameter is the exposed rectangle in the scene. I believe you want the background image as the scene background, meaning it should scroll with the items?

    try this:
    @
    void myGraphicsScene::drawBackground(QPainter* pPainter, const QRectF& rRect)
    {
    QBrush oBrush = backgroundBrush();
    QRectF sceneRect = this->sceneRect();

     pPainter->save();
         pPainter->setClipRegion( QRegion( rRect.toRect() ) );   //just some optimization
         pPainter->fillRect( sceneRect, Qt::red );  //you may optimize this line
         pPainter->fillRect( sceneRect.translated(-150,0), oBrush );
     pPainter->restore(); 
    

    }
    @


Log in to reply
 

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