简体   繁体   中英

Qt viewport size is wrong

I want to place an image in my Qt GUI. The issue that I have is with the scaling of the image after it has been loaded - it appears to be scaled down. That occurs due to an error in fitInView() routine that gets called to fit the image to the given window size. In particular, in my re-implementation of fitInView() there is the following line:

viewrect = self.viewport().rect()

viewport().rect() returns incorrect results because the image has not been shown yet, and that causes the Widget to have "bogus sizes", as has been pointed out in this forum thread .

Possible solutions that I found mostly agree with each other on one thing - one has to implement a showEvent() routine that would call fitInView() ; that way, the image would have time to properly load and the Widget viewport will be set to a correct value. I tried that:

def showEvent(self, event):
    self.fitInView()

That function is invoked after I choose an image that I want to upload, and call self.show() to show that image. However, implementing showEvent() did not solve the problem. Has anyone come across this issue before?

The solution turned out to be very similar to what I tried. Simply setting and fitting the pixmap after calling window.show() has fixed the size problem! It is also necessary to keep the showEvent() routine for this to work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM