Bigfoot29
Bigfoot29

Reputation: 70

centralWidget size seems erroneous?

I am trying to figure out a way to get a simple QMainWindow to show an empty QWidget and report the real screen size used to the command line. What works (modified ZetCode PyQt5 tutorial stuff):

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QWidget, QSizePolicy, QVBoxLayout
from PyQt5.QtCore import QPoint
from PyQt5.QtGui import QIcon


class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):               

        self.setCentralWidget(QWidget(self))

        exitAction = QAction(QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.close)

        self.statusBar()

        #menubar = self.menuBar()
        #fileMenu = menubar.addMenu('&File')
        #fileMenu.addAction(exitAction)

        #toolbar = self.addToolBar('Exit')
        #toolbar.addAction(exitAction)

        self.setGeometry(700, 100, 300, 700)
        self.setWindowTitle('Main window')    
        self.show()

        #TL -> topLeft
        TL = QPoint(self.centralWidget().geometry().x(), self.centralWidget().geometry().y())
        print("TL_Relative",TL)
        print("TL_Absolute:",self.mapToGlobal(TL))

        #BR -> bottomRight
        BR = QPoint(self.centralWidget().geometry().width(), self.centralWidget().geometry().height())
        print("BR_Relative",BR)
        print("BR_Absolute:",self.mapToGlobal(BR))


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Results are:

TL_Relative PyQt5.QtCore.QPoint()
TL_Absolute: PyQt5.QtCore.QPoint(700, 100)
BR_Relative PyQt5.QtCore.QPoint(300, 678)
BR_Absolute: PyQt5.QtCore.QPoint(1000, 778)

However, when I uncomment all the commented out initUI entries, I get:

TL_Relative PyQt5.QtCore.QPoint(0, 65)
TL_Absolute: PyQt5.QtCore.QPoint(700, 165)
BR_Relative PyQt5.QtCore.QPoint(300, 613)
BR_Absolute: PyQt5.QtCore.QPoint(1000, 713)

The top values are okay, but BR_Relative no sense to me. Adding things at the top of the screen removes height from the bottom?

I also tried a lot of other ways. geometry(), rect() with its topLeft() and bottomRight()... they all show (nearly) the same result.

Where am I wrong?

In case its important: I am running a Raspbian powered RPi2 with Python 3.4/PyQT5. Reason for this script is to have a framework that can hold OMXplayer "inside" handing over the gained coordinates to its --win-parameter when launching OMXplayer. After launching, OMXplayer is supposed to overlay the empty centralWidget. But as soon as I add the menu or the toolbar, the OMXplayer window doesn't fit any more. Only statusBar works.

Upvotes: 1

Views: 298

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

A picture is worth a thousand words. From the QMainWindow docs:

Qt Main Window Framework

So, since the window geometry remains the same, the menubar and toolbar must take space away from the central-widget. The original height of the central widget was 678; subtract 65 and you get 613.

To get the correct values, try:

    geometry = self.centralWidget().geometry()
    print("TL_Relative", geometry.topLeft())
    print("TL_Absolute:", self.mapToGlobal(geometry.topLeft()))

    print("BR_Relative", geometry.bottomRight())
    print("BR_Absolute:", self.mapToGlobal(geometry.bottomRight()))

Upvotes: 1

Related Questions