我在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的实际高度,即25?
我使用的是PySide 1.2.4,Python2.7.10 32位,Windows7。
发布于 2016-07-10 16:17:12
我发现了如何:
self.subject_text.sizeHint().height()它给出了QLineEdit的实际高度。
发布于 2016-07-14 04:32:42
#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_())https://stackoverflow.com/questions/38289902
复制相似问题