PyQt/PySide—小部件的实际大小
我在Qt设计器中制作了一个小部件,其中包含QLineEdit。 根据Qt Designer,QLineEdit的高度为25(几何体下的对象属性)。 当我打印小部件的屏幕并测量实际像素大小时,它实际上是25像素。 问题是,当我试图用python代码读取QLineEdit的高度时,它显示为30。 这就是我所尝试的:PyQt/PySide—小部件的实际大小,qt,pyqt,pyside,Qt,Pyqt,Pyside,我在Qt设计器中制作了一个小部件,其中包含QLineEdit。 根据Qt Designer,QLineEdit的高度为25(几何体下的对象属性)。 当我打印小部件的屏幕并测量实际像素大小时,它实际上是25像素。 问题是,当我试图用python代码读取QLineEdit的高度时,它显示为30。 这就是我所尝试的: print self.subject_text.height() print self.subject_text.geometry().height() print self.subje
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的实际高度,即25
我使用PySide 1.2.4、Python 2.7.10 32位、windows 7。我发现了如何:
self.subject_text.sizeHint().height()
它给出了QLineEdit的实际高度。行编辑器内置了边距。这是一个奇怪的怪癖,但它们总是比预期的要短一点。您正在使用布局吗?大小提示只是一个提示。如果涉及布局,可能会改变实际高度。
#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_())