简体   繁体   English

centralWidget的大小似乎是错误的?

[英]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. 我试图找出一种方法来获得一个简单的QMainWindow来显示一个空的QWidget并向命令行报告实际的屏幕尺寸。 What works (modified ZetCode PyQt5 tutorial stuff): 什么有效(修改后的ZetCode PyQt5教程资料):

#!/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: 但是,当我取消注释所有已注释掉的initUI条目时,我得到:

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. 最高的值还可以,但是BR_Relative对我来说毫无意义。 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. geometry()rect()及其topLeft()bottomRight() ...它们都(几乎)显示相同的结果。

Where am I wrong? 我哪里错了?

In case its important: I am running a Raspbian powered RPi2 with Python 3.4/PyQT5. 如果它很重要:我正在运行带有Python 3.4 / PyQT5的Raspbian驱动的RPi2。 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. 之所以使用此脚本,是因为有一个框架可以在启动OMXplayer时容纳“内部” OMXplayer,并将获得的坐标移交给--win -parameter。 After launching, OMXplayer is supposed to overlay the empty centralWidget. 启动后,OMXplayer应该覆盖空的centralWidget。 But as soon as I add the menu or the toolbar, the OMXplayer window doesn't fit any more. 但是,一旦我添加了菜单或工具栏,OMXplayer窗口就不适合了。 Only statusBar works. 仅statusBar起作用。

A picture is worth a thousand words. 一张图片胜过千言万语。 From the QMainWindow docs: QMainWindow文档:

Qt主窗口框架

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 ; 中央小部件的原始高度为678 ; subtract 65 and you get 613 . 减去65 ,得到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()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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