PyQt size() returns 在显示小部件之前尺寸错误?
PyQt size() returns wrong size before showing the widget?
我试图通过检查按钮 1 的大小然后设置按钮 2 的大小来匹配按钮 1 和按钮 2 的大小,但是 size()
按钮 1 returns 的值不正确 ( 640, 480) 除非我先 show()
它。但是,如果我在完成布局设置之前显示它,它会在屏幕上闪烁,而我不想要的后续代码运行。
我该如何解决这个问题?
这是一个最小的例子:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QSize
import random
class MyButton(QtWidgets.QPushButton):
def __init__(self):
super().__init__("BUTTON1")
def sizeHint(self):
return QSize(100,100)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
# Button with sizeHint 100x100
btn1 = MyButton()
# There is a chance this button will be sized differently than its sizeHint wants
if random.randint(0, 1):
btn1.setFixedHeight(200)
# This line works if btn1.setFixedHeight was called, but otherwise gives the wrong height of 480px
height = btn1.size().height()
# I want btn2 to be the same height as btn1
btn2 = QtWidgets.QPushButton("BUTTON2")
btn2.setFixedHeight(height)
# Boilerplate
layout = QtWidgets.QHBoxLayout()
layout.addWidget(btn1)
layout.addWidget(btn2)
container = QtWidgets.QWidget()
container.setLayout(layout)
container.show()
sys.exit(app.exec_())
void QWidget::resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Note: Setter function for property size.
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QSize
import random
class MyButton(QtWidgets.QPushButton):
def __init__(self):
super().__init__("BUTTON1")
def sizeHint(self):
return QSize(100, 100)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
# Button with sizeHint 100x100
btn1 = MyButton()
btn1.resize(btn1.sizeHint()) # <========
# There is a chance this button will be sized differently than its sizeHint wants
# if random.randint(0, 1):
# btn1.setFixedHeight(200)
# print("btn1 2->", btn1.size())
# This line works if btn1.setFixedHeight was called, but otherwise gives the wrong height of 480px
height = btn1.size().height()
# I want btn2 to be the same height as btn1
btn2 = QtWidgets.QPushButton("BUTTON2")
btn2.setFixedHeight(height)
# Boilerplate
layout = QtWidgets.QHBoxLayout()
layout.addWidget(btn1)
layout.addWidget(btn2)
container = QtWidgets.QWidget()
container.setLayout(layout)
container.show()
sys.exit(app.exec_())
我试图通过检查按钮 1 的大小然后设置按钮 2 的大小来匹配按钮 1 和按钮 2 的大小,但是 size()
按钮 1 returns 的值不正确 ( 640, 480) 除非我先 show()
它。但是,如果我在完成布局设置之前显示它,它会在屏幕上闪烁,而我不想要的后续代码运行。
我该如何解决这个问题?
这是一个最小的例子:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QSize
import random
class MyButton(QtWidgets.QPushButton):
def __init__(self):
super().__init__("BUTTON1")
def sizeHint(self):
return QSize(100,100)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
# Button with sizeHint 100x100
btn1 = MyButton()
# There is a chance this button will be sized differently than its sizeHint wants
if random.randint(0, 1):
btn1.setFixedHeight(200)
# This line works if btn1.setFixedHeight was called, but otherwise gives the wrong height of 480px
height = btn1.size().height()
# I want btn2 to be the same height as btn1
btn2 = QtWidgets.QPushButton("BUTTON2")
btn2.setFixedHeight(height)
# Boilerplate
layout = QtWidgets.QHBoxLayout()
layout.addWidget(btn1)
layout.addWidget(btn2)
container = QtWidgets.QWidget()
container.setLayout(layout)
container.show()
sys.exit(app.exec_())
void QWidget::resize(int w, int h)
This corresponds to resize(QSize(w, h)). Note: Setter function for property size.
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QSize
import random
class MyButton(QtWidgets.QPushButton):
def __init__(self):
super().__init__("BUTTON1")
def sizeHint(self):
return QSize(100, 100)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
# Button with sizeHint 100x100
btn1 = MyButton()
btn1.resize(btn1.sizeHint()) # <========
# There is a chance this button will be sized differently than its sizeHint wants
# if random.randint(0, 1):
# btn1.setFixedHeight(200)
# print("btn1 2->", btn1.size())
# This line works if btn1.setFixedHeight was called, but otherwise gives the wrong height of 480px
height = btn1.size().height()
# I want btn2 to be the same height as btn1
btn2 = QtWidgets.QPushButton("BUTTON2")
btn2.setFixedHeight(height)
# Boilerplate
layout = QtWidgets.QHBoxLayout()
layout.addWidget(btn1)
layout.addWidget(btn2)
container = QtWidgets.QWidget()
container.setLayout(layout)
container.show()
sys.exit(app.exec_())