3

有没有办法在 PyQt5 中获得 QFrame 的实际大小?我的显示器分辨率是 1920x1080。在我将 QFrame 添加到布局后,它会在我的情况下填充整个窗口约 1200 像素。但是 width() 命令返回 640。似乎默认大小为 640x480 并且查询时不会更改?

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

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_ ()

在此处输入图像描述

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

4

1 回答 1

5

几何图形不会立即更新,因为它涉及成本,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_())

在此处输入图像描述

于 2018-07-24T13:31:38.037 回答