centralWidget 大小似乎有误?
centralWidget size seems erroneous?
我想找出一种方法来获得一个简单的 QMainWindow 来显示一个空的 QWidget 并报告用于命令行的实际屏幕大小。什么有效(修改 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_())
结果是:
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)
但是,当我取消注释所有已注释掉的 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)
最高值没问题,但 BR_Relative
对我来说没有意义。在屏幕顶部添加东西会从底部移除高度?
我也尝试了很多其他的方法。 geometry()
、rect()
及其 topLeft()
和 bottomRight()
...它们都显示(几乎)相同的结果。
我哪里错了?
如果它很重要:我是 运行 一个 Raspbian 驱动的 RPi2,带有 Python 3.4/PyQT5。这个脚本的原因是有一个框架可以让 OMXplayer "inside" 在启动 OMXplayer 时将获得的坐标移交给它的 --win
参数。启动后,OMXplayer 应该覆盖空的 centralWidget。但是一旦我添加了菜单或工具栏,OMXplayer window 就不再适合了。只有状态栏有效。
一图胜千言。来自 QMainWindow 文档:
因此,由于 window 几何形状保持不变,菜单栏和工具栏必须 space 远离中央小部件。中央小部件的原始高度为678
;减去 65
得到 613
.
要获得正确的值,请尝试:
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()))
我想找出一种方法来获得一个简单的 QMainWindow 来显示一个空的 QWidget 并报告用于命令行的实际屏幕大小。什么有效(修改 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_())
结果是:
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)
但是,当我取消注释所有已注释掉的 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)
最高值没问题,但 BR_Relative
对我来说没有意义。在屏幕顶部添加东西会从底部移除高度?
我也尝试了很多其他的方法。 geometry()
、rect()
及其 topLeft()
和 bottomRight()
...它们都显示(几乎)相同的结果。
我哪里错了?
如果它很重要:我是 运行 一个 Raspbian 驱动的 RPi2,带有 Python 3.4/PyQT5。这个脚本的原因是有一个框架可以让 OMXplayer "inside" 在启动 OMXplayer 时将获得的坐标移交给它的 --win
参数。启动后,OMXplayer 应该覆盖空的 centralWidget。但是一旦我添加了菜单或工具栏,OMXplayer window 就不再适合了。只有状态栏有效。
一图胜千言。来自 QMainWindow 文档:
因此,由于 window 几何形状保持不变,菜单栏和工具栏必须 space 远离中央小部件。中央小部件的原始高度为678
;减去 65
得到 613
.
要获得正确的值,请尝试:
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()))