QFrame的PyQt5实际大小

时间:2018-07-22 14:12:55

标签: python pyqt pyqt5 qframe

是否有办法在PyQt5中获取QFrame的实际大小?我的显示器分辨率为1920x1080。在我添加QFrame到布局后,在我的情况下,它填充了整个窗口,大约1200px。但是width()命令返回640。似乎默认尺寸为640x480,查询是否不变?

我希望能够在不使用布局b / c的情况下使小部件居中,我计划在设计中使用多个叠加层。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import PyQt5.QtWidgets
import sys

class MainWindow(QMainWindow):

   def __init__(self, parent):

    QMainWindow.__init__(self, parent)
    self.setWindowTitle('Sample')
    size_object = PyQt5.QtWidgets.QDesktopWidget ().screenGeometry ( -1 )  # active screen
    scr_width = size_object.width ()
    scr_height = size_object.height()
    self.resize ( 3 / 4 * scr_width, 3 / 4 * scr_height )

    qr = self.frameGeometry()  # geometry of the main window
    cp = QDesktopWidget().availableGeometry().center()  # center point of screen
    qr.moveCenter(cp)  # move rectangle's center point to screen's center point
    self.move(qr.topLeft())  # top left of rectangle becomes top left of window centering it

    self.setCentralWidget ( QWidget ( self ) )
    self.hbox = QHBoxLayout ()
    self.centralWidget ().setLayout ( self.hbox )

    self.frame_sample = QFrame ()
    self.hbox.addWidget ( self.frame_sample )
    self.frame_sample.setStyleSheet ( "background-color: rgb(200, 100, 255)" )


    button_go = QPushButton ("sample",  self.frame_sample )
    button_go.setStyleSheet (
        'QPushButton {background: red; yellow; font-weight: bold;'
        ' text-decoration: underline; text-align: middle;}' )
    button_go.resize ( 100, 100 )
    button_go.move ( (self.frame_sample.width () - button_go.width ()) / 2,
                     (self.frame_sample.height () - button_go.height ()) / 2 )


app = QApplication ( sys.argv )
myWindow = MainWindow ( None )
myWindow.show ()
app.exec_ ()

enter image description here

我希望根据计算出的位置在QFrame的中间显示一个按钮。

1 个答案:

答案 0 :(得分:1)

由于涉及到成本,因此不会立即更新几何图形。在显示小部件或需要更新小部件时,将完成Qt;在这种情况下,必须在显示小部件或调整小部件的大小时进行计算。

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setWindowTitle('Sample')
        size_object = QDesktopWidget().screenGeometry(-1)  # active screen
        scr_width = size_object.width ()
        scr_height = size_object.height()
        self.resize ( 3 / 4 * scr_width, 3 / 4 * scr_height )

        qr = self.frameGeometry()  # geometry of the main window
        cp = QDesktopWidget().availableGeometry().center()  # center point of screen
        qr.moveCenter(cp)  # move rectangle's center point to screen's center point
        self.move(qr.topLeft())  # top left of rectangle becomes top left of window centering it

        self.setCentralWidget(QWidget())
        self.hbox = QHBoxLayout(self.centralWidget())

        self.frame_sample = QFrame()
        self.hbox.addWidget(self.frame_sample )
        self.frame_sample.setStyleSheet( "background-color: rgb(200, 100, 255)" )


        self.button_go = QPushButton("sample",  self.frame_sample)
        self.button_go.setStyleSheet(
            'QPushButton {background: red; yellow; font-weight: bold;'
            ' text-decoration: underline; text-align: middle;}' )
        self.button_go.resize (100, 100)

    def event(self, e):
        if e.type() in (QEvent.Show, QEvent.Resize):
            geo = self.button_go.geometry()
            geo.moveCenter(self.frame_sample.rect().center())
            self.button_go.setGeometry(geo)

        return QMainWindow.event(self, e)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    myWindow = MainWindow()
    myWindow.show ()
    sys.exit(app.exec_())

enter image description here