有没有办法在 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 的中间。