Get maximized window size when maximized?
-
I want to get the size of my QT Main Window or QT Label, since I am trying to make a full screen image viewer. The image management would be done in OpenCV and the GUI will be QT based. I need to resize the image to fit the viewer in OpenCV itself. Here is my code at present.
# importing libraries from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys width, height=0,0 class Window(QMainWindow): def __init__(self): super().__init__() # setting title self.setWindowTitle("WhiteCam Viewer") # setting geometry self.setGeometry(100, 100, 600, 400) # calling method self.UiComponents() # showing all the widgets self.show() # method for widgets def UiComponents(self): global width, height self.showFullScreen() self.imageLabel=QLabel("cooool",self) self.setCentralWidget(self.imageLabel) self.imageLabel.setStyleSheet("background-color: #FF0000") width=window.imageLabel.geometry().width() height=window.imageLabel.geometry().height() print(width, height) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # start the app sys.exit(App.exec())
The width and height are returned as 100, 30 when the expectation is 1920,1080
as can be seen from this image. How can I do so? -
@JoeCFD
Thanks!
What about a maximized window? I modified the code to be Fullscreen just to test if it gave the results I wanted. -
Hi and welcome to devnet,
Widgets do not have their final dimensions until they are shown hence your issue.
You can use the showEvent method to load the image in its final size. Since you are writing some sort of viewer, you should rather have a slot manage that part. For your current test, you can call it using a single shot timer with a time of 0.
On a side note, calling show or showFullScreen from within the constructor is a bad habit. Widget shall not show themselves, it's the responsibility of the method or class managing the widget to show it at the right time which may or may not be after construction.