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. Understanding the Mapping from View Coordinates to Scene Coordinates

Understanding the Mapping from View Coordinates to Scene Coordinates

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Balajee
    wrote on last edited by
    #1

    I was expecting the following snippet of code to show me 4 squares on each corner of my monitor (whose resolution is 1920x1080) on a frameless and fullscreen QGraphicsView that occupies the entire monitor's framebuffer:

    @int main(int argc, char **args)
    {
    QApplication app(argc, args);

    const int monitorWidth = 1920;
    const int monitorHeight = 1080;
    const int rectDim = 40;

    QGraphicsView view;
    view.setWindowFlags(Qt::FramelessWindowHint);
    view.setGeometry(0, 0, monitorWidth, monitorHeight);
    view.showFullScreen();
    
    QGraphicsScene scene;
    scene.setBackgroundBrush(Qt::black);
    scene.setSceneRect(QRectF(0.f, 0.f, monitorWidth, monitorHeight));
    view.setScene(&scene);
    
    //Create 4 rectangle items
    QGraphicsRectItem items[4];
    
    //top left corner
    items[0].setRect(QRect(0, 0, rectDim, rectDim));
    items[0].setPen(QPen(Qt::red));
    items[0].setBrush(Qt::red);
    
    //top right corner
    items[1].setRect(QRect(monitorWidth-rectDim, 0, rectDim, rectDim));
    items[1].setPen(QPen(Qt::red));
    items[1].setBrush(Qt::red);
    
    //bottom left corner
    items[2].setRect(QRect(0, monitorHeight-rectDim, rectDim, rectDim));
    items[2].setPen(QPen(Qt::red));
    items[2].setBrush(Qt::red);
    
    //bottom right corner
    items[3].setRect(QRect(monitorWidth-rectDim, monitorHeight-rectDim, rectDim, rectDim));
    items[3].setPen(QPen(Qt::red));
    items[3].setBrush(Qt::red);
    
    for(unsigned int i=0; i<4; ++i)
    {
        scene.addItem(items+i);
    }
    
    return app.exec();
    

    } @

    I find that I get scroll bars in my QGraphicsView which appear when the QGraphicsScene Rect is out of bounds of the QGraphicsView viewport. However, as shown in the code above, both match exactly. Even if I hide the scroll bars using:

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

    I find that the bottom and right squares are clipped incompletely meaning that there is still some part of QGraphicsScene which is not visible in the QGraphicsView. Why is this the case?

    Clearly, my confusion is in the mapping of scene coordinates to that in view. Despite reading the information on "coordinate systems in the QGraphicsView":http://qt-project.org/doc/qt-4.8/graphicsview.html#the-graphics-view-coordinate-system reference, I am unable to figure out what is going wrong. Please help.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, welcome to Qt forums,

      Seems what you are looking for is:
      @
      view.setFrameShape(QGraphicsView::NoFrame);
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Balajee
        wrote on last edited by
        #3

        Yes, that seems to fix the issue, thanks. That also explains a 2 pixel difference observed between the view coords and the scene coords which if I set as below:

        @scene.setSceneRect(QRectF(0.f, 0.f, monitorWidth-2, monitorHeight-2));@

        the toolbars and the clipping disappeared even with the old code.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4
          1. QGraphicsView is subclass of QAbstractScrollArea, which is subclass of QFrame.
          2. QGraphicsScene is shown on the viewPort() of QAbstractScrollArea
          3. Frame is between the viewPort() and QAbstractScrollArea
          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