Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [help!] QGraphicsScene background image manipulation - adding the margin next to the background image

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    poorBob
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • P Offline
      P Offline
      poorBob
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        poorBob
        wrote on last edited by
        #3

        Bump, bump :(

        1 Reply Last reply
        0
        • P Offline
          P Offline
          poorBob
          wrote on last edited by
          #4

          Any idea? Anybody?

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            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(); 
            

            }
            @

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved