gpt4 book ai didi

python - PyQt5 QFrame 的实际大小

转载 作者:行者123 更新时间:2023-12-01 09:13:19 28 4
gpt4 key购买 nike

有没有办法在 PyQt5 中获取 QFrame 的实际大小?我的显示器分辨率是 1920x1080。在我将 QFrame 添加到布局中后,在我的例子中,它填充了整个窗口,大约 1200px。然而 width() 命令返回 640。似乎默认大小 640x480 并且它不会因查询而改变?

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

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 的中间显示一个按钮。

最佳答案

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

关于python - PyQt5 QFrame 的实际大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51465953/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com