2015-11-02 99 views
1

我想找出一种方法来获得一个简单的QMainWindow来显示一个空的QWidget并报告用于命令行的实际屏幕大小。是什么在起作用(修改ZetCode PyQt5教程的东西):centralWidget大小似乎错误?

#!/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在启动OMXplayer的时候“内部”将获得的坐标移交给它的--win-参数。启动后,OMXplayer应该覆盖空的中央控件。但是,只要我添加菜单或工具栏,OMXplayer窗口就不再适合了。只有statusBar有效。

回答

1

一张图片胜过千言万语。从QMainWindow文档:

Qt Main Window Framework

所以,因为窗口的尺寸保持不变,菜单栏和工具栏必须采取空间从中央部件的路程。中央部件的原始高度为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())) 
+0

谢谢!你让我发现我的思维错误。我假设height()显示了QWidget描述的图形中所述的中央窗口的右下角坐标(我现在没有找到链接)。所以......我必须复制中央窗口TL位置错误的名为BR“位置”才能得到真正的位置。 (有更好/更简单的方法吗?) – Bigfoot29

+0

@ Bigfoot29。看起来您可能忽略了一种可能性 - 请参阅我的最新答案。 – ekhumoro

+0

工程就像一个魅力!这也是可行的: X1 = self.centralWidget()。geometry()。x()\ n Y1 = self.centralWidget()。geometry()。y()\ n X2 = X1 + self.centralWidget ).geometry()。宽度()\ n Y2 = Y1 + self.centralWidget()。几何结构()。高度()\ n TL = QPoint(X1,Y1)\ n BR = QPoint(X2,Y2 )\ n (这将是我的代码方法的“直接”解决方案,但是你的更好,只给出了“内联”空间,并且对于这里的错误编码感到抱歉,注释中没有换行符:/) – Bigfoot29