首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt / PySide -小部件的实际大小

PyQt / PySide -小部件的实际大小
EN

Stack Overflow用户
提问于 2016-07-10 16:05:03
回答 2查看 2.9K关注 0票数 2

我在Qt Designer中制作了一个包含QLineEdit的小工具。根据Qt Designer的说法,QLineEdit的高度是25 (对象属性,在几何图形下)。当我打印小工具的屏幕并测量实际像素大小时,它实际上是25像素。问题是,当我尝试用python代码读取QLineEdit的高度时,它显示为30。这是我尝试过的:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

发布于 2016-07-10 16:17:12

我发现了如何:

代码语言:javascript
复制
self.subject_text.sizeHint().height()

它给出了QLineEdit的实际高度。

票数 0
EN

Stack Overflow用户

发布于 2016-07-14 04:32:42

代码语言:javascript
复制
#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_())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38289902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档