PyQt / PySide - 小部件的实际大小
Posted
技术标签:
【中文标题】PyQt / PySide - 小部件的实际大小【英文标题】:PyQt / PySide - Actual size of a widget 【发布时间】:2016-07-10 08:05:03 【问题描述】:我在 Qt Designer 中制作了一个包含 QLineEdit 的小部件。 根据 Qt Designer 的说法,QLineEdit 的高度是 25(对象属性,在几何图形下)。 当我打印小部件的屏幕并测量实际像素大小时,它确实是 25 像素。 问题是当我尝试使用 python 代码读取 QLineEdit 的高度时,它显示为 30。 这是我尝试过的:
print self.subject_text.height()
print self.subject_text.geometry().height()
print self.subject_text.frameGeometry().height()
print self.subject_text.size()
print self.subject_text.frameSize().height()
print self.subject_text.rect()
他们都说 30。我想让 QPushButton 具有相同的高度,它使它比 QLineEdit 高 5 个像素。有什么方法可以读取 QLineEdit 的 ACTUAL 高度,即 25?
我使用 PySide 1.2.4、Python 2.7.10 32 位、Windows 7。
【问题讨论】:
行编辑器已内置边距。这是一个奇怪的怪癖,但它们总是比预期的要短一点。你在使用布局吗? 【参考方案1】:我找到了方法:
self.subject_text.sizeHint().height()
它给出了 QLineEdit 的实际高度。
【讨论】:
尺寸提示只是一个提示。如果涉及布局,可能会改变实际高度。【参考方案2】:#This is the example code for Edit the Height of the widget.
#If your are not expecting this answer, sorry.
#It is PyQt4, but you can try with PySide with small changes.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.verticalLayout = QtGui.QVBoxLayout (self)
self.verticalLayout.setObjectName ('verticalLayout')
self.label = QtGui.QLabel(self)
self.label.setObjectName('label')
self.label.setText('Height')
self.spinBox = QtGui.QSpinBox(self)
self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.spinBox.setObjectName('spinBox')
self.spinBox.setValue(20)
self.spinBox.setMaximum (1000)
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setText('Apply')
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName('horizontalLayout')
self.horizontalLayout.addWidget(self.label)
self.horizontalLayout.addWidget(self.spinBox)
self.horizontalLayout.addWidget(self.pushButton)
self.lineEdit = QtGui.QLineEdit(self)
self.lineEdit.setObjectName('lineEdit')
self.pushButton_1 = QtGui.QPushButton(self)
self.pushButton_1.setText('>')
self.pushButton_2 = QtGui.QPushButton(self)
self.pushButton_2.setObjectName('pushButton_2')
self.pushButton_2.setText('20')
self.horizontalLayout_1 = QtGui.QHBoxLayout()
self.horizontalLayout_1.setObjectName('horizontalLayout')
self.horizontalLayout_1.addWidget(self.lineEdit)
self.horizontalLayout_1.addWidget(self.pushButton_1)
self.horizontalLayout_1.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.horizontalLayout_1)
self.pushButton.clicked.connect (self.applyToLineEdit)
self.pushButton_1.clicked.connect (self.editPushButton)
def applyToLineEdit (self) :
currentSize = self.lineEdit.geometry ()
currentValue = self.spinBox.value()
w = currentSize.width()
#h = currentSize.height()
h = currentValue
x = currentSize.x()
z = currentSize.y()
self.lineEdit.setGeometry (QtCore.QRect (w, h, x, z))
self.lineEdit.setText ('My Height is %s'%str(h ))
#The below line because of self.lineEdit is under the horizontalLayout, either not necessary
self.lineEdit.setMinimumSize (QtCore.QSize(0, h))
def editPushButton (self) :
currentSize = self.lineEdit.geometry ()
w = currentSize.width()
h = currentSize.height()
x = currentSize.x()
z = currentSize.y()
self.pushButton_2.setGeometry (QtCore.QRect (w, h, x, z))
self.pushButton_2.setText (str(h))
#The below line because of self.lineEdit is under the horizontalLayout, either not necessary
self.pushButton_2.setMinimumSize (QtCore.QSize(0, h))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
【讨论】:
以上是关于PyQt / PySide - 小部件的实际大小的主要内容,如果未能解决你的问题,请参考以下文章
使用 PyQt4 或 PySide 捕获屏幕,除了特定的小部件?
将 PySide/PyQt 小部件嵌入到 Qt/C++ 应用程序中
Qt/PyQt4/PySide QDateEdit 日历弹窗掉屏
将 vtkOrientationMarkerWidget 与 QVTKRenderWindowInteractor 一起使用 [PyQt4/PySide]