我在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。
答案 0 :(得分:0)
我找到了:
self.subject_text.sizeHint().height()
它给出了QLineEdit的实际高度。
答案 1 :(得分:0)
#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_())