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. Exact correct size needed

Exact correct size needed

Scheduled Pinned Locked Moved Solved General and Desktop
33 Posts 7 Posters 4.4k 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #1

    I have, and want:

    • Top-level QWidget window it has standard title/furniture).
    • QGraphicsView which fills the whole QWidget content area.
    • QGraphicsPixmapItem which fills the whole QGraphicsView.
    • The QGraphicsPixmapItem rescales its QPixmap content on window resize, and must maintain aspect ratio.

    In layman's terms, a keep-aspect pixmap to fill the whole of the window. With no scrolling, so sized to fit correctly/perfectly.

    My code is like:

    class MapWindow : public QWidget;
    
    void MapWindow::setupUi()
    {
        // keep original map size for rescaling quality
        this->pixmapOriginalMap = new QPixmap(UIUtils::applicationDocsDirectory() + "/Map.jpeg");
    
        // create scene & view
        this->scene = new QGraphicsScene(this);
        this->view = new QGraphicsView(scene);
    
        // I seem to have to have a layout
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->setMargin(0);
        layout->addWidget(view);
    
        // create the map as a `QGraphicsPixmapItem`
        // its size will be set in `resizeEvent()`
        this->pixmapMap = new QGraphicsPixmapItem();
        scene->addItem(pixmapMap);
    }
    
    /*virtual*/ void MapWindow::resizeEvent(QResizeEvent *event) /*override*/
    {
        QSize size(event->size());
        this->pixmapMap->setPixmap(pixmapOriginalMap->scaled(size.width(), size.height() - 2, Qt::KeepAspectRatio));
    }
    

    My question is what/whose width/height should I actually be using in resizeEvent to scale the pixmap? Can you see have size.height() - 2 there presently?! Without that, I get just too big a height, and it causes a vertical scrollbar for just 2 extra pixels. And I end up with bigger scrollers, horizontal as well as vertical, as I resize e.g. to smaller.

    So what here should I be using to get exact size right for what I want, please?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ollarch
      wrote on last edited by
      #11

      Hi,

      The view is the one that takes care of the display part. What it does is show the scene with the indicated transformations. So, let the view do its job.
      The view is not resizing, it only paints the scene and does not modify the scene. So, as I told you, the pixmap is still the same, what have changed is where are you looking it.

      Think on a CAD software. The schematic have to keep the units regardless of the zoom applied or the rotation of the view. You can rotate the view, zoom it, ... but when you ask the view to map the mouse position into scene coordinates, this coordinates will still be the same pixel position.

      Start with points 1 and 2. When you have it working, do the point 3. Finally point 4.
      When you finish all this steps, call "rotate(90)" on your view and see what happens (what you see and the mouse positions).

      JonBJ 2 Replies Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        Can you try something like
        view->geometry().width()
        and see what that reports ?

        JonBJ 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Can you try something like
          view->geometry().width()
          and see what that reports ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @mrjj
          This comes out view->geometry().size() == event->size() always, when first called and as I drag window smaller/larger.

          Playing with resizing now, I see that the smaller I make the window the larger the non-visible area becomes, i.e. I get scrollbars on both vertical and horizontal with the "empty" areas of the bar to scroll into getting larger. So, I make that.... event->size() is too large and gets proportionately larger as the window gets smaller? Which might well correspond to a fixed amount which needs to be subtracted from event->size()? Like some margin or border? Or I might be getting my math confumbulated over this....

          P.S.
          A useful discovery? If I comment out KeepAspectRatio that does not solve but the scrollbars are slightly different. For example, where it starts out fitting with my numbers with KeepAspectRatio I actually get scrollers with a bit missing when not. That implies to me that we have some issue about the QPixmap/scaling to take into account??

          Pl45m4P 1 Reply Last reply
          0
          • JonBJ JonB

            @mrjj
            This comes out view->geometry().size() == event->size() always, when first called and as I drag window smaller/larger.

            Playing with resizing now, I see that the smaller I make the window the larger the non-visible area becomes, i.e. I get scrollbars on both vertical and horizontal with the "empty" areas of the bar to scroll into getting larger. So, I make that.... event->size() is too large and gets proportionately larger as the window gets smaller? Which might well correspond to a fixed amount which needs to be subtracted from event->size()? Like some margin or border? Or I might be getting my math confumbulated over this....

            P.S.
            A useful discovery? If I comment out KeepAspectRatio that does not solve but the scrollbars are slightly different. For example, where it starts out fitting with my numbers with KeepAspectRatio I actually get scrollers with a bit missing when not. That implies to me that we have some issue about the QPixmap/scaling to take into account??

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @JonB

            So you don't want scrollbars?! Try a fixed scene (that fits in your view and widget)

            https://doc.qt.io/qt-5/qgraphicsscene.html#sceneRect-prop

            Then resize your pixmap according to your window/view


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            JonBJ 1 Reply Last reply
            2
            • Pl45m4P Pl45m4

              @JonB

              So you don't want scrollbars?! Try a fixed scene (that fits in your view and widget)

              https://doc.qt.io/qt-5/qgraphicsscene.html#sceneRect-prop

              Then resize your pixmap according to your window/view

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @Pl45m4
              I don't think I understand you at all! Or, I'm already doing that?

              I do not want scrollbars, but only in the sense that they should not be needed because everything fits precisely, not that there is to be anything out of view.

              The actual pixmap turns out to be 6057 x 3824. I want it to always fit in the gfx view perfectly (scaled, keeping ratio), i.e. nothing scrolled out of view, as I resize the window. The window is supposed to be a widget, which is totally filled by the one QGraphicsView, which in turn is totally filled by the one QGraphicsPixmapItem.

              You can see I am acting on the window widget's resizeEvent, and rescaling the pixmap (with keep ratio), which is what I understood I need to do, perhaps from reading around.

              But, as I said, I am new to Qt graphics so are you referring me to something I do not understand/know is available?

              Pl45m4P 1 Reply Last reply
              0
              • JonBJ JonB

                @Pl45m4
                I don't think I understand you at all! Or, I'm already doing that?

                I do not want scrollbars, but only in the sense that they should not be needed because everything fits precisely, not that there is to be anything out of view.

                The actual pixmap turns out to be 6057 x 3824. I want it to always fit in the gfx view perfectly (scaled, keeping ratio), i.e. nothing scrolled out of view, as I resize the window. The window is supposed to be a widget, which is totally filled by the one QGraphicsView, which in turn is totally filled by the one QGraphicsPixmapItem.

                You can see I am acting on the window widget's resizeEvent, and rescaling the pixmap (with keep ratio), which is what I understood I need to do, perhaps from reading around.

                But, as I said, I am new to Qt graphics so are you referring me to something I do not understand/know is available?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #6

                @JonB

                From my experience, you do get scrollBars as soon as your content is close to the border of your scene.
                And because you don't seem to set any (fixed) sceneRect, you always will. (AFAIK, 1 or 2 px difference is still enough to trigger the scrollBars)

                From sceneRect doc:
                "If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks)."

                -> Because your sceneRect never shrinks, your non-visible area will grow when you make your widget bigger and then decrease the size again. (Your Pixmap shrinks, but your empty, blank scene doesn't. This is why you have scrollBars)

                So I don't know if this will help you, but I would try to resize the scene with your widget as well.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                JonBJ 2 Replies Last reply
                3
                • Pl45m4P Pl45m4

                  @JonB

                  From my experience, you do get scrollBars as soon as your content is close to the border of your scene.
                  And because you don't seem to set any (fixed) sceneRect, you always will. (AFAIK, 1 or 2 px difference is still enough to trigger the scrollBars)

                  From sceneRect doc:
                  "If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks)."

                  -> Because your sceneRect never shrinks, your non-visible area will grow when you make your widget bigger and then decrease the size again. (Your Pixmap shrinks, but your empty, blank scene doesn't. This is why you have scrollBars)

                  So I don't know if this will help you, but I would try to resize the scene with your widget as well.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @Pl45m4 said in Exact correct size needed:

                  AFAIK, 1 or 2 px difference is still enough to trigger the scrollBars

                  Indeed. But while it starts out like that, when the window is "large", as I make the window "small" the unscrolled area increases to like half the total width or height. And that unscrolled area is just empty space, it's not my pixmap that fills it, the pixmap looks right and fits. We are not talking about "a few pixels".

                  -> Because your sceneRect never shrinks, your non-visible area will grow when you make your widget bigger and then decrease the size again.

                  So I don't know if this will help you, but I would try to resize the scene with your widget as well.

                  I'm looking into this. I know about the "sceneRect never shrinks". Yep, I'm thinking about this now, I do see what you mean. It won't solve why it's not quite right initially --- though, as you say, that might be the "few pixels accuracy" --- but that would make sense that any shrink would then have scrollbars.... Lemme have a play :)

                  1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @JonB

                    From my experience, you do get scrollBars as soon as your content is close to the border of your scene.
                    And because you don't seem to set any (fixed) sceneRect, you always will. (AFAIK, 1 or 2 px difference is still enough to trigger the scrollBars)

                    From sceneRect doc:
                    "If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks)."

                    -> Because your sceneRect never shrinks, your non-visible area will grow when you make your widget bigger and then decrease the size again. (Your Pixmap shrinks, but your empty, blank scene doesn't. This is why you have scrollBars)

                    So I don't know if this will help you, but I would try to resize the scene with your widget as well.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Pl45m4
                    Looks like I owe you a beer :)

                    /*virtual*/ void MapWindow::resizeEvent(QResizeEvent *event) /*override*/
                    {
                        const QSize &size(event->size());
                        // to avoid scrollbars, scene must be resized, and needs to be "a little bit" smaller
                        int width = size.width() - 2;
                        int height = size.height() - 2;
                        scene->setSceneRect(0, 0, width, height);
                        // scale the pixmap to fit, keeping aspect ratio
                        const QPixmap &pixmap(pixmapOriginalMap->scaled(width, height, Qt::KeepAspectRatio));
                        pixmapMap->setPixmap(pixmap);
                    }
                    

                    This works much better! I was confusing the pixel issue and the need to resize scene smaller on shrink.

                    Those - 2s are magic :) Without I get scrollbar. There should be a Qt constant for this :)

                    1 Reply Last reply
                    1
                    • O Offline
                      O Offline
                      ollarch
                      wrote on last edited by
                      #9

                      Hi,

                      You have to understand what is the scene and what is the view. In your case, the scene is in pixel units. What the view does is show the scene (or part). The view can show the pixmapItem zoomed but the item size in scene units is still the same.

                      So, as I posted on the other topic:

                      1. Add the pixmapItem to the scene.
                      2. Call "fitInView(pixmapItem,Qt::KeepAspectRatio)
                      3. Override the "resizeEvent" of your Main Widget or your Main Window and call the "fitInView" there.
                      4. If you want to get the mouse position: create a derived QGraphcisView, and you can do something like this:
                      void MyGraphicsView::mouseMoveEvent(QMouseEvent *pqEvent)
                      {
                      	QGraphicsView::mouseMoveEvent(pqEvent);
                      	QPointF qScenePos = mapToScene(pqEvent->pos());
                      	qint32 qX = qScenePos.x();
                      	qint32 qY = qScenePos.y();
                      	emit position(qX,qY);
                      }
                      

                      This takes the GraphicsView position and maps it into the scene coordinates, in this case your image pixel coordinate.
                      Note that you have to use the MyGraphicsView class instead of QGraphicsView class. Also I have added the SIGNAL "position"

                      JonBJ 1 Reply Last reply
                      1
                      • O ollarch

                        Hi,

                        You have to understand what is the scene and what is the view. In your case, the scene is in pixel units. What the view does is show the scene (or part). The view can show the pixmapItem zoomed but the item size in scene units is still the same.

                        So, as I posted on the other topic:

                        1. Add the pixmapItem to the scene.
                        2. Call "fitInView(pixmapItem,Qt::KeepAspectRatio)
                        3. Override the "resizeEvent" of your Main Widget or your Main Window and call the "fitInView" there.
                        4. If you want to get the mouse position: create a derived QGraphcisView, and you can do something like this:
                        void MyGraphicsView::mouseMoveEvent(QMouseEvent *pqEvent)
                        {
                        	QGraphicsView::mouseMoveEvent(pqEvent);
                        	QPointF qScenePos = mapToScene(pqEvent->pos());
                        	qint32 qX = qScenePos.x();
                        	qint32 qY = qScenePos.y();
                        	emit position(qX,qY);
                        }
                        

                        This takes the GraphicsView position and maps it into the scene coordinates, in this case your image pixel coordinate.
                        Note that you have to use the MyGraphicsView class instead of QGraphicsView class. Also I have added the SIGNAL "position"

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @ollarch
                        Thank you for joining this discussion!

                        QGraphcisView::fitInView(), yes I remembered that vaguely.

                        I have questions about what you are saying:

                        • I looked through articles and they said for best quality scaling of pixmaps I should do it via the QPixmap::scaled() I am using [I am struggling to find references for this now...]. Isn't your fitInView(pixmapItem,Qt::KeepAspectRatio) way going to "lose me quality"?

                        • You are doing it by resizing the view. Yet I am not doing that, I am resizing the whole scene to fir the pixmap. As I think advised by @Pl45m4. What I have works (though there are those - 2s...). Doesn't it? is it better to do your way via view instead of scene?

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          ollarch
                          wrote on last edited by
                          #11

                          Hi,

                          The view is the one that takes care of the display part. What it does is show the scene with the indicated transformations. So, let the view do its job.
                          The view is not resizing, it only paints the scene and does not modify the scene. So, as I told you, the pixmap is still the same, what have changed is where are you looking it.

                          Think on a CAD software. The schematic have to keep the units regardless of the zoom applied or the rotation of the view. You can rotate the view, zoom it, ... but when you ask the view to map the mouse position into scene coordinates, this coordinates will still be the same pixel position.

                          Start with points 1 and 2. When you have it working, do the point 3. Finally point 4.
                          When you finish all this steps, call "rotate(90)" on your view and see what happens (what you see and the mouse positions).

                          JonBJ 2 Replies Last reply
                          1
                          • O ollarch

                            Hi,

                            The view is the one that takes care of the display part. What it does is show the scene with the indicated transformations. So, let the view do its job.
                            The view is not resizing, it only paints the scene and does not modify the scene. So, as I told you, the pixmap is still the same, what have changed is where are you looking it.

                            Think on a CAD software. The schematic have to keep the units regardless of the zoom applied or the rotation of the view. You can rotate the view, zoom it, ... but when you ask the view to map the mouse position into scene coordinates, this coordinates will still be the same pixel position.

                            Start with points 1 and 2. When you have it working, do the point 3. Finally point 4.
                            When you finish all this steps, call "rotate(90)" on your view and see what happens (what you see and the mouse positions).

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #12

                            @ollarch
                            Thank you. I will save what I have now and rewrite your way tomorrow, and report back :)

                            1 Reply Last reply
                            0
                            • O ollarch

                              Hi,

                              The view is the one that takes care of the display part. What it does is show the scene with the indicated transformations. So, let the view do its job.
                              The view is not resizing, it only paints the scene and does not modify the scene. So, as I told you, the pixmap is still the same, what have changed is where are you looking it.

                              Think on a CAD software. The schematic have to keep the units regardless of the zoom applied or the rotation of the view. You can rotate the view, zoom it, ... but when you ask the view to map the mouse position into scene coordinates, this coordinates will still be the same pixel position.

                              Start with points 1 and 2. When you have it working, do the point 3. Finally point 4.
                              When you finish all this steps, call "rotate(90)" on your view and see what happens (what you see and the mouse positions).

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #13

                              @ollarch
                              Firstly, I do understand what you are saying/your approach. I had been expecting to have to take the current scaling into account and do my own conversion of mouse coords to scene/pixmap coords. I agree that your way means the QGraphicsView will do this for me, and that makes me a happy bunny :)

                              I followed your instructions above exactly. It almost worked, but there is a little wrinkle!

                              When the window is first shown the pixmap is "small", does not fit whole area --- even though I had fitToView() in the constructor (where I add the pixmap) and a resizeEvent is generated. Once I then start interactive window resizing it jumps to correct, full size, and maintains that from then on correctly.

                              [Also your way I did not have to put in any "magic" - 2 on sizes, it never caused scrollbars to appear (even when I still had them allowed), so no need for "small pixel adjustment, which is an added plus point :) ]

                              I tried "everything" (I'm pretty thorough!) but nothing remedied this. So on a hunch I put in a showEvent() override to do the fitToView() there, and sure enough that, and only that, made it also be correct when first shown.

                              The essentials of my now-working code are:

                              void MapWindow::setupUi()
                              {
                                  // as per https://doc.qt.io/qt-5/qgraphicsview.html#fitInView
                                  // since we intend to always call `QGraphicsView::fitToView()` in `resizeEvent()`
                                  // we explicitly disable scrollbars to *ensure* no infinite recursion
                                  view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                                  view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                                  // create the map as a `QGraphicsPixmapItem`
                                  // `pixmapMap` will be fitted tightly in view in `showEvent()`/`resizeEvent()`
                                  this->pixmapMap = new QGraphicsPixmapItem(*pixmapOriginalMap);
                                  scene->addItem(pixmapMap);
                              }
                              
                              /*virtual*/ void MapWindow::showEvent(QShowEvent *showEvent) /*override*/
                              {
                                  // call the base method
                                  QWidget::showEvent(showEvent);
                              
                                  view->fitInView(pixmapMap, Qt::KeepAspectRatio);
                              }
                              
                              /*virtual*/ void MapWindow::resizeEvent(QResizeEvent *event) /*override*/
                              {
                                  // call the base method
                                  QWidget::resizeEvent(event);
                              
                                  view->fitInView(pixmapMap, Qt::KeepAspectRatio);
                              }
                              

                              [I am unsure about the need to call QWidget::showEvent/resizeEvent() base class, and where, in my overrides. Some examples do, some don't. With/without, or moving/before after the fitInView() call, all make no difference.]

                              I don't know whether you have any comment/alternative suggestion about this need to do fitView() in showEvent(), but I couldn't get it to work first time without?

                              O 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @ollarch
                                Firstly, I do understand what you are saying/your approach. I had been expecting to have to take the current scaling into account and do my own conversion of mouse coords to scene/pixmap coords. I agree that your way means the QGraphicsView will do this for me, and that makes me a happy bunny :)

                                I followed your instructions above exactly. It almost worked, but there is a little wrinkle!

                                When the window is first shown the pixmap is "small", does not fit whole area --- even though I had fitToView() in the constructor (where I add the pixmap) and a resizeEvent is generated. Once I then start interactive window resizing it jumps to correct, full size, and maintains that from then on correctly.

                                [Also your way I did not have to put in any "magic" - 2 on sizes, it never caused scrollbars to appear (even when I still had them allowed), so no need for "small pixel adjustment, which is an added plus point :) ]

                                I tried "everything" (I'm pretty thorough!) but nothing remedied this. So on a hunch I put in a showEvent() override to do the fitToView() there, and sure enough that, and only that, made it also be correct when first shown.

                                The essentials of my now-working code are:

                                void MapWindow::setupUi()
                                {
                                    // as per https://doc.qt.io/qt-5/qgraphicsview.html#fitInView
                                    // since we intend to always call `QGraphicsView::fitToView()` in `resizeEvent()`
                                    // we explicitly disable scrollbars to *ensure* no infinite recursion
                                    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                                    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                                    // create the map as a `QGraphicsPixmapItem`
                                    // `pixmapMap` will be fitted tightly in view in `showEvent()`/`resizeEvent()`
                                    this->pixmapMap = new QGraphicsPixmapItem(*pixmapOriginalMap);
                                    scene->addItem(pixmapMap);
                                }
                                
                                /*virtual*/ void MapWindow::showEvent(QShowEvent *showEvent) /*override*/
                                {
                                    // call the base method
                                    QWidget::showEvent(showEvent);
                                
                                    view->fitInView(pixmapMap, Qt::KeepAspectRatio);
                                }
                                
                                /*virtual*/ void MapWindow::resizeEvent(QResizeEvent *event) /*override*/
                                {
                                    // call the base method
                                    QWidget::resizeEvent(event);
                                
                                    view->fitInView(pixmapMap, Qt::KeepAspectRatio);
                                }
                                

                                [I am unsure about the need to call QWidget::showEvent/resizeEvent() base class, and where, in my overrides. Some examples do, some don't. With/without, or moving/before after the fitInView() call, all make no difference.]

                                I don't know whether you have any comment/alternative suggestion about this need to do fitView() in showEvent(), but I couldn't get it to work first time without?

                                O Offline
                                O Offline
                                ollarch
                                wrote on last edited by
                                #14

                                @JonB said in Exact correct size needed:

                                When the window is first shown the pixmap is "small", does not fit whole area

                                This is because when the widget constructor is executed, the widget size is not the "final" widget size. After the constructor is called you call "show" and then it calculates the size of the ui elements.

                                [I am unsure about the need to call QWidget::showEvent/resizeEvent() base class

                                You don't need to call it.

                                Can you check the values on the debugger of "event->s" and "event->olds" ? The first one is the new size and the last is the old size.

                                A workarround for this could be to use a "QTimer::singleshot(100,this,&YourWindowClass::fitMyPixmapToView)" into constructor.
                                , but I have not need to use it.

                                JonBJ 1 Reply Last reply
                                0
                                • O ollarch

                                  @JonB said in Exact correct size needed:

                                  When the window is first shown the pixmap is "small", does not fit whole area

                                  This is because when the widget constructor is executed, the widget size is not the "final" widget size. After the constructor is called you call "show" and then it calculates the size of the ui elements.

                                  [I am unsure about the need to call QWidget::showEvent/resizeEvent() base class

                                  You don't need to call it.

                                  Can you check the values on the debugger of "event->s" and "event->olds" ? The first one is the new size and the last is the old size.

                                  A workarround for this could be to use a "QTimer::singleshot(100,this,&YourWindowClass::fitMyPixmapToView)" into constructor.
                                  , but I have not need to use it.

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #15

                                  @ollarch said in Exact correct size needed:

                                  This is because when the widget constructor is executed, the widget size is not the "final" widget size.

                                  Yep, I do realize this :)

                                  After the constructor is called you call "show" and then it calculates the size of the ui elements.

                                  It doesn't matter when I call show(), I find I have to do the "fit" in showEvent(), i.e. when it's actually shown.

                                  Can you check the values on the debugger of "event->s" and "event->olds" ? The first one is the new size and the last is the old size.

                                  I did that right from the start. In the very first resizeEvent() the old size is always (-1, -1), while the (new) size is always the correct, big one. This is still the case even after I have fitted in showEvent(). So, it looks like I get the first resizeEvent() before the showEvent(), and at that point fitting doesn't work in resizeEvent() but it does in showEvent().

                                  12:33:20: Debugging starts
                                  resizeEvent QSize(-1, -1) QSize(1492, 772)
                                  showEvent
                                  // now start dragging
                                  resizeEvent QSize(1492, 772) QSize(1491, 772)
                                  resizeEvent QSize(1491, 772) QSize(1490, 772)
                                  resizeEvent QSize(1490, 772) QSize(1490, 771)
                                  ...
                                  

                                  A workarround for this could be to use a "QTimer::singleshot(100,this,&YourWindowClass::fitMyPixmapToView)" into constructor.

                                  Indeed I could have done that. But since it works via showEvent() I'm happy.

                                  What else can I say? :)

                                  1 Reply Last reply
                                  0
                                  • O Offline
                                    O Offline
                                    ollarch
                                    wrote on last edited by
                                    #16

                                    Hi, I just found that I use to call "showMaximized" on "main.cpp" instead of "show" to make the application full screen.
                                    I've tested with calling "show" and yes, it does not fit the item to the view the first time.

                                    I don't like too much this solution but you can use this

                                    QTimer::singleShot(50, this, SLOT(fit()));
                                    

                                    Define "fit" method as slot.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • O ollarch

                                      Hi, I just found that I use to call "showMaximized" on "main.cpp" instead of "show" to make the application full screen.
                                      I've tested with calling "show" and yes, it does not fit the item to the view the first time.

                                      I don't like too much this solution but you can use this

                                      QTimer::singleShot(50, this, SLOT(fit()));
                                      

                                      Define "fit" method as slot.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #17

                                      @ollarch
                                      Thanks, it sounds like (if I understand correctly) you are saying you find the same issue as me first time? Though I haven't seen this commented on elsewhere.

                                      As I said earlier, I have no problem with rectifying via your QTimer, but since I have rectified via showEvent() (as per my code) why are you not satisfied with that approach. any reason?

                                      I have moved on now to mouseworks, and am happy with my coordinates :)

                                      1 Reply Last reply
                                      0
                                      • O Offline
                                        O Offline
                                        ollarch
                                        wrote on last edited by ollarch
                                        #18

                                        As I said earlier, I have no problem with rectifying via your QTimer, but since I have rectified via showEvent() (as per my code) why are you not satisfied with that approach. any reason?

                                        No, I have any problem and I'm satisfied that it works.

                                        I have moved on now to mouseworks, and am happy with my coordinates :)

                                        Now you can try to rotate the view 45 degrees and you will see that when you map the view coordinates to scene coordinates you still have the correct pixel coordinate.

                                        JonBJ 1 Reply Last reply
                                        0
                                        • O ollarch

                                          As I said earlier, I have no problem with rectifying via your QTimer, but since I have rectified via showEvent() (as per my code) why are you not satisfied with that approach. any reason?

                                          No, I have any problem and I'm satisfied that it works.

                                          I have moved on now to mouseworks, and am happy with my coordinates :)

                                          Now you can try to rotate the view 45 degrees and you will see that when you map the view coordinates to scene coordinates you still have the correct pixel coordinate.

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #19

                                          @ollarch
                                          Indeed, though rotation is not something I will want to do, but I take your point.

                                          Overall I am very happy with the approach you stuck with and made me adopt for my situation. As I said, I thought I was going to have to do my own coordinate transformation arithmetic taking into account my current scale/zoom setting etc. I am much happier that all I have to do is call Qt's already-written mapToScene() to do it for me, and everything makes logical, "purist" sense to me.

                                          So thank you for sticking to your guns and recommending the approach I was really looking for!

                                          O 1 Reply Last reply
                                          0
                                          • S Offline
                                            S Offline
                                            sainivedant41
                                            wrote on last edited by sainivedant41
                                            #20

                                            Thanks for the clarification. mobdro

                                            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