gor*_*yef 3 python pyqt pyqt5 qframe
有没有办法在 PyQt5 中获取 QFrame 的实际大小?我的显示器分辨率是 1920x1080。在我将 QFrame 添加到布局中后,在我的例子中,它填充了整个窗口,大约 1200px。然而 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 的中间。
几何图形不会立即更新,因为它涉及成本,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_())